(scm_srfi1_delete, scm_srfi1_delete_duplicates): Use a
[bpt/guile.git] / srfi / srfi-1.c
1 /* srfi-1.c --- SRFI-1 procedures for Guile
2 *
3 * Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2005, 2006
4 * Free Software Foundation, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <libguile.h>
22 #include <libguile/lang.h>
23
24 #include "srfi-1.h"
25
26 /* The intent of this file is to gradually replace those Scheme
27 * procedures in srfi-1.scm which extends core primitive procedures,
28 * so that using srfi-1 won't have performance penalties.
29 *
30 * Please feel free to contribute any new replacements!
31 */
32
33 static long
34 srfi1_ilength (SCM sx)
35 {
36 long i = 0;
37 SCM tortoise = sx;
38 SCM hare = sx;
39
40 do {
41 if (SCM_NULL_OR_NIL_P(hare)) return i;
42 if (!scm_is_pair (hare)) return -2;
43 hare = SCM_CDR(hare);
44 i++;
45 if (SCM_NULL_OR_NIL_P(hare)) return i;
46 if (!scm_is_pair (hare)) return -2;
47 hare = SCM_CDR(hare);
48 i++;
49 /* For every two steps the hare takes, the tortoise takes one. */
50 tortoise = SCM_CDR(tortoise);
51 }
52 while (! scm_is_eq (hare, tortoise));
53
54 /* If the tortoise ever catches the hare, then the list must contain
55 a cycle. */
56 return -1;
57 }
58
59 static SCM
60 equal_trampoline (SCM proc, SCM arg1, SCM arg2)
61 {
62 return scm_equal_p (arg1, arg2);
63 }
64
65 /* list_copy_part() copies the first COUNT cells of LST, puts the result at
66 *dst, and returns the SCM_CDRLOC of the last cell in that new list.
67
68 This function is designed to be careful about LST possibly having changed
69 in between the caller deciding what to copy, and the copy actually being
70 done here. The COUNT ensures we terminate if LST has become circular,
71 SCM_VALIDATE_CONS guards against a cdr in the list changed to some
72 non-pair object. */
73
74 #include <stdio.h>
75 static SCM *
76 list_copy_part (SCM lst, int count, SCM *dst)
77 #define FUNC_NAME "list_copy_part"
78 {
79 SCM c;
80 for ( ; count > 0; count--)
81 {
82 SCM_VALIDATE_CONS (SCM_ARGn, lst);
83 c = scm_cons (SCM_CAR (lst), SCM_EOL);
84 *dst = c;
85 dst = SCM_CDRLOC (c);
86 lst = SCM_CDR (lst);
87 }
88 return dst;
89 }
90 #undef FUNC_NAME
91
92
93 SCM_DEFINE (scm_srfi1_alist_copy, "alist-copy", 1, 0, 0,
94 (SCM alist),
95 "Return a copy of @var{alist}, copying both the pairs comprising\n"
96 "the list and those making the associations.")
97 #define FUNC_NAME s_scm_srfi1_alist_copy
98 {
99 SCM ret, *p, elem, c;
100
101 /* ret is the list to return. p is where to append to it, initially &ret
102 then SCM_CDRLOC of the last pair. */
103 ret = SCM_EOL;
104 p = &ret;
105
106 for ( ; scm_is_pair (alist); alist = SCM_CDR (alist))
107 {
108 elem = SCM_CAR (alist);
109
110 /* each element of alist must be a pair */
111 SCM_ASSERT_TYPE (scm_is_pair (elem), alist, SCM_ARG1, FUNC_NAME,
112 "association list");
113
114 c = scm_cons (scm_cons (SCM_CAR (elem), SCM_CDR (elem)), SCM_EOL);
115 *p = c;
116 p = SCM_CDRLOC (c);
117 }
118
119 /* alist must be a proper list */
120 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (alist), alist, SCM_ARG1, FUNC_NAME,
121 "association list");
122 return ret;
123 }
124 #undef FUNC_NAME
125
126
127 SCM_DEFINE (scm_srfi1_break, "break", 2, 0, 0,
128 (SCM pred, SCM lst),
129 "Return two values, the longest initial prefix of @var{lst}\n"
130 "whose elements all fail the predicate @var{pred}, and the\n"
131 "remainder of @var{lst}.\n"
132 "\n"
133 "Note that the name @code{break} conflicts with the @code{break}\n"
134 "binding established by @code{while}. Applications wanting to\n"
135 "use @code{break} from within a @code{while} loop will need to\n"
136 "make a new define under a different name.")
137 #define FUNC_NAME s_scm_srfi1_break
138 {
139 scm_t_trampoline_1 pred_tramp;
140 SCM ret, *p;
141
142 pred_tramp = scm_trampoline_1 (pred);
143 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
144
145 ret = SCM_EOL;
146 p = &ret;
147 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
148 {
149 SCM elem = SCM_CAR (lst);
150 if (scm_is_true (pred_tramp (pred, elem)))
151 goto done;
152
153 /* want this elem, tack it onto the end of ret */
154 *p = scm_cons (elem, SCM_EOL);
155 p = SCM_CDRLOC (*p);
156 }
157 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
158
159 done:
160 return scm_values (scm_list_2 (ret, lst));
161 }
162 #undef FUNC_NAME
163
164
165 SCM_DEFINE (scm_srfi1_break_x, "break!", 2, 0, 0,
166 (SCM pred, SCM lst),
167 "Return two values, the longest initial prefix of @var{lst}\n"
168 "whose elements all fail the predicate @var{pred}, and the\n"
169 "remainder of @var{lst}. @var{lst} may be modified to form the\n"
170 "return.")
171 #define FUNC_NAME s_scm_srfi1_break_x
172 {
173 SCM upto, *p;
174 scm_t_trampoline_1 pred_tramp;
175
176 pred_tramp = scm_trampoline_1 (pred);
177 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
178
179 p = &lst;
180 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
181 {
182 if (scm_is_true (pred_tramp (pred, SCM_CAR (upto))))
183 goto done;
184
185 /* want this element */
186 p = SCM_CDRLOC (upto);
187 }
188 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
189
190 done:
191 *p = SCM_EOL;
192 return scm_values (scm_list_2 (lst, upto));
193 }
194 #undef FUNC_NAME
195
196
197 SCM_DEFINE (scm_srfi1_car_plus_cdr, "car+cdr", 1, 0, 0,
198 (SCM pair),
199 "Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.")
200 #define FUNC_NAME s_scm_srfi1_car_plus_cdr
201 {
202 SCM_VALIDATE_CONS (SCM_ARG1, pair);
203 return scm_values (scm_list_2 (SCM_CAR (pair), SCM_CDR (pair)));
204 }
205 #undef FUNC_NAME
206
207
208 SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
209 (SCM lstlst),
210 "Construct a list by appending all lists in @var{lstlst}.\n"
211 "\n"
212 "@code{concatenate} is the same as @code{(apply append\n"
213 "@var{lstlst})}. It exists because some Scheme implementations\n"
214 "have a limit on the number of arguments a function takes, which\n"
215 "the @code{apply} might exceed. In Guile there is no such\n"
216 "limit.")
217 #define FUNC_NAME s_scm_srfi1_concatenate
218 {
219 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
220 return scm_append (lstlst);
221 }
222 #undef FUNC_NAME
223
224
225 SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
226 (SCM lstlst),
227 "Construct a list by appending all lists in @var{lstlst}. Those\n"
228 "lists may be modified to produce the result.\n"
229 "\n"
230 "@code{concatenate!} is the same as @code{(apply append!\n"
231 "@var{lstlst})}. It exists because some Scheme implementations\n"
232 "have a limit on the number of arguments a function takes, which\n"
233 "the @code{apply} might exceed. In Guile there is no such\n"
234 "limit.")
235 #define FUNC_NAME s_scm_srfi1_concatenate
236 {
237 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
238 return scm_append_x (lstlst);
239 }
240 #undef FUNC_NAME
241
242
243 SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
244 (SCM pred, SCM list1, SCM rest),
245 "Return a count of the number of times @var{pred} returns true\n"
246 "when called on elements from the given lists.\n"
247 "\n"
248 "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
249 "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
250 "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
251 "with the first element of each list, the second with the second\n"
252 "element from each, and so on.\n"
253 "\n"
254 "Counting stops when the end of the shortest list is reached.\n"
255 "At least one list must be non-circular.")
256 #define FUNC_NAME s_scm_srfi1_count
257 {
258 long count;
259 SCM lst;
260 int argnum;
261 SCM_VALIDATE_REST_ARGUMENT (rest);
262
263 count = 0;
264
265 if (scm_is_null (rest))
266 {
267 /* one list */
268 scm_t_trampoline_1 pred_tramp;
269 pred_tramp = scm_trampoline_1 (pred);
270 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
271
272 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
273 count += scm_is_true (pred_tramp (pred, SCM_CAR (list1)));
274
275 /* check below that list1 is a proper list, and done */
276 end_list1:
277 lst = list1;
278 argnum = 2;
279 }
280 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
281 {
282 /* two lists */
283 scm_t_trampoline_2 pred_tramp;
284 SCM list2;
285
286 pred_tramp = scm_trampoline_2 (pred);
287 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
288
289 list2 = SCM_CAR (rest);
290 for (;;)
291 {
292 if (! scm_is_pair (list1))
293 goto end_list1;
294 if (! scm_is_pair (list2))
295 {
296 lst = list2;
297 argnum = 3;
298 break;
299 }
300 count += scm_is_true (pred_tramp
301 (pred, SCM_CAR (list1), SCM_CAR (list2)));
302 list1 = SCM_CDR (list1);
303 list2 = SCM_CDR (list2);
304 }
305 }
306 else
307 {
308 /* three or more lists */
309 SCM vec, args, a;
310 size_t len, i;
311
312 /* vec is the list arguments */
313 vec = scm_vector (scm_cons (list1, rest));
314 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
315
316 /* args is the argument list to pass to pred, same length as vec,
317 re-used for each call */
318 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
319
320 for (;;)
321 {
322 /* first elem of each list in vec into args, and step those
323 vec entries onto their next element */
324 for (i = 0, a = args, argnum = 2;
325 i < len;
326 i++, a = SCM_CDR (a), argnum++)
327 {
328 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
329 if (! scm_is_pair (lst))
330 goto check_lst_and_done;
331 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
332 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
333 }
334
335 count += scm_is_true (scm_apply (pred, args, SCM_EOL));
336 }
337 }
338
339 check_lst_and_done:
340 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
341 return scm_from_long (count);
342 }
343 #undef FUNC_NAME
344
345
346 SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
347 (SCM x, SCM lst, SCM pred),
348 "Return a list containing the elements of @var{lst} but with\n"
349 "those equal to @var{x} deleted. The returned elements will be\n"
350 "in the same order as they were in @var{lst}.\n"
351 "\n"
352 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
353 "given. An equality call is made just once for each element,\n"
354 "but the order in which the calls are made on the elements is\n"
355 "unspecified.\n"
356 "\n"
357 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
358 "given @var{x} is first. This means for instance elements\n"
359 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
360 "\n"
361 "@var{lst} is not modified, but the returned list might share a\n"
362 "common tail with @var{lst}.")
363 #define FUNC_NAME s_scm_srfi1_delete
364 {
365 scm_t_trampoline_2 equal_p;
366 SCM ret, *p, keeplst;
367 int count;
368
369 if (SCM_UNBNDP (pred))
370 return scm_delete (x, lst);
371
372 equal_p = scm_trampoline_2 (pred);
373 SCM_ASSERT (equal_p, pred, SCM_ARG3, FUNC_NAME);
374
375 /* ret is the return list being constructed. p is where to append to it,
376 initially &ret then SCM_CDRLOC of the last pair. lst progresses as
377 elements are considered.
378
379 Elements to be retained are not immediately copied, instead keeplst is
380 the last pair in lst which is to be retained but not yet copied, count
381 is how many from there are wanted. When there's no more deletions, *p
382 can be set to keeplst to share the remainder of the original lst. (The
383 entire original lst if there's no deletions at all.) */
384
385 keeplst = lst;
386 count = 0;
387 p = &ret;
388
389 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
390 {
391 if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
392 {
393 /* delete this element, so copy those at keeplst */
394 p = list_copy_part (keeplst, count, p);
395 keeplst = SCM_CDR (lst);
396 count = 0;
397 }
398 else
399 {
400 /* keep this element */
401 count++;
402 }
403 }
404
405 /* final retained elements */
406 *p = keeplst;
407
408 /* demand that lst was a proper list */
409 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
410
411 return ret;
412 }
413 #undef FUNC_NAME
414
415
416 SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
417 (SCM x, SCM lst, SCM pred),
418 "Return a list containing the elements of @var{lst} but with\n"
419 "those equal to @var{x} deleted. The returned elements will be\n"
420 "in the same order as they were in @var{lst}.\n"
421 "\n"
422 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
423 "given. An equality call is made just once for each element,\n"
424 "but the order in which the calls are made on the elements is\n"
425 "unspecified.\n"
426 "\n"
427 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
428 "given @var{x} is first. This means for instance elements\n"
429 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
430 "\n"
431 "@var{lst} may be modified to construct the returned list.")
432 #define FUNC_NAME s_scm_srfi1_delete_x
433 {
434 scm_t_trampoline_2 equal_p;
435 SCM walk;
436 SCM *prev;
437
438 if (SCM_UNBNDP (pred))
439 return scm_delete_x (x, lst);
440
441 equal_p = scm_trampoline_2 (pred);
442 SCM_ASSERT (equal_p, pred, SCM_ARG3, FUNC_NAME);
443
444 for (prev = &lst, walk = lst;
445 scm_is_pair (walk);
446 walk = SCM_CDR (walk))
447 {
448 if (scm_is_true (equal_p (pred, x, SCM_CAR (walk))))
449 *prev = SCM_CDR (walk);
450 else
451 prev = SCM_CDRLOC (walk);
452 }
453
454 /* demand the input was a proper list */
455 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
456 return lst;
457 }
458 #undef FUNC_NAME
459
460
461 SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
462 (SCM lst, SCM pred),
463 "Return a list containing the elements of @var{lst} but without\n"
464 "duplicates.\n"
465 "\n"
466 "When elements are equal, only the first in @var{lst} is\n"
467 "retained. Equal elements can be anywhere in @var{lst}, they\n"
468 "don't have to be adjacent. The returned list will have the\n"
469 "retained elements in the same order as they were in @var{lst}.\n"
470 "\n"
471 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
472 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
473 "being before @var{y} in @var{lst}. A call is made at most once\n"
474 "for each combination, but the sequence of the calls across the\n"
475 "elements is unspecified.\n"
476 "\n"
477 "@var{lst} is not modified, but the return might share a common\n"
478 "tail with @var{lst}.\n"
479 "\n"
480 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
481 "it must check each element against all those preceding it. For\n"
482 "long lists it is more efficient to sort and then compare only\n"
483 "adjacent elements.")
484 #define FUNC_NAME s_scm_srfi1_delete_duplicates
485 {
486 scm_t_trampoline_2 equal_p;
487 SCM ret, *p, keeplst, item, l;
488 int count, i;
489
490 /* ret is the new list constructed. p is where to append, initially &ret
491 then SCM_CDRLOC of the last pair. lst is advanced as each element is
492 considered.
493
494 Elements retained are not immediately appended to ret, instead keeplst
495 is the last pair in lst which is to be kept but is not yet copied.
496 Initially this is the first pair of lst, since the first element is
497 always retained.
498
499 *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
500 the elements retained, making the equality search loop easy.
501
502 If an item must be deleted, elements from keeplst (inclusive) to lst
503 (exclusive) must be copied and appended to ret. When there's no more
504 deletions, *p is left set to keeplst, so ret shares structure with the
505 original lst. (ret will be the entire original lst if there are no
506 deletions.) */
507
508 /* skip to end if an empty list (or something invalid) */
509 ret = SCM_EOL;
510
511 if (SCM_UNBNDP (pred))
512 equal_p = equal_trampoline;
513 else
514 {
515 equal_p = scm_trampoline_2 (pred);
516 SCM_ASSERT (equal_p, pred, SCM_ARG2, FUNC_NAME);
517 }
518
519 keeplst = lst;
520 count = 0;
521 p = &ret;
522
523 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
524 {
525 item = SCM_CAR (lst);
526
527 /* look for item in "ret" list */
528 for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
529 {
530 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
531 {
532 /* "item" is a duplicate, so copy keeplst onto ret */
533 duplicate:
534 p = list_copy_part (keeplst, count, p);
535
536 keeplst = SCM_CDR (lst); /* elem after the one deleted */
537 count = 0;
538 goto next_elem;
539 }
540 }
541
542 /* look for item in "keeplst" list
543 be careful traversing, in case nasty code changed the cdrs */
544 for (i = 0, l = keeplst;
545 i < count && scm_is_pair (l);
546 i++, l = SCM_CDR (l))
547 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
548 goto duplicate;
549
550 /* keep this element */
551 count++;
552
553 next_elem:
554 ;
555 }
556 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
557
558 /* share tail of keeplst items */
559 *p = keeplst;
560
561 return ret;
562 }
563 #undef FUNC_NAME
564
565
566 SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
567 (SCM lst, SCM pred),
568 "Return a list containing the elements of @var{lst} but without\n"
569 "duplicates.\n"
570 "\n"
571 "When elements are equal, only the first in @var{lst} is\n"
572 "retained. Equal elements can be anywhere in @var{lst}, they\n"
573 "don't have to be adjacent. The returned list will have the\n"
574 "retained elements in the same order as they were in @var{lst}.\n"
575 "\n"
576 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
577 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
578 "being before @var{y} in @var{lst}. A call is made at most once\n"
579 "for each combination, but the sequence of the calls across the\n"
580 "elements is unspecified.\n"
581 "\n"
582 "@var{lst} may be modified to construct the returned list.\n"
583 "\n"
584 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
585 "it must check each element against all those preceding it. For\n"
586 "long lists it is more efficient to sort and then compare only\n"
587 "adjacent elements.")
588 #define FUNC_NAME s_scm_srfi1_delete_duplicates_x
589 {
590 scm_t_trampoline_2 equal_p;
591 SCM ret, endret, item, l;
592
593 /* ret is the return list, constructed from the pairs in lst. endret is
594 the last pair of ret, initially the first pair. lst is advanced as
595 elements are considered. */
596
597 /* skip to end if an empty list (or something invalid) */
598 ret = lst;
599 if (scm_is_pair (lst))
600 {
601 if (SCM_UNBNDP (pred))
602 equal_p = equal_trampoline;
603 else
604 {
605 equal_p = scm_trampoline_2 (pred);
606 SCM_ASSERT (equal_p, pred, SCM_ARG2, FUNC_NAME);
607 }
608
609 endret = ret;
610
611 /* loop over lst elements starting from second */
612 for (;;)
613 {
614 lst = SCM_CDR (lst);
615 if (! scm_is_pair (lst))
616 break;
617 item = SCM_CAR (lst);
618
619 /* is item equal to any element from ret to endret (inclusive)? */
620 l = ret;
621 for (;;)
622 {
623 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
624 break; /* equal, forget this element */
625
626 if (scm_is_eq (l, endret))
627 {
628 /* not equal to any, so append this pair */
629 SCM_SETCDR (endret, lst);
630 endret = lst;
631 break;
632 }
633 l = SCM_CDR (l);
634 }
635 }
636
637 /* terminate, in case last element was deleted */
638 SCM_SETCDR (endret, SCM_EOL);
639 }
640
641 /* demand that lst was a proper list */
642 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
643
644 return ret;
645 }
646 #undef FUNC_NAME
647
648
649 SCM_DEFINE (scm_srfi1_drop_right, "drop-right", 2, 0, 0,
650 (SCM lst, SCM n),
651 "Return a new list containing all except the last @var{n}\n"
652 "elements of @var{lst}.")
653 #define FUNC_NAME s_scm_srfi1_drop_right
654 {
655 SCM tail = scm_list_tail (lst, n);
656 SCM ret = SCM_EOL;
657 SCM *rend = &ret;
658 while (scm_is_pair (tail))
659 {
660 *rend = scm_cons (SCM_CAR (lst), SCM_EOL);
661 rend = SCM_CDRLOC (*rend);
662
663 lst = SCM_CDR (lst);
664 tail = SCM_CDR (tail);
665 }
666 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
667 return ret;
668 }
669 #undef FUNC_NAME
670
671
672 SCM_DEFINE (scm_srfi1_drop_right_x, "drop-right!", 2, 0, 0,
673 (SCM lst, SCM n),
674 "Return the a list containing the @var{n} last elements of\n"
675 "@var{lst}. @var{lst} may be modified to build the return.")
676 #define FUNC_NAME s_scm_srfi1_drop_right_x
677 {
678 SCM tail, *p;
679
680 if (scm_is_eq (n, SCM_INUM0))
681 return lst;
682
683 tail = scm_list_tail (lst, n);
684 p = &lst;
685
686 /* p and tail work along the list, p being the cdrloc of the cell n steps
687 behind tail */
688 for ( ; scm_is_pair (tail); tail = SCM_CDR (tail))
689 p = SCM_CDRLOC (*p);
690
691 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
692
693 *p = SCM_EOL;
694 return lst;
695 }
696 #undef FUNC_NAME
697
698
699 SCM_DEFINE (scm_srfi1_drop_while, "drop-while", 2, 0, 0,
700 (SCM pred, SCM lst),
701 "Drop the longest initial prefix of @var{lst} whose elements all\n"
702 "satisfy the predicate @var{pred}.")
703 #define FUNC_NAME s_scm_srfi1_drop_while
704 {
705 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
706 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
707
708 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
709 if (scm_is_false (pred_tramp (pred, SCM_CAR (lst))))
710 goto done;
711
712 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
713 done:
714 return lst;
715 }
716 #undef FUNC_NAME
717
718
719 SCM_DEFINE (scm_srfi1_eighth, "eighth", 1, 0, 0,
720 (SCM lst),
721 "Return the eighth element of @var{lst}.")
722 #define FUNC_NAME s_scm_srfi1_eighth
723 {
724 return scm_list_ref (lst, SCM_I_MAKINUM (7));
725 }
726 #undef FUNC_NAME
727
728
729 SCM_DEFINE (scm_srfi1_fifth, "fifth", 1, 0, 0,
730 (SCM lst),
731 "Return the fifth element of @var{lst}.")
732 #define FUNC_NAME s_scm_srfi1_fifth
733 {
734 return scm_list_ref (lst, SCM_I_MAKINUM (4));
735 }
736 #undef FUNC_NAME
737
738
739 SCM_DEFINE (scm_srfi1_filter_map, "filter-map", 2, 0, 1,
740 (SCM proc, SCM list1, SCM rest),
741 "Apply @var{proc} to to the elements of @var{list1} @dots{} and\n"
742 "return a list of the results as per SRFI-1 @code{map}, except\n"
743 "that any @code{#f} results are omitted from the list returned.")
744 #define FUNC_NAME s_scm_srfi1_filter_map
745 {
746 SCM ret, *loc, elem, newcell, lst;
747 int argnum;
748
749 SCM_VALIDATE_REST_ARGUMENT (rest);
750
751 ret = SCM_EOL;
752 loc = &ret;
753
754 if (scm_is_null (rest))
755 {
756 /* one list */
757 scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
758 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
759
760 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
761 {
762 elem = proc_tramp (proc, SCM_CAR (list1));
763 if (scm_is_true (elem))
764 {
765 newcell = scm_cons (elem, SCM_EOL);
766 *loc = newcell;
767 loc = SCM_CDRLOC (newcell);
768 }
769 }
770
771 /* check below that list1 is a proper list, and done */
772 end_list1:
773 lst = list1;
774 argnum = 2;
775 }
776 else if (scm_is_null (SCM_CDR (rest)))
777 {
778 /* two lists */
779 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
780 SCM list2 = SCM_CAR (rest);
781 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
782
783 for (;;)
784 {
785 if (! scm_is_pair (list1))
786 goto end_list1;
787 if (! scm_is_pair (list2))
788 {
789 lst = list2;
790 argnum = 3;
791 goto check_lst_and_done;
792 }
793 elem = proc_tramp (proc, SCM_CAR (list1), SCM_CAR (list2));
794 if (scm_is_true (elem))
795 {
796 newcell = scm_cons (elem, SCM_EOL);
797 *loc = newcell;
798 loc = SCM_CDRLOC (newcell);
799 }
800 list1 = SCM_CDR (list1);
801 list2 = SCM_CDR (list2);
802 }
803 }
804 else
805 {
806 /* three or more lists */
807 SCM vec, args, a;
808 size_t len, i;
809
810 /* vec is the list arguments */
811 vec = scm_vector (scm_cons (list1, rest));
812 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
813
814 /* args is the argument list to pass to proc, same length as vec,
815 re-used for each call */
816 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
817
818 for (;;)
819 {
820 /* first elem of each list in vec into args, and step those
821 vec entries onto their next element */
822 for (i = 0, a = args, argnum = 2;
823 i < len;
824 i++, a = SCM_CDR (a), argnum++)
825 {
826 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
827 if (! scm_is_pair (lst))
828 goto check_lst_and_done;
829 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for proc */
830 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
831 }
832
833 elem = scm_apply (proc, args, SCM_EOL);
834 if (scm_is_true (elem))
835 {
836 newcell = scm_cons (elem, SCM_EOL);
837 *loc = newcell;
838 loc = SCM_CDRLOC (newcell);
839 }
840 }
841 }
842
843 check_lst_and_done:
844 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
845 return ret;
846 }
847 #undef FUNC_NAME
848
849
850 SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
851 (SCM pred, SCM lst),
852 "Return the first element of @var{lst} which satisfies the\n"
853 "predicate @var{pred}, or return @code{#f} if no such element is\n"
854 "found.")
855 #define FUNC_NAME s_scm_srfi1_find
856 {
857 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
858 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
859
860 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
861 {
862 SCM elem = SCM_CAR (lst);
863 if (scm_is_true (pred_tramp (pred, elem)))
864 return elem;
865 }
866 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
867
868 return SCM_BOOL_F;
869 }
870 #undef FUNC_NAME
871
872
873 SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
874 (SCM pred, SCM lst),
875 "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
876 "predicate @var{pred}, or return @code{#f} if no such element is\n"
877 "found.")
878 #define FUNC_NAME s_scm_srfi1_find_tail
879 {
880 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
881 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
882
883 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
884 if (scm_is_true (pred_tramp (pred, SCM_CAR (lst))))
885 return lst;
886 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
887
888 return SCM_BOOL_F;
889 }
890 #undef FUNC_NAME
891
892
893 SCM_DEFINE (scm_srfi1_fold, "fold", 3, 0, 1,
894 (SCM proc, SCM init, SCM list1, SCM rest),
895 "Apply @var{proc} to the elements of @var{lst1} @dots{}\n"
896 "@var{lstN} to build a result, and return that result.\n"
897 "\n"
898 "Each @var{proc} call is @code{(@var{proc} @var{elem1} @dots{}\n"
899 "@var{elemN} @var{previous})}, where @var{elem1} is from\n"
900 "@var{lst1}, through @var{elemN} from @var{lstN}.\n"
901 "@var{previous} is the return from the previous call to\n"
902 "@var{proc}, or the given @var{init} for the first call. If any\n"
903 "list is empty, just @var{init} is returned.\n"
904 "\n"
905 "@code{fold} works through the list elements from first to last.\n"
906 "The following shows a list reversal and the calls it makes,\n"
907 "\n"
908 "@example\n"
909 "(fold cons '() '(1 2 3))\n"
910 "\n"
911 "(cons 1 '())\n"
912 "(cons 2 '(1))\n"
913 "(cons 3 '(2 1)\n"
914 "@result{} (3 2 1)\n"
915 "@end example\n"
916 "\n"
917 "If @var{lst1} through @var{lstN} have different lengths,\n"
918 "@code{fold} stops when the end of the shortest is reached.\n"
919 "Ie.@: elements past the length of the shortest are ignored in\n"
920 "the other @var{lst}s. At least one @var{lst} must be\n"
921 "non-circular.\n"
922 "\n"
923 "The way @code{fold} builds a result from iterating is quite\n"
924 "general, it can do more than other iterations like say\n"
925 "@code{map} or @code{filter}. The following for example removes\n"
926 "adjacent duplicate elements from a list,\n"
927 "\n"
928 "@example\n"
929 "(define (delete-adjacent-duplicates lst)\n"
930 " (fold-right (lambda (elem ret)\n"
931 " (if (equal? elem (first ret))\n"
932 " ret\n"
933 " (cons elem ret)))\n"
934 " (list (last lst))\n"
935 " lst))\n"
936 "(delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))\n"
937 "@result{} (1 2 3 4 5)\n"
938 "@end example\n"
939 "\n"
940 "Clearly the same sort of thing can be done with a\n"
941 "@code{for-each} and a variable in which to build the result,\n"
942 "but a self-contained @var{proc} can be re-used in multiple\n"
943 "contexts, where a @code{for-each} would have to be written out\n"
944 "each time.")
945 #define FUNC_NAME s_scm_srfi1_fold
946 {
947 SCM lst;
948 int argnum;
949 SCM_VALIDATE_REST_ARGUMENT (rest);
950
951 if (scm_is_null (rest))
952 {
953 /* one list */
954 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
955 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
956
957 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
958 init = proc_tramp (proc, SCM_CAR (list1), init);
959
960 /* check below that list1 is a proper list, and done */
961 lst = list1;
962 argnum = 2;
963 }
964 else
965 {
966 /* two or more lists */
967 SCM vec, args, a;
968 size_t len, i;
969
970 /* vec is the list arguments */
971 vec = scm_vector (scm_cons (list1, rest));
972 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
973
974 /* args is the argument list to pass to proc, same length as vec,
975 re-used for each call */
976 args = scm_make_list (SCM_I_MAKINUM (len+1), SCM_UNDEFINED);
977
978 for (;;)
979 {
980 /* first elem of each list in vec into args, and step those
981 vec entries onto their next element */
982 for (i = 0, a = args, argnum = 2;
983 i < len;
984 i++, a = SCM_CDR (a), argnum++)
985 {
986 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
987 if (! scm_is_pair (lst))
988 goto check_lst_and_done;
989 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for proc */
990 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
991 }
992 SCM_SETCAR (a, init);
993
994 init = scm_apply (proc, args, SCM_EOL);
995 }
996 }
997
998 check_lst_and_done:
999 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
1000 return init;
1001 }
1002 #undef FUNC_NAME
1003
1004
1005 SCM_DEFINE (scm_srfi1_last, "last", 1, 0, 0,
1006 (SCM lst),
1007 "Like @code{cons}, but with interchanged arguments. Useful\n"
1008 "mostly when passed to higher-order procedures.")
1009 #define FUNC_NAME s_scm_srfi1_last
1010 {
1011 SCM pair = scm_last_pair (lst);
1012 /* scm_last_pair returns SCM_EOL for an empty list */
1013 SCM_VALIDATE_CONS (SCM_ARG1, pair);
1014 return SCM_CAR (pair);
1015 }
1016 #undef FUNC_NAME
1017
1018
1019 SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
1020 (SCM lst),
1021 "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
1022 "circular.")
1023 #define FUNC_NAME s_scm_srfi1_length_plus
1024 {
1025 long len = scm_ilength (lst);
1026 return (len >= 0 ? SCM_I_MAKINUM (len) : SCM_BOOL_F);
1027 }
1028 #undef FUNC_NAME
1029
1030
1031 SCM_DEFINE (scm_srfi1_list_index, "list-index", 2, 0, 1,
1032 (SCM pred, SCM list1, SCM rest),
1033 "Return the index of the first set of elements, one from each of\n"
1034 "@var{lst1}@dots{}@var{lstN}, which satisfies @var{pred}.\n"
1035 "\n"
1036 "@var{pred} is called as @code{(@var{pred} elem1 @dots{}\n"
1037 "elemN)}. Searching stops when the end of the shortest\n"
1038 "@var{lst} is reached. The return index starts from 0 for the\n"
1039 "first set of elements. If no set of elements pass then the\n"
1040 "return is @code{#f}.\n"
1041 "\n"
1042 "@example\n"
1043 "(list-index odd? '(2 4 6 9)) @result{} 3\n"
1044 "(list-index = '(1 2 3) '(3 1 2)) @result{} #f\n"
1045 "@end example")
1046 #define FUNC_NAME s_scm_srfi1_list_index
1047 {
1048 long n = 0;
1049 SCM lst;
1050 int argnum;
1051 SCM_VALIDATE_REST_ARGUMENT (rest);
1052
1053 if (scm_is_null (rest))
1054 {
1055 /* one list */
1056 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
1057 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1058
1059 for ( ; scm_is_pair (list1); n++, list1 = SCM_CDR (list1))
1060 if (scm_is_true (pred_tramp (pred, SCM_CAR (list1))))
1061 return SCM_I_MAKINUM (n);
1062
1063 /* not found, check below that list1 is a proper list */
1064 end_list1:
1065 lst = list1;
1066 argnum = 2;
1067 }
1068 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
1069 {
1070 /* two lists */
1071 SCM list2 = SCM_CAR (rest);
1072 scm_t_trampoline_2 pred_tramp = scm_trampoline_2 (pred);
1073 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1074
1075 for ( ; ; n++)
1076 {
1077 if (! scm_is_pair (list1))
1078 goto end_list1;
1079 if (! scm_is_pair (list2))
1080 {
1081 lst = list2;
1082 argnum = 3;
1083 break;
1084 }
1085 if (scm_is_true (pred_tramp (pred,
1086 SCM_CAR (list1), SCM_CAR (list2))))
1087 return SCM_I_MAKINUM (n);
1088
1089 list1 = SCM_CDR (list1);
1090 list2 = SCM_CDR (list2);
1091 }
1092 }
1093 else
1094 {
1095 /* three or more lists */
1096 SCM vec, args, a;
1097 size_t len, i;
1098
1099 /* vec is the list arguments */
1100 vec = scm_vector (scm_cons (list1, rest));
1101 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
1102
1103 /* args is the argument list to pass to pred, same length as vec,
1104 re-used for each call */
1105 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
1106
1107 for ( ; ; n++)
1108 {
1109 /* first elem of each list in vec into args, and step those
1110 vec entries onto their next element */
1111 for (i = 0, a = args, argnum = 2;
1112 i < len;
1113 i++, a = SCM_CDR (a), argnum++)
1114 {
1115 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
1116 if (! scm_is_pair (lst))
1117 goto not_found_check_lst;
1118 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
1119 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
1120 }
1121
1122 if (scm_is_true (scm_apply (pred, args, SCM_EOL)))
1123 return SCM_I_MAKINUM (n);
1124 }
1125 }
1126
1127 not_found_check_lst:
1128 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
1129 return SCM_BOOL_F;
1130 }
1131 #undef FUNC_NAME
1132
1133
1134 /* This routine differs from the core list-copy in allowing improper lists.
1135 Maybe the core could allow them similarly. */
1136
1137 SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
1138 (SCM lst),
1139 "Return a copy of the given list @var{lst}.\n"
1140 "\n"
1141 "@var{lst} can be a proper or improper list. And if @var{lst}\n"
1142 "is not a pair then it's treated as the final tail of an\n"
1143 "improper list and simply returned.")
1144 #define FUNC_NAME s_scm_srfi1_list_copy
1145 {
1146 SCM newlst;
1147 SCM * fill_here;
1148 SCM from_here;
1149
1150 newlst = lst;
1151 fill_here = &newlst;
1152 from_here = lst;
1153
1154 while (scm_is_pair (from_here))
1155 {
1156 SCM c;
1157 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
1158 *fill_here = c;
1159 fill_here = SCM_CDRLOC (c);
1160 from_here = SCM_CDR (from_here);
1161 }
1162 return newlst;
1163 }
1164 #undef FUNC_NAME
1165
1166
1167 SCM_DEFINE (scm_srfi1_list_tabulate, "list-tabulate", 2, 0, 0,
1168 (SCM n, SCM proc),
1169 "Return an @var{n}-element list, where each list element is\n"
1170 "produced by applying the procedure @var{init-proc} to the\n"
1171 "corresponding list index. The order in which @var{init-proc}\n"
1172 "is applied to the indices is not specified.")
1173 #define FUNC_NAME s_scm_srfi1_list_tabulate
1174 {
1175 long i, nn;
1176 scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
1177 SCM ret = SCM_EOL;
1178
1179 nn = scm_to_signed_integer (n, 0, LONG_MAX);
1180 SCM_ASSERT (proc_tramp, proc, SCM_ARG2, FUNC_NAME);
1181
1182 for (i = nn-1; i >= 0; i--)
1183 ret = scm_cons (proc_tramp (proc, scm_from_long (i)), ret);
1184
1185 return ret;
1186 }
1187 #undef FUNC_NAME
1188
1189
1190 SCM_DEFINE (scm_srfi1_lset_adjoin, "lset-adjoin", 2, 0, 1,
1191 (SCM equal, SCM lst, SCM rest),
1192 "Add to @var{list} any of the given @var{elem}s not already in\n"
1193 "the list. @var{elem}s are @code{cons}ed onto the start of\n"
1194 "@var{list} (so the return shares a common tail with\n"
1195 "@var{list}), but the order they're added is unspecified.\n"
1196 "\n"
1197 "The given @var{=} procedure is used for comparing elements,\n"
1198 "called as @code{(@var{=} listelem elem)}, ie.@: the second\n"
1199 "argument is one of the given @var{elem} parameters.\n"
1200 "\n"
1201 "@example\n"
1202 "(lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)\n"
1203 "@end example")
1204 #define FUNC_NAME s_scm_srfi1_lset_adjoin
1205 {
1206 scm_t_trampoline_2 equal_tramp;
1207 SCM l, elem;
1208
1209 equal_tramp = scm_trampoline_2 (equal);
1210 SCM_ASSERT (equal_tramp, equal, SCM_ARG1, FUNC_NAME);
1211 SCM_VALIDATE_REST_ARGUMENT (rest);
1212
1213 /* It's not clear if duplicates among the `rest' elements are meant to be
1214 cast out. The spec says `=' is called as (= list-elem rest-elem),
1215 suggesting perhaps not, but the reference implementation shows the
1216 "list" at each stage as including those "rest" elements already added.
1217 The latter corresponds to what's described for lset-union, so that's
1218 what's done here. */
1219
1220 for ( ; scm_is_pair (rest); rest = SCM_CDR (rest))
1221 {
1222 elem = SCM_CAR (rest);
1223
1224 for (l = lst; scm_is_pair (l); l = SCM_CDR (l))
1225 if (scm_is_true (equal_tramp (equal, SCM_CAR (l), elem)))
1226 goto next_elem; /* elem already in lst, don't add */
1227
1228 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(l), lst, SCM_ARG2, FUNC_NAME, "list");
1229
1230 /* elem is not equal to anything already in lst, add it */
1231 lst = scm_cons (elem, lst);
1232
1233 next_elem:
1234 ;
1235 }
1236
1237 return lst;
1238 }
1239 #undef FUNC_NAME
1240
1241
1242 SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
1243 (SCM equal, SCM lst, SCM rest),
1244 "Return @var{lst} with any elements in the lists in @var{rest}\n"
1245 "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
1246 "just that list is returned.\n"
1247 "\n"
1248 "The given @var{equal} procedure is used for comparing elements,\n"
1249 "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
1250 "is from @var{lst} and the second from one of the subsequent\n"
1251 "lists. But exactly which calls are made and in what order is\n"
1252 "unspecified.\n"
1253 "\n"
1254 "@example\n"
1255 "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
1256 "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
1257 "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
1258 "@end example\n"
1259 "\n"
1260 "@code{lset-difference!} may modify @var{lst} to form its\n"
1261 "result.")
1262 #define FUNC_NAME s_scm_srfi1_lset_difference_x
1263 {
1264 scm_t_trampoline_2 equal_tramp = scm_trampoline_2 (equal);
1265 SCM ret, *pos, elem, r, b;
1266 int argnum;
1267
1268 SCM_ASSERT (equal_tramp, equal, SCM_ARG1, FUNC_NAME);
1269 SCM_VALIDATE_REST_ARGUMENT (rest);
1270
1271 ret = SCM_EOL;
1272 pos = &ret;
1273 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1274 {
1275 elem = SCM_CAR (lst);
1276
1277 for (r = rest, argnum = SCM_ARG3;
1278 scm_is_pair (r);
1279 r = SCM_CDR (r), argnum++)
1280 {
1281 for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
1282 if (scm_is_true (equal_tramp (equal, elem, SCM_CAR (b))))
1283 goto next_elem; /* equal to elem, so drop that elem */
1284
1285 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
1286 }
1287
1288 /* elem not equal to anything in later lists, so keep it */
1289 *pos = lst;
1290 pos = SCM_CDRLOC (lst);
1291
1292 next_elem:
1293 ;
1294 }
1295 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1296
1297 *pos = SCM_EOL;
1298 return ret;
1299 }
1300 #undef FUNC_NAME
1301
1302
1303 /* Typechecking for multi-argument MAP and FOR-EACH.
1304
1305 Verify that each element of the vector ARGV, except for the first,
1306 is a list and return minimum length. Attribute errors to WHO,
1307 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
1308 static inline int
1309 check_map_args (SCM argv,
1310 long len,
1311 SCM gf,
1312 SCM proc,
1313 SCM args,
1314 const char *who)
1315 {
1316 long i;
1317 SCM elt;
1318
1319 for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
1320 {
1321 long elt_len;
1322 elt = SCM_SIMPLE_VECTOR_REF (argv, i);
1323
1324 if (!(scm_is_null (elt) || scm_is_pair (elt)))
1325 goto check_map_error;
1326
1327 elt_len = srfi1_ilength (elt);
1328 if (elt_len < -1)
1329 goto check_map_error;
1330
1331 if (len < 0 || (elt_len >= 0 && elt_len < len))
1332 len = elt_len;
1333 }
1334
1335 if (len < 0)
1336 {
1337 /* i == 0 */
1338 elt = SCM_EOL;
1339 check_map_error:
1340 if (gf)
1341 scm_apply_generic (gf, scm_cons (proc, args));
1342 else
1343 scm_wrong_type_arg (who, i + 2, elt);
1344 }
1345
1346 scm_remember_upto_here_1 (argv);
1347 return len;
1348 }
1349
1350
1351 SCM_GPROC (s_srfi1_map, "map", 2, 0, 1, scm_srfi1_map, g_srfi1_map);
1352
1353 /* Note: Currently, scm_srfi1_map applies PROC to the argument list(s)
1354 sequentially, starting with the first element(s). This is used in
1355 the Scheme procedure `map-in-order', which guarantees sequential
1356 behaviour, is implemented using scm_map. If the behaviour changes,
1357 we need to update `map-in-order'.
1358 */
1359
1360 SCM
1361 scm_srfi1_map (SCM proc, SCM arg1, SCM args)
1362 #define FUNC_NAME s_srfi1_map
1363 {
1364 long i, len;
1365 SCM res = SCM_EOL;
1366 SCM *pres = &res;
1367
1368 len = srfi1_ilength (arg1);
1369 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
1370 g_srfi1_map,
1371 scm_cons2 (proc, arg1, args), SCM_ARG2, s_srfi1_map);
1372 SCM_VALIDATE_REST_ARGUMENT (args);
1373 if (scm_is_null (args))
1374 {
1375 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
1376 SCM_GASSERT2 (call, g_srfi1_map, proc, arg1, SCM_ARG1, s_srfi1_map);
1377 SCM_GASSERT2 (len >= 0, g_srfi1_map, proc, arg1, SCM_ARG2, s_srfi1_map);
1378 while (SCM_NIMP (arg1))
1379 {
1380 *pres = scm_list_1 (call (proc, SCM_CAR (arg1)));
1381 pres = SCM_CDRLOC (*pres);
1382 arg1 = SCM_CDR (arg1);
1383 }
1384 return res;
1385 }
1386 if (scm_is_null (SCM_CDR (args)))
1387 {
1388 SCM arg2 = SCM_CAR (args);
1389 int len2 = srfi1_ilength (arg2);
1390 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
1391 SCM_GASSERTn (call, g_srfi1_map,
1392 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_map);
1393 if (len < 0 || (len2 >= 0 && len2 < len))
1394 len = len2;
1395 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
1396 && len >= 0 && len2 >= -1,
1397 g_srfi1_map,
1398 scm_cons2 (proc, arg1, args),
1399 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
1400 s_srfi1_map);
1401 while (len > 0)
1402 {
1403 *pres = scm_list_1 (call (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
1404 pres = SCM_CDRLOC (*pres);
1405 arg1 = SCM_CDR (arg1);
1406 arg2 = SCM_CDR (arg2);
1407 --len;
1408 }
1409 return res;
1410 }
1411 args = scm_vector (arg1 = scm_cons (arg1, args));
1412 len = check_map_args (args, len, g_srfi1_map, proc, arg1, s_srfi1_map);
1413 while (len > 0)
1414 {
1415 arg1 = SCM_EOL;
1416 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
1417 {
1418 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
1419 arg1 = scm_cons (SCM_CAR (elt), arg1);
1420 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
1421 }
1422 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
1423 pres = SCM_CDRLOC (*pres);
1424 --len;
1425 }
1426 return res;
1427 }
1428 #undef FUNC_NAME
1429
1430 SCM_REGISTER_PROC (s_srfi1_map_in_order, "map-in-order", 2, 0, 1, scm_srfi1_map);
1431
1432 SCM_GPROC (s_srfi1_for_each, "for-each", 2, 0, 1, scm_srfi1_for_each, g_srfi1_for_each);
1433
1434 SCM
1435 scm_srfi1_for_each (SCM proc, SCM arg1, SCM args)
1436 #define FUNC_NAME s_srfi1_for_each
1437 {
1438 long i, len;
1439 len = srfi1_ilength (arg1);
1440 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
1441 g_srfi1_for_each, scm_cons2 (proc, arg1, args),
1442 SCM_ARG2, s_srfi1_for_each);
1443 SCM_VALIDATE_REST_ARGUMENT (args);
1444 if (scm_is_null (args))
1445 {
1446 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
1447 SCM_GASSERT2 (call, g_srfi1_for_each, proc, arg1,
1448 SCM_ARG1, s_srfi1_for_each);
1449 SCM_GASSERT2 (len >= 0, g_srfi1_for_each, proc, arg1,
1450 SCM_ARG2, s_srfi1_map);
1451 while (SCM_NIMP (arg1))
1452 {
1453 call (proc, SCM_CAR (arg1));
1454 arg1 = SCM_CDR (arg1);
1455 }
1456 return SCM_UNSPECIFIED;
1457 }
1458 if (scm_is_null (SCM_CDR (args)))
1459 {
1460 SCM arg2 = SCM_CAR (args);
1461 int len2 = srfi1_ilength (arg2);
1462 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
1463 SCM_GASSERTn (call, g_srfi1_for_each,
1464 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_for_each);
1465 if (len < 0 || (len2 >= 0 && len2 < len))
1466 len = len2;
1467 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
1468 && len >= 0 && len2 >= -1,
1469 g_srfi1_for_each,
1470 scm_cons2 (proc, arg1, args),
1471 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
1472 s_srfi1_for_each);
1473 while (len > 0)
1474 {
1475 call (proc, SCM_CAR (arg1), SCM_CAR (arg2));
1476 arg1 = SCM_CDR (arg1);
1477 arg2 = SCM_CDR (arg2);
1478 --len;
1479 }
1480 return SCM_UNSPECIFIED;
1481 }
1482 args = scm_vector (arg1 = scm_cons (arg1, args));
1483 len = check_map_args (args, len, g_srfi1_for_each, proc, arg1,
1484 s_srfi1_for_each);
1485 while (len > 0)
1486 {
1487 arg1 = SCM_EOL;
1488 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
1489 {
1490 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
1491 arg1 = scm_cons (SCM_CAR (elt), arg1);
1492 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
1493 }
1494 scm_apply (proc, arg1, SCM_EOL);
1495 --len;
1496 }
1497 return SCM_UNSPECIFIED;
1498 }
1499 #undef FUNC_NAME
1500
1501
1502 SCM_DEFINE (scm_srfi1_member, "member", 2, 1, 0,
1503 (SCM x, SCM lst, SCM pred),
1504 "Return the first sublist of @var{lst} whose @sc{car} is equal\n"
1505 "to @var{x}. If @var{x} does not appear in @var{lst}, return\n"
1506 "@code{#f}.\n"
1507 "\n"
1508 "Equality is determined by @code{equal?}, or by the equality\n"
1509 "predicate @var{=} if given. @var{=} is called @code{(= @var{x}\n"
1510 "elem)}, ie.@: with the given @var{x} first, so for example to\n"
1511 "find the first element greater than 5,\n"
1512 "\n"
1513 "@example\n"
1514 "(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)\n"
1515 "@end example\n"
1516 "\n"
1517 "This version of @code{member} extends the core @code{member} by\n"
1518 "accepting an equality predicate.")
1519 #define FUNC_NAME s_scm_srfi1_member
1520 {
1521 scm_t_trampoline_2 equal_p;
1522 SCM_VALIDATE_LIST (2, lst);
1523 if (SCM_UNBNDP (pred))
1524 equal_p = equal_trampoline;
1525 else
1526 {
1527 equal_p = scm_trampoline_2 (pred);
1528 SCM_ASSERT (equal_p, pred, 3, FUNC_NAME);
1529 }
1530 for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
1531 {
1532 if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
1533 return lst;
1534 }
1535 return SCM_BOOL_F;
1536 }
1537 #undef FUNC_NAME
1538
1539 SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
1540 (SCM key, SCM alist, SCM pred),
1541 "Behaves like @code{assq} but uses third argument @var{pred?}\n"
1542 "for key comparison. If @var{pred?} is not supplied,\n"
1543 "@code{equal?} is used. (Extended from R5RS.)\n")
1544 #define FUNC_NAME s_scm_srfi1_assoc
1545 {
1546 SCM ls = alist;
1547 scm_t_trampoline_2 equal_p;
1548 if (SCM_UNBNDP (pred))
1549 equal_p = equal_trampoline;
1550 else
1551 {
1552 equal_p = scm_trampoline_2 (pred);
1553 SCM_ASSERT (equal_p, pred, 3, FUNC_NAME);
1554 }
1555 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
1556 {
1557 SCM tmp = SCM_CAR (ls);
1558 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1559 "association list");
1560 if (scm_is_true (equal_p (pred, SCM_CAR (tmp), key)))
1561 return tmp;
1562 }
1563 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1564 "association list");
1565 return SCM_BOOL_F;
1566 }
1567 #undef FUNC_NAME
1568
1569
1570 SCM_DEFINE (scm_srfi1_ninth, "ninth", 1, 0, 0,
1571 (SCM lst),
1572 "Return the ninth element of @var{lst}.")
1573 #define FUNC_NAME s_scm_srfi1_ninth
1574 {
1575 return scm_list_ref (lst, scm_from_int (8));
1576 }
1577 #undef FUNC_NAME
1578
1579
1580 SCM_DEFINE (scm_srfi1_not_pair_p, "not-pair?", 1, 0, 0,
1581 (SCM obj),
1582 "Return @code{#t} is @var{obj} is not a pair, @code{#f}\n"
1583 "otherwise.\n"
1584 "\n"
1585 "This is shorthand notation @code{(not (pair? @var{obj}))} and\n"
1586 "is supposed to be used for end-of-list checking in contexts\n"
1587 "where dotted lists are allowed.")
1588 #define FUNC_NAME s_scm_srfi1_not_pair_p
1589 {
1590 return scm_from_bool (! scm_is_pair (obj));
1591 }
1592 #undef FUNC_NAME
1593
1594
1595 SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
1596 (SCM pred, SCM list),
1597 "Partition the elements of @var{list} with predicate @var{pred}.\n"
1598 "Return two values: the list of elements satifying @var{pred} and\n"
1599 "the list of elements @emph{not} satisfying @var{pred}. The order\n"
1600 "of the output lists follows the order of @var{list}. @var{list}\n"
1601 "is not mutated. One of the output lists may share memory with @var{list}.\n")
1602 #define FUNC_NAME s_scm_srfi1_partition
1603 {
1604 /* In this implementation, the output lists don't share memory with
1605 list, because it's probably not worth the effort. */
1606 scm_t_trampoline_1 call = scm_trampoline_1(pred);
1607 SCM kept = scm_cons(SCM_EOL, SCM_EOL);
1608 SCM kept_tail = kept;
1609 SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
1610 SCM dropped_tail = dropped;
1611
1612 SCM_ASSERT(call, pred, 2, FUNC_NAME);
1613
1614 for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
1615 SCM elt = SCM_CAR(list);
1616 SCM new_tail = scm_cons(SCM_CAR(list), SCM_EOL);
1617 if (scm_is_true (call (pred, elt))) {
1618 SCM_SETCDR(kept_tail, new_tail);
1619 kept_tail = new_tail;
1620 }
1621 else {
1622 SCM_SETCDR(dropped_tail, new_tail);
1623 dropped_tail = new_tail;
1624 }
1625 }
1626 /* re-use the initial conses for the values list */
1627 SCM_SETCAR(kept, SCM_CDR(kept));
1628 SCM_SETCDR(kept, dropped);
1629 SCM_SETCAR(dropped, SCM_CDR(dropped));
1630 SCM_SETCDR(dropped, SCM_EOL);
1631 return scm_values(kept);
1632 }
1633 #undef FUNC_NAME
1634
1635
1636 SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
1637 (SCM pred, SCM lst),
1638 "Split @var{lst} into those elements which do and don't satisfy\n"
1639 "the predicate @var{pred}.\n"
1640 "\n"
1641 "The return is two values (@pxref{Multiple Values}), the first\n"
1642 "being a list of all elements from @var{lst} which satisfy\n"
1643 "@var{pred}, the second a list of those which do not.\n"
1644 "\n"
1645 "The elements in the result lists are in the same order as in\n"
1646 "@var{lst} but the order in which the calls @code{(@var{pred}\n"
1647 "elem)} are made on the list elements is unspecified.\n"
1648 "\n"
1649 "@var{lst} may be modified to construct the return lists.")
1650 #define FUNC_NAME s_scm_srfi1_partition_x
1651 {
1652 SCM tlst, flst, *tp, *fp;
1653 scm_t_trampoline_1 pred_tramp;
1654
1655 pred_tramp = scm_trampoline_1 (pred);
1656 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1657
1658 /* tlst and flst are the lists of true and false elements. tp and fp are
1659 where to store to append to them, initially &tlst and &flst, then
1660 SCM_CDRLOC of the last pair in the respective lists. */
1661
1662 tlst = SCM_EOL;
1663 flst = SCM_EOL;
1664 tp = &tlst;
1665 fp = &flst;
1666
1667 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1668 {
1669 if (scm_is_true (pred_tramp (pred, SCM_CAR (lst))))
1670 {
1671 *tp = lst;
1672 tp = SCM_CDRLOC (lst);
1673 }
1674 else
1675 {
1676 *fp = lst;
1677 fp = SCM_CDRLOC (lst);
1678 }
1679 }
1680
1681 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1682
1683 /* terminate whichever didn't get the last element(s) */
1684 *tp = SCM_EOL;
1685 *fp = SCM_EOL;
1686
1687 return scm_values (scm_list_2 (tlst, flst));
1688 }
1689 #undef FUNC_NAME
1690
1691
1692 SCM_DEFINE (scm_srfi1_reduce, "reduce", 3, 0, 0,
1693 (SCM proc, SCM def, SCM lst),
1694 "@code{reduce} is a variant of @code{fold}, where the first call\n"
1695 "to @var{proc} is on two elements from @var{lst}, rather than\n"
1696 "one element and a given initial value.\n"
1697 "\n"
1698 "If @var{lst} is empty, @code{reduce} returns @var{def} (this is\n"
1699 "the only use for @var{def}). If @var{lst} has just one element\n"
1700 "then that's the return value. Otherwise @var{proc} is called\n"
1701 "on the elements of @var{lst}.\n"
1702 "\n"
1703 "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
1704 "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
1705 "second and subsequent elements of @var{lst}), and\n"
1706 "@var{previous} is the return from the previous call to\n"
1707 "@var{proc}. The first element of @var{lst} is the\n"
1708 "@var{previous} for the first call to @var{proc}.\n"
1709 "\n"
1710 "For example, the following adds a list of numbers, the calls\n"
1711 "made to @code{+} are shown. (Of course @code{+} accepts\n"
1712 "multiple arguments and can add a list directly, with\n"
1713 "@code{apply}.)\n"
1714 "\n"
1715 "@example\n"
1716 "(reduce + 0 '(5 6 7)) @result{} 18\n"
1717 "\n"
1718 "(+ 6 5) @result{} 11\n"
1719 "(+ 7 11) @result{} 18\n"
1720 "@end example\n"
1721 "\n"
1722 "@code{reduce} can be used instead of @code{fold} where the\n"
1723 "@var{init} value is an ``identity'', meaning a value which\n"
1724 "under @var{proc} doesn't change the result, in this case 0 is\n"
1725 "an identity since @code{(+ 5 0)} is just 5. @code{reduce}\n"
1726 "avoids that unnecessary call.")
1727 #define FUNC_NAME s_scm_srfi1_reduce
1728 {
1729 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
1730 SCM ret;
1731
1732 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
1733
1734 ret = def; /* if lst is empty */
1735 if (scm_is_pair (lst))
1736 {
1737 ret = SCM_CAR (lst); /* if lst has one element */
1738
1739 for (lst = SCM_CDR (lst); scm_is_pair (lst); lst = SCM_CDR (lst))
1740 ret = proc_tramp (proc, SCM_CAR (lst), ret);
1741 }
1742
1743 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG3, FUNC_NAME, "list");
1744 return ret;
1745 }
1746 #undef FUNC_NAME
1747
1748
1749 SCM_DEFINE (scm_srfi1_reduce_right, "reduce-right", 3, 0, 0,
1750 (SCM proc, SCM def, SCM lst),
1751 "@code{reduce-right} is a variant of @code{fold-right}, where\n"
1752 "the first call to @var{proc} is on two elements from @var{lst},\n"
1753 "rather than one element and a given initial value.\n"
1754 "\n"
1755 "If @var{lst} is empty, @code{reduce-right} returns @var{def}\n"
1756 "(this is the only use for @var{def}). If @var{lst} has just\n"
1757 "one element then that's the return value. Otherwise @var{proc}\n"
1758 "is called on the elements of @var{lst}.\n"
1759 "\n"
1760 "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
1761 "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
1762 "second last and then working back to the first element of\n"
1763 "@var{lst}), and @var{previous} is the return from the previous\n"
1764 "call to @var{proc}. The last element of @var{lst} is the\n"
1765 "@var{previous} for the first call to @var{proc}.\n"
1766 "\n"
1767 "For example, the following adds a list of numbers, the calls\n"
1768 "made to @code{+} are shown. (Of course @code{+} accepts\n"
1769 "multiple arguments and can add a list directly, with\n"
1770 "@code{apply}.)\n"
1771 "\n"
1772 "@example\n"
1773 "(reduce-right + 0 '(5 6 7)) @result{} 18\n"
1774 "\n"
1775 "(+ 6 7) @result{} 13\n"
1776 "(+ 5 13) @result{} 18\n"
1777 "@end example\n"
1778 "\n"
1779 "@code{reduce-right} can be used instead of @code{fold-right}\n"
1780 "where the @var{init} value is an ``identity'', meaning a value\n"
1781 "which under @var{proc} doesn't change the result, in this case\n"
1782 "0 is an identity since @code{(+ 7 0)} is just 5.\n"
1783 "@code{reduce-right} avoids that unnecessary call.\n"
1784 "\n"
1785 "@code{reduce} should be preferred over @code{reduce-right} if\n"
1786 "the order of processing doesn't matter, or can be arranged\n"
1787 "either way, since @code{reduce} is a little more efficient.")
1788 #define FUNC_NAME s_scm_srfi1_reduce_right
1789 {
1790 /* To work backwards across a list requires either repeatedly traversing
1791 to get each previous element, or using some memory for a reversed or
1792 random-access form. Repeated traversal might not be too terrible, but
1793 is of course quadratic complexity and hence to be avoided in case LST
1794 is long. A vector is preferred over a reversed list since it's more
1795 compact and is less work for the gc to collect. */
1796
1797 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
1798 SCM ret, vec;
1799 long len, i;
1800
1801 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
1802
1803 if (SCM_NULL_OR_NIL_P (lst))
1804 return def;
1805
1806 vec = scm_vector (lst);
1807 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
1808
1809 ret = SCM_SIMPLE_VECTOR_REF (vec, len-1);
1810 for (i = len-2; i >= 0; i--)
1811 ret = proc_tramp (proc, SCM_SIMPLE_VECTOR_REF (vec, i), ret);
1812
1813 return ret;
1814 }
1815 #undef FUNC_NAME
1816
1817
1818 SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
1819 (SCM pred, SCM list),
1820 "Return a list containing all elements from @var{lst} which do\n"
1821 "not satisfy the predicate @var{pred}. The elements in the\n"
1822 "result list have the same order as in @var{lst}. The order in\n"
1823 "which @var{pred} is applied to the list elements is not\n"
1824 "specified.")
1825 #define FUNC_NAME s_scm_srfi1_remove
1826 {
1827 scm_t_trampoline_1 call = scm_trampoline_1 (pred);
1828 SCM walk;
1829 SCM *prev;
1830 SCM res = SCM_EOL;
1831 SCM_ASSERT (call, pred, 1, FUNC_NAME);
1832 SCM_VALIDATE_LIST (2, list);
1833
1834 for (prev = &res, walk = list;
1835 scm_is_pair (walk);
1836 walk = SCM_CDR (walk))
1837 {
1838 if (scm_is_false (call (pred, SCM_CAR (walk))))
1839 {
1840 *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
1841 prev = SCM_CDRLOC (*prev);
1842 }
1843 }
1844
1845 return res;
1846 }
1847 #undef FUNC_NAME
1848
1849
1850 SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
1851 (SCM pred, SCM list),
1852 "Return a list containing all elements from @var{list} which do\n"
1853 "not satisfy the predicate @var{pred}. The elements in the\n"
1854 "result list have the same order as in @var{list}. The order in\n"
1855 "which @var{pred} is applied to the list elements is not\n"
1856 "specified. @var{list} may be modified to build the return\n"
1857 "list.")
1858 #define FUNC_NAME s_scm_srfi1_remove_x
1859 {
1860 scm_t_trampoline_1 call = scm_trampoline_1 (pred);
1861 SCM walk;
1862 SCM *prev;
1863 SCM_ASSERT (call, pred, 1, FUNC_NAME);
1864 SCM_VALIDATE_LIST (2, list);
1865
1866 for (prev = &list, walk = list;
1867 scm_is_pair (walk);
1868 walk = SCM_CDR (walk))
1869 {
1870 if (scm_is_false (call (pred, SCM_CAR (walk))))
1871 prev = SCM_CDRLOC (walk);
1872 else
1873 *prev = SCM_CDR (walk);
1874 }
1875
1876 return list;
1877 }
1878 #undef FUNC_NAME
1879
1880
1881 SCM_DEFINE (scm_srfi1_seventh, "seventh", 1, 0, 0,
1882 (SCM lst),
1883 "Return the seventh element of @var{lst}.")
1884 #define FUNC_NAME s_scm_srfi1_seventh
1885 {
1886 return scm_list_ref (lst, scm_from_int (6));
1887 }
1888 #undef FUNC_NAME
1889
1890
1891 SCM_DEFINE (scm_srfi1_sixth, "sixth", 1, 0, 0,
1892 (SCM lst),
1893 "Return the sixth element of @var{lst}.")
1894 #define FUNC_NAME s_scm_srfi1_sixth
1895 {
1896 return scm_list_ref (lst, scm_from_int (5));
1897 }
1898 #undef FUNC_NAME
1899
1900
1901 SCM_DEFINE (scm_srfi1_span, "span", 2, 0, 0,
1902 (SCM pred, SCM lst),
1903 "Return two values, the longest initial prefix of @var{lst}\n"
1904 "whose elements all satisfy the predicate @var{pred}, and the\n"
1905 "remainder of @var{lst}.")
1906 #define FUNC_NAME s_scm_srfi1_span
1907 {
1908 scm_t_trampoline_1 pred_tramp;
1909 SCM ret, *p;
1910
1911 pred_tramp = scm_trampoline_1 (pred);
1912 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1913
1914 ret = SCM_EOL;
1915 p = &ret;
1916 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1917 {
1918 SCM elem = SCM_CAR (lst);
1919 if (scm_is_false (pred_tramp (pred, elem)))
1920 goto done;
1921
1922 /* want this elem, tack it onto the end of ret */
1923 *p = scm_cons (elem, SCM_EOL);
1924 p = SCM_CDRLOC (*p);
1925 }
1926 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1927
1928 done:
1929 return scm_values (scm_list_2 (ret, lst));
1930 }
1931 #undef FUNC_NAME
1932
1933
1934 SCM_DEFINE (scm_srfi1_span_x, "span!", 2, 0, 0,
1935 (SCM pred, SCM lst),
1936 "Return two values, the longest initial prefix of @var{lst}\n"
1937 "whose elements all satisfy the predicate @var{pred}, and the\n"
1938 "remainder of @var{lst}. @var{lst} may be modified to form the\n"
1939 "return.")
1940 #define FUNC_NAME s_scm_srfi1_span_x
1941 {
1942 SCM upto, *p;
1943 scm_t_trampoline_1 pred_tramp;
1944
1945 pred_tramp = scm_trampoline_1 (pred);
1946 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1947
1948 p = &lst;
1949 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
1950 {
1951 if (scm_is_false (pred_tramp (pred, SCM_CAR (upto))))
1952 goto done;
1953
1954 /* want this element */
1955 p = SCM_CDRLOC (upto);
1956 }
1957 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
1958
1959 done:
1960 *p = SCM_EOL;
1961 return scm_values (scm_list_2 (lst, upto));
1962 }
1963 #undef FUNC_NAME
1964
1965
1966 SCM_DEFINE (scm_srfi1_split_at, "split-at", 2, 0, 0,
1967 (SCM lst, SCM n),
1968 "Return two values (multiple values), being a list of the\n"
1969 "elements before index @var{n} in @var{lst}, and a list of those\n"
1970 "after.")
1971 #define FUNC_NAME s_scm_srfi1_split_at
1972 {
1973 size_t nn;
1974 /* pre is a list of elements before the i split point, loc is the CDRLOC
1975 of the last cell, ie. where to store to append to it */
1976 SCM pre = SCM_EOL;
1977 SCM *loc = &pre;
1978
1979 for (nn = scm_to_size_t (n); nn != 0; nn--)
1980 {
1981 SCM_VALIDATE_CONS (SCM_ARG1, lst);
1982
1983 *loc = scm_cons (SCM_CAR (lst), SCM_EOL);
1984 loc = SCM_CDRLOC (*loc);
1985 lst = SCM_CDR(lst);
1986 }
1987 return scm_values (scm_list_2 (pre, lst));
1988 }
1989 #undef FUNC_NAME
1990
1991
1992 SCM_DEFINE (scm_srfi1_split_at_x, "split-at!", 2, 0, 0,
1993 (SCM lst, SCM n),
1994 "Return two values (multiple values), being a list of the\n"
1995 "elements before index @var{n} in @var{lst}, and a list of those\n"
1996 "after. @var{lst} is modified to form those values.")
1997 #define FUNC_NAME s_scm_srfi1_split_at
1998 {
1999 size_t nn;
2000 SCM upto = lst;
2001 SCM *loc = &lst;
2002
2003 for (nn = scm_to_size_t (n); nn != 0; nn--)
2004 {
2005 SCM_VALIDATE_CONS (SCM_ARG1, upto);
2006
2007 loc = SCM_CDRLOC (upto);
2008 upto = SCM_CDR (upto);
2009 }
2010
2011 *loc = SCM_EOL;
2012 return scm_values (scm_list_2 (lst, upto));
2013 }
2014 #undef FUNC_NAME
2015
2016
2017 SCM_DEFINE (scm_srfi1_take_x, "take!", 2, 0, 0,
2018 (SCM lst, SCM n),
2019 "Return a list containing the first @var{n} elements of\n"
2020 "@var{lst}.")
2021 #define FUNC_NAME s_scm_srfi1_take_x
2022 {
2023 long nn;
2024 SCM pos;
2025
2026 nn = scm_to_signed_integer (n, 0, LONG_MAX);
2027 if (nn == 0)
2028 return SCM_EOL;
2029
2030 pos = scm_list_tail (lst, scm_from_long (nn - 1));
2031
2032 /* Must have at least one cell left, mustn't have reached the end of an
2033 n-1 element list. SCM_VALIDATE_CONS here gives the same error as
2034 scm_list_tail does on say an n-2 element list, though perhaps a range
2035 error would make more sense (for both). */
2036 SCM_VALIDATE_CONS (SCM_ARG1, pos);
2037
2038 SCM_SETCDR (pos, SCM_EOL);
2039 return lst;
2040 }
2041 #undef FUNC_NAME
2042
2043
2044 SCM_DEFINE (scm_srfi1_take_right, "take-right", 2, 0, 0,
2045 (SCM lst, SCM n),
2046 "Return the a list containing the @var{n} last elements of\n"
2047 "@var{lst}.")
2048 #define FUNC_NAME s_scm_srfi1_take_right
2049 {
2050 SCM tail = scm_list_tail (lst, n);
2051 while (scm_is_pair (tail))
2052 {
2053 lst = SCM_CDR (lst);
2054 tail = SCM_CDR (tail);
2055 }
2056 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
2057 return lst;
2058 }
2059 #undef FUNC_NAME
2060
2061
2062 SCM_DEFINE (scm_srfi1_take_while, "take-while", 2, 0, 0,
2063 (SCM pred, SCM lst),
2064 "Return a new list which is the longest initial prefix of\n"
2065 "@var{lst} whose elements all satisfy the predicate @var{pred}.")
2066 #define FUNC_NAME s_scm_srfi1_take_while
2067 {
2068 scm_t_trampoline_1 pred_tramp;
2069 SCM ret, *p;
2070
2071 pred_tramp = scm_trampoline_1 (pred);
2072 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
2073
2074 ret = SCM_EOL;
2075 p = &ret;
2076 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
2077 {
2078 SCM elem = SCM_CAR (lst);
2079 if (scm_is_false (pred_tramp (pred, elem)))
2080 goto done;
2081
2082 /* want this elem, tack it onto the end of ret */
2083 *p = scm_cons (elem, SCM_EOL);
2084 p = SCM_CDRLOC (*p);
2085 }
2086 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
2087
2088 done:
2089 return ret;
2090 }
2091 #undef FUNC_NAME
2092
2093
2094 SCM_DEFINE (scm_srfi1_take_while_x, "take-while!", 2, 0, 0,
2095 (SCM pred, SCM lst),
2096 "Return the longest initial prefix of @var{lst} whose elements\n"
2097 "all satisfy the predicate @var{pred}. @var{lst} may be\n"
2098 "modified to form the return.")
2099 #define FUNC_NAME s_scm_srfi1_take_while_x
2100 {
2101 SCM upto, *p;
2102 scm_t_trampoline_1 pred_tramp;
2103
2104 pred_tramp = scm_trampoline_1 (pred);
2105 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
2106
2107 p = &lst;
2108 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
2109 {
2110 if (scm_is_false (pred_tramp (pred, SCM_CAR (upto))))
2111 goto done;
2112
2113 /* want this element */
2114 p = SCM_CDRLOC (upto);
2115 }
2116 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
2117
2118 done:
2119 *p = SCM_EOL;
2120 return lst;
2121 }
2122 #undef FUNC_NAME
2123
2124
2125 SCM_DEFINE (scm_srfi1_tenth, "tenth", 1, 0, 0,
2126 (SCM lst),
2127 "Return the tenth element of @var{lst}.")
2128 #define FUNC_NAME s_scm_srfi1_tenth
2129 {
2130 return scm_list_ref (lst, scm_from_int (9));
2131 }
2132 #undef FUNC_NAME
2133
2134
2135 SCM_DEFINE (scm_srfi1_xcons, "xcons", 2, 0, 0,
2136 (SCM d, SCM a),
2137 "Like @code{cons}, but with interchanged arguments. Useful\n"
2138 "mostly when passed to higher-order procedures.")
2139 #define FUNC_NAME s_scm_srfi1_xcons
2140 {
2141 return scm_cons (a, d);
2142 }
2143 #undef FUNC_NAME
2144
2145
2146 void
2147 scm_init_srfi_1 (void)
2148 {
2149 SCM the_root_module = scm_lookup_closure_module (SCM_BOOL_F);
2150 #ifndef SCM_MAGIC_SNARFER
2151 #include "srfi/srfi-1.x"
2152 #endif
2153 scm_c_extend_primitive_generic
2154 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "map")),
2155 SCM_VARIABLE_REF (scm_c_lookup ("map")));
2156 scm_c_extend_primitive_generic
2157 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "for-each")),
2158 SCM_VARIABLE_REF (scm_c_lookup ("for-each")));
2159 }
2160
2161 /* End of srfi-1.c. */