1 /* srfi-1.c --- SRFI-1 procedures for Guile
3 * Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2005, 2006
4 * Free Software Foundation, Inc.
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.
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.
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
22 #include <libguile/lang.h>
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.
30 * Please feel free to contribute any new replacements!
34 srfi1_ilength (SCM sx
)
41 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
42 if (!scm_is_pair (hare
)) return -2;
45 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
46 if (!scm_is_pair (hare
)) return -2;
49 /* For every two steps the hare takes, the tortoise takes one. */
50 tortoise
= SCM_CDR(tortoise
);
52 while (! scm_is_eq (hare
, tortoise
));
54 /* If the tortoise ever catches the hare, then the list must contain
60 equal_trampoline (SCM proc
, SCM arg1
, SCM arg2
)
62 return scm_equal_p (arg1
, arg2
);
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.
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
76 list_copy_part (SCM lst
, int count
, SCM
*dst
)
77 #define FUNC_NAME "list_copy_part"
80 for ( ; count
> 0; count
--)
82 SCM_VALIDATE_CONS (SCM_ARGn
, lst
);
83 c
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
93 SCM_DEFINE (scm_srfi1_alist_copy
, "alist-copy", 1, 0, 0,
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
101 /* ret is the list to return. p is where to append to it, initially &ret
102 then SCM_CDRLOC of the last pair. */
106 for ( ; scm_is_pair (alist
); alist
= SCM_CDR (alist
))
108 elem
= SCM_CAR (alist
);
110 /* each element of alist must be a pair */
111 SCM_ASSERT_TYPE (scm_is_pair (elem
), alist
, SCM_ARG1
, FUNC_NAME
,
114 c
= scm_cons (scm_cons (SCM_CAR (elem
), SCM_CDR (elem
)), SCM_EOL
);
119 /* alist must be a proper list */
120 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (alist
), alist
, SCM_ARG1
, FUNC_NAME
,
127 SCM_DEFINE (scm_srfi1_break
, "break", 2, 0, 0,
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"
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
139 scm_t_trampoline_1 pred_tramp
;
142 pred_tramp
= scm_trampoline_1 (pred
);
143 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
147 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
149 SCM elem
= SCM_CAR (lst
);
150 if (scm_is_true (pred_tramp (pred
, elem
)))
153 /* want this elem, tack it onto the end of ret */
154 *p
= scm_cons (elem
, SCM_EOL
);
157 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
160 return scm_values (scm_list_2 (ret
, lst
));
165 SCM_DEFINE (scm_srfi1_break_x
, "break!", 2, 0, 0,
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"
171 #define FUNC_NAME s_scm_srfi1_break_x
174 scm_t_trampoline_1 pred_tramp
;
176 pred_tramp
= scm_trampoline_1 (pred
);
177 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
180 for (upto
= lst
; scm_is_pair (upto
); upto
= SCM_CDR (upto
))
182 if (scm_is_true (pred_tramp (pred
, SCM_CAR (upto
))))
185 /* want this element */
186 p
= SCM_CDRLOC (upto
);
188 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
192 return scm_values (scm_list_2 (lst
, upto
));
197 SCM_DEFINE (scm_srfi1_car_plus_cdr
, "car+cdr", 1, 0, 0,
199 "Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.")
200 #define FUNC_NAME s_scm_srfi1_car_plus_cdr
202 SCM_VALIDATE_CONS (SCM_ARG1
, pair
);
203 return scm_values (scm_list_2 (SCM_CAR (pair
), SCM_CDR (pair
)));
208 SCM_DEFINE (scm_srfi1_concatenate
, "concatenate", 1, 0, 0,
210 "Construct a list by appending all lists in @var{lstlst}.\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"
217 #define FUNC_NAME s_scm_srfi1_concatenate
219 SCM_VALIDATE_LIST (SCM_ARG1
, lstlst
);
220 return scm_append (lstlst
);
225 SCM_DEFINE (scm_srfi1_concatenate_x
, "concatenate!", 1, 0, 0,
227 "Construct a list by appending all lists in @var{lstlst}. Those\n"
228 "lists may be modified to produce the result.\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"
235 #define FUNC_NAME s_scm_srfi1_concatenate
237 SCM_VALIDATE_LIST (SCM_ARG1
, lstlst
);
238 return scm_append_x (lstlst
);
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"
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"
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
261 SCM_VALIDATE_REST_ARGUMENT (rest
);
265 if (scm_is_null (rest
))
268 scm_t_trampoline_1 pred_tramp
;
269 pred_tramp
= scm_trampoline_1 (pred
);
270 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
272 for ( ; scm_is_pair (list1
); list1
= SCM_CDR (list1
))
273 count
+= scm_is_true (pred_tramp (pred
, SCM_CAR (list1
)));
275 /* check below that list1 is a proper list, and done */
280 else if (scm_is_pair (rest
) && scm_is_null (SCM_CDR (rest
)))
283 scm_t_trampoline_2 pred_tramp
;
286 pred_tramp
= scm_trampoline_2 (pred
);
287 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
289 list2
= SCM_CAR (rest
);
292 if (! scm_is_pair (list1
))
294 if (! scm_is_pair (list2
))
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
);
308 /* three or more lists */
312 /* vec is the list arguments */
313 vec
= scm_vector (scm_cons (list1
, rest
));
314 len
= SCM_SIMPLE_VECTOR_LENGTH (vec
);
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
);
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;
326 i
++, a
= SCM_CDR (a
), argnum
++)
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 */
335 count
+= scm_is_true (scm_apply (pred
, args
, SCM_EOL
));
340 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, argnum
, FUNC_NAME
, "list");
341 return scm_from_long (count
);
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"
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"
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"
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
365 scm_t_trampoline_2 equal_p
;
366 SCM ret
, *p
, keeplst
;
369 if (SCM_UNBNDP (pred
))
370 return scm_delete (x
, lst
);
372 equal_p
= scm_trampoline_2 (pred
);
373 SCM_ASSERT (equal_p
, pred
, SCM_ARG3
, FUNC_NAME
);
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.
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.) */
389 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
391 if (scm_is_true (equal_p (pred
, x
, SCM_CAR (lst
))))
393 /* delete this element, so copy those at keeplst */
394 p
= list_copy_part (keeplst
, count
, p
);
395 keeplst
= SCM_CDR (lst
);
400 /* keep this element */
405 /* final retained elements */
408 /* demand that lst was a proper list */
409 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
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"
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"
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"
431 "@var{lst} may be modified to construct the returned list.")
432 #define FUNC_NAME s_scm_srfi1_delete_x
434 scm_t_trampoline_2 equal_p
;
438 if (SCM_UNBNDP (pred
))
439 return scm_delete_x (x
, lst
);
441 equal_p
= scm_trampoline_2 (pred
);
442 SCM_ASSERT (equal_p
, pred
, SCM_ARG3
, FUNC_NAME
);
444 for (prev
= &lst
, walk
= lst
;
446 walk
= SCM_CDR (walk
))
448 if (scm_is_true (equal_p (pred
, x
, SCM_CAR (walk
))))
449 *prev
= SCM_CDR (walk
);
451 prev
= SCM_CDRLOC (walk
);
454 /* demand the input was a proper list */
455 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk
), walk
, SCM_ARG2
, FUNC_NAME
,"list");
461 SCM_DEFINE (scm_srfi1_delete_duplicates
, "delete-duplicates", 1, 1, 0,
463 "Return a list containing the elements of @var{lst} but without\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"
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"
477 "@var{lst} is not modified, but the return might share a common\n"
478 "tail with @var{lst}.\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
486 scm_t_trampoline_2 equal_p
;
487 SCM ret
, *p
, keeplst
, item
, l
;
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
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
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.
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
508 /* skip to end if an empty list (or something invalid) */
511 if (SCM_UNBNDP (pred
))
512 equal_p
= equal_trampoline
;
515 equal_p
= scm_trampoline_2 (pred
);
516 SCM_ASSERT (equal_p
, pred
, SCM_ARG2
, FUNC_NAME
);
523 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
525 item
= SCM_CAR (lst
);
527 /* look for item in "ret" list */
528 for (l
= ret
; scm_is_pair (l
); l
= SCM_CDR (l
))
530 if (scm_is_true (equal_p (pred
, SCM_CAR (l
), item
)))
532 /* "item" is a duplicate, so copy keeplst onto ret */
534 p
= list_copy_part (keeplst
, count
, p
);
536 keeplst
= SCM_CDR (lst
); /* elem after the one deleted */
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
)))
550 /* keep this element */
556 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG1
, FUNC_NAME
, "list");
558 /* share tail of keeplst items */
566 SCM_DEFINE (scm_srfi1_delete_duplicates_x
, "delete-duplicates!", 1, 1, 0,
568 "Return a list containing the elements of @var{lst} but without\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"
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"
582 "@var{lst} may be modified to construct the returned list.\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
590 scm_t_trampoline_2 equal_p
;
591 SCM ret
, endret
, item
, l
;
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. */
597 /* skip to end if an empty list (or something invalid) */
599 if (scm_is_pair (lst
))
601 if (SCM_UNBNDP (pred
))
602 equal_p
= equal_trampoline
;
605 equal_p
= scm_trampoline_2 (pred
);
606 SCM_ASSERT (equal_p
, pred
, SCM_ARG2
, FUNC_NAME
);
611 /* loop over lst elements starting from second */
615 if (! scm_is_pair (lst
))
617 item
= SCM_CAR (lst
);
619 /* is item equal to any element from ret to endret (inclusive)? */
623 if (scm_is_true (equal_p (pred
, SCM_CAR (l
), item
)))
624 break; /* equal, forget this element */
626 if (scm_is_eq (l
, endret
))
628 /* not equal to any, so append this pair */
629 SCM_SETCDR (endret
, lst
);
637 /* terminate, in case last element was deleted */
638 SCM_SETCDR (endret
, SCM_EOL
);
641 /* demand that lst was a proper list */
642 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG1
, FUNC_NAME
, "list");
649 SCM_DEFINE (scm_srfi1_drop_right
, "drop-right", 2, 0, 0,
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
655 SCM tail
= scm_list_tail (lst
, n
);
658 while (scm_is_pair (tail
))
660 *rend
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
661 rend
= SCM_CDRLOC (*rend
);
664 tail
= SCM_CDR (tail
);
666 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail
), tail
, SCM_ARG1
, FUNC_NAME
, "list");
672 SCM_DEFINE (scm_srfi1_drop_right_x
, "drop-right!", 2, 0, 0,
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
680 if (scm_is_eq (n
, SCM_INUM0
))
683 tail
= scm_list_tail (lst
, n
);
686 /* p and tail work along the list, p being the cdrloc of the cell n steps
688 for ( ; scm_is_pair (tail
); tail
= SCM_CDR (tail
))
691 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail
), tail
, SCM_ARG1
, FUNC_NAME
, "list");
699 SCM_DEFINE (scm_srfi1_drop_while
, "drop-while", 2, 0, 0,
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
705 scm_t_trampoline_1 pred_tramp
= scm_trampoline_1 (pred
);
706 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
708 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
709 if (scm_is_false (pred_tramp (pred
, SCM_CAR (lst
))))
712 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
719 SCM_DEFINE (scm_srfi1_eighth
, "eighth", 1, 0, 0,
721 "Return the eighth element of @var{lst}.")
722 #define FUNC_NAME s_scm_srfi1_eighth
724 return scm_list_ref (lst
, SCM_I_MAKINUM (7));
729 SCM_DEFINE (scm_srfi1_fifth
, "fifth", 1, 0, 0,
731 "Return the fifth element of @var{lst}.")
732 #define FUNC_NAME s_scm_srfi1_fifth
734 return scm_list_ref (lst
, SCM_I_MAKINUM (4));
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
746 SCM ret
, *loc
, elem
, newcell
, lst
;
749 SCM_VALIDATE_REST_ARGUMENT (rest
);
754 if (scm_is_null (rest
))
757 scm_t_trampoline_1 proc_tramp
= scm_trampoline_1 (proc
);
758 SCM_ASSERT (proc_tramp
, proc
, SCM_ARG1
, FUNC_NAME
);
760 for ( ; scm_is_pair (list1
); list1
= SCM_CDR (list1
))
762 elem
= proc_tramp (proc
, SCM_CAR (list1
));
763 if (scm_is_true (elem
))
765 newcell
= scm_cons (elem
, SCM_EOL
);
767 loc
= SCM_CDRLOC (newcell
);
771 /* check below that list1 is a proper list, and done */
776 else if (scm_is_null (SCM_CDR (rest
)))
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
);
785 if (! scm_is_pair (list1
))
787 if (! scm_is_pair (list2
))
791 goto check_lst_and_done
;
793 elem
= proc_tramp (proc
, SCM_CAR (list1
), SCM_CAR (list2
));
794 if (scm_is_true (elem
))
796 newcell
= scm_cons (elem
, SCM_EOL
);
798 loc
= SCM_CDRLOC (newcell
);
800 list1
= SCM_CDR (list1
);
801 list2
= SCM_CDR (list2
);
806 /* three or more lists */
810 /* vec is the list arguments */
811 vec
= scm_vector (scm_cons (list1
, rest
));
812 len
= SCM_SIMPLE_VECTOR_LENGTH (vec
);
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
);
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;
824 i
++, a
= SCM_CDR (a
), argnum
++)
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 */
833 elem
= scm_apply (proc
, args
, SCM_EOL
);
834 if (scm_is_true (elem
))
836 newcell
= scm_cons (elem
, SCM_EOL
);
838 loc
= SCM_CDRLOC (newcell
);
844 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, argnum
, FUNC_NAME
, "list");
850 SCM_DEFINE (scm_srfi1_find
, "find", 2, 0, 0,
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"
855 #define FUNC_NAME s_scm_srfi1_find
857 scm_t_trampoline_1 pred_tramp
= scm_trampoline_1 (pred
);
858 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
860 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
862 SCM elem
= SCM_CAR (lst
);
863 if (scm_is_true (pred_tramp (pred
, elem
)))
866 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
873 SCM_DEFINE (scm_srfi1_find_tail
, "find-tail", 2, 0, 0,
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"
878 #define FUNC_NAME s_scm_srfi1_find_tail
880 scm_t_trampoline_1 pred_tramp
= scm_trampoline_1 (pred
);
881 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
883 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
884 if (scm_is_true (pred_tramp (pred
, SCM_CAR (lst
))))
886 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
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"
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"
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"
909 "(fold cons '() '(1 2 3))\n"
914 "@result{} (3 2 1)\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"
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"
929 "(define (delete-adjacent-duplicates lst)\n"
930 " (fold-right (lambda (elem ret)\n"
931 " (if (equal? elem (first ret))\n"
933 " (cons elem ret)))\n"
934 " (list (last lst))\n"
936 "(delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))\n"
937 "@result{} (1 2 3 4 5)\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"
945 #define FUNC_NAME s_scm_srfi1_fold
949 SCM_VALIDATE_REST_ARGUMENT (rest
);
951 if (scm_is_null (rest
))
954 scm_t_trampoline_2 proc_tramp
= scm_trampoline_2 (proc
);
955 SCM_ASSERT (proc_tramp
, proc
, SCM_ARG1
, FUNC_NAME
);
957 for ( ; scm_is_pair (list1
); list1
= SCM_CDR (list1
))
958 init
= proc_tramp (proc
, SCM_CAR (list1
), init
);
960 /* check below that list1 is a proper list, and done */
966 /* two or more lists */
970 /* vec is the list arguments */
971 vec
= scm_vector (scm_cons (list1
, rest
));
972 len
= SCM_SIMPLE_VECTOR_LENGTH (vec
);
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
);
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;
984 i
++, a
= SCM_CDR (a
), argnum
++)
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 */
992 SCM_SETCAR (a
, init
);
994 init
= scm_apply (proc
, args
, SCM_EOL
);
999 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, argnum
, FUNC_NAME
, "list");
1005 SCM_DEFINE (scm_srfi1_last
, "last", 1, 0, 0,
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
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
);
1019 SCM_DEFINE (scm_srfi1_length_plus
, "length+", 1, 0, 0,
1021 "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
1023 #define FUNC_NAME s_scm_srfi1_length_plus
1025 long len
= scm_ilength (lst
);
1026 return (len
>= 0 ? SCM_I_MAKINUM (len
) : SCM_BOOL_F
);
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"
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"
1043 "(list-index odd? '(2 4 6 9)) @result{} 3\n"
1044 "(list-index = '(1 2 3) '(3 1 2)) @result{} #f\n"
1046 #define FUNC_NAME s_scm_srfi1_list_index
1051 SCM_VALIDATE_REST_ARGUMENT (rest
);
1053 if (scm_is_null (rest
))
1056 scm_t_trampoline_1 pred_tramp
= scm_trampoline_1 (pred
);
1057 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
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
);
1063 /* not found, check below that list1 is a proper list */
1068 else if (scm_is_pair (rest
) && scm_is_null (SCM_CDR (rest
)))
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
);
1077 if (! scm_is_pair (list1
))
1079 if (! scm_is_pair (list2
))
1085 if (scm_is_true (pred_tramp (pred
,
1086 SCM_CAR (list1
), SCM_CAR (list2
))))
1087 return SCM_I_MAKINUM (n
);
1089 list1
= SCM_CDR (list1
);
1090 list2
= SCM_CDR (list2
);
1095 /* three or more lists */
1099 /* vec is the list arguments */
1100 vec
= scm_vector (scm_cons (list1
, rest
));
1101 len
= SCM_SIMPLE_VECTOR_LENGTH (vec
);
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
);
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;
1113 i
++, a
= SCM_CDR (a
), argnum
++)
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 */
1122 if (scm_is_true (scm_apply (pred
, args
, SCM_EOL
)))
1123 return SCM_I_MAKINUM (n
);
1127 not_found_check_lst
:
1128 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, argnum
, FUNC_NAME
, "list");
1134 /* This routine differs from the core list-copy in allowing improper lists.
1135 Maybe the core could allow them similarly. */
1137 SCM_DEFINE (scm_srfi1_list_copy
, "list-copy", 1, 0, 0,
1139 "Return a copy of the given list @var{lst}.\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
1151 fill_here
= &newlst
;
1154 while (scm_is_pair (from_here
))
1157 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
1159 fill_here
= SCM_CDRLOC (c
);
1160 from_here
= SCM_CDR (from_here
);
1167 SCM_DEFINE (scm_srfi1_list_tabulate
, "list-tabulate", 2, 0, 0,
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
1176 scm_t_trampoline_1 proc_tramp
= scm_trampoline_1 (proc
);
1179 nn
= scm_to_signed_integer (n
, 0, LONG_MAX
);
1180 SCM_ASSERT (proc_tramp
, proc
, SCM_ARG2
, FUNC_NAME
);
1182 for (i
= nn
-1; i
>= 0; i
--)
1183 ret
= scm_cons (proc_tramp (proc
, scm_from_long (i
)), ret
);
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"
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"
1202 "(lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)\n"
1204 #define FUNC_NAME s_scm_srfi1_lset_adjoin
1206 scm_t_trampoline_2 equal_tramp
;
1209 equal_tramp
= scm_trampoline_2 (equal
);
1210 SCM_ASSERT (equal_tramp
, equal
, SCM_ARG1
, FUNC_NAME
);
1211 SCM_VALIDATE_REST_ARGUMENT (rest
);
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. */
1220 for ( ; scm_is_pair (rest
); rest
= SCM_CDR (rest
))
1222 elem
= SCM_CAR (rest
);
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 */
1228 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(l
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
1230 /* elem is not equal to anything already in lst, add it */
1231 lst
= scm_cons (elem
, lst
);
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"
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"
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"
1260 "@code{lset-difference!} may modify @var{lst} to form its\n"
1262 #define FUNC_NAME s_scm_srfi1_lset_difference_x
1264 scm_t_trampoline_2 equal_tramp
= scm_trampoline_2 (equal
);
1265 SCM ret
, *pos
, elem
, r
, b
;
1268 SCM_ASSERT (equal_tramp
, equal
, SCM_ARG1
, FUNC_NAME
);
1269 SCM_VALIDATE_REST_ARGUMENT (rest
);
1273 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
1275 elem
= SCM_CAR (lst
);
1277 for (r
= rest
, argnum
= SCM_ARG3
;
1279 r
= SCM_CDR (r
), argnum
++)
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 */
1285 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b
), b
, argnum
, FUNC_NAME
,"list");
1288 /* elem not equal to anything in later lists, so keep it */
1290 pos
= SCM_CDRLOC (lst
);
1295 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
1303 /* Typechecking for multi-argument MAP and FOR-EACH.
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. */
1309 check_map_args (SCM argv
,
1319 for (i
= SCM_SIMPLE_VECTOR_LENGTH (argv
) - 1; i
>= 1; i
--)
1322 elt
= SCM_SIMPLE_VECTOR_REF (argv
, i
);
1324 if (!(scm_is_null (elt
) || scm_is_pair (elt
)))
1325 goto check_map_error
;
1327 elt_len
= srfi1_ilength (elt
);
1329 goto check_map_error
;
1331 if (len
< 0 || (elt_len
>= 0 && elt_len
< len
))
1341 scm_apply_generic (gf
, scm_cons (proc
, args
));
1343 scm_wrong_type_arg (who
, i
+ 2, elt
);
1346 scm_remember_upto_here_1 (argv
);
1351 SCM_GPROC (s_srfi1_map
, "map", 2, 0, 1, scm_srfi1_map
, g_srfi1_map
);
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'.
1361 scm_srfi1_map (SCM proc
, SCM arg1
, SCM args
)
1362 #define FUNC_NAME s_srfi1_map
1368 len
= srfi1_ilength (arg1
);
1369 SCM_GASSERTn ((scm_is_null (arg1
) || scm_is_pair (arg1
)) && len
>= -1,
1371 scm_cons2 (proc
, arg1
, args
), SCM_ARG2
, s_srfi1_map
);
1372 SCM_VALIDATE_REST_ARGUMENT (args
);
1373 if (scm_is_null (args
))
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
))
1380 *pres
= scm_list_1 (call (proc
, SCM_CAR (arg1
)));
1381 pres
= SCM_CDRLOC (*pres
);
1382 arg1
= SCM_CDR (arg1
);
1386 if (scm_is_null (SCM_CDR (args
)))
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
))
1395 SCM_GASSERTn ((scm_is_null (arg2
) || scm_is_pair (arg2
))
1396 && len
>= 0 && len2
>= -1,
1398 scm_cons2 (proc
, arg1
, args
),
1399 len2
>= 0 ? SCM_ARG2
: SCM_ARG3
,
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
);
1411 args
= scm_vector (arg1
= scm_cons (arg1
, args
));
1412 len
= check_map_args (args
, len
, g_srfi1_map
, proc
, arg1
, s_srfi1_map
);
1416 for (i
= SCM_SIMPLE_VECTOR_LENGTH (args
) - 1; i
>= 0; i
--)
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
));
1422 *pres
= scm_list_1 (scm_apply (proc
, arg1
, SCM_EOL
));
1423 pres
= SCM_CDRLOC (*pres
);
1430 SCM_REGISTER_PROC (s_srfi1_map_in_order
, "map-in-order", 2, 0, 1, scm_srfi1_map
);
1432 SCM_GPROC (s_srfi1_for_each
, "for-each", 2, 0, 1, scm_srfi1_for_each
, g_srfi1_for_each
);
1435 scm_srfi1_for_each (SCM proc
, SCM arg1
, SCM args
)
1436 #define FUNC_NAME s_srfi1_for_each
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
))
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
))
1453 call (proc
, SCM_CAR (arg1
));
1454 arg1
= SCM_CDR (arg1
);
1456 return SCM_UNSPECIFIED
;
1458 if (scm_is_null (SCM_CDR (args
)))
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
))
1467 SCM_GASSERTn ((scm_is_null (arg2
) || scm_is_pair (arg2
))
1468 && len
>= 0 && len2
>= -1,
1470 scm_cons2 (proc
, arg1
, args
),
1471 len2
>= 0 ? SCM_ARG2
: SCM_ARG3
,
1475 call (proc
, SCM_CAR (arg1
), SCM_CAR (arg2
));
1476 arg1
= SCM_CDR (arg1
);
1477 arg2
= SCM_CDR (arg2
);
1480 return SCM_UNSPECIFIED
;
1482 args
= scm_vector (arg1
= scm_cons (arg1
, args
));
1483 len
= check_map_args (args
, len
, g_srfi1_for_each
, proc
, arg1
,
1488 for (i
= SCM_SIMPLE_VECTOR_LENGTH (args
) - 1; i
>= 0; i
--)
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
));
1494 scm_apply (proc
, arg1
, SCM_EOL
);
1497 return SCM_UNSPECIFIED
;
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"
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"
1514 "(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)\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
1521 scm_t_trampoline_2 equal_p
;
1522 SCM_VALIDATE_LIST (2, lst
);
1523 if (SCM_UNBNDP (pred
))
1524 equal_p
= equal_trampoline
;
1527 equal_p
= scm_trampoline_2 (pred
);
1528 SCM_ASSERT (equal_p
, pred
, 3, FUNC_NAME
);
1530 for (; !SCM_NULL_OR_NIL_P (lst
); lst
= SCM_CDR (lst
))
1532 if (scm_is_true (equal_p (pred
, x
, SCM_CAR (lst
))))
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
1547 scm_t_trampoline_2 equal_p
;
1548 if (SCM_UNBNDP (pred
))
1549 equal_p
= equal_trampoline
;
1552 equal_p
= scm_trampoline_2 (pred
);
1553 SCM_ASSERT (equal_p
, pred
, 3, FUNC_NAME
);
1555 for(; scm_is_pair (ls
); ls
= SCM_CDR (ls
))
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
)))
1563 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls
), alist
, SCM_ARG2
, FUNC_NAME
,
1564 "association list");
1570 SCM_DEFINE (scm_srfi1_ninth
, "ninth", 1, 0, 0,
1572 "Return the ninth element of @var{lst}.")
1573 #define FUNC_NAME s_scm_srfi1_ninth
1575 return scm_list_ref (lst
, scm_from_int (8));
1580 SCM_DEFINE (scm_srfi1_not_pair_p
, "not-pair?", 1, 0, 0,
1582 "Return @code{#t} is @var{obj} is not a pair, @code{#f}\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
1590 return scm_from_bool (! scm_is_pair (obj
));
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
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
;
1612 SCM_ASSERT(call
, pred
, 2, FUNC_NAME
);
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
;
1622 SCM_SETCDR(dropped_tail
, new_tail
);
1623 dropped_tail
= new_tail
;
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
);
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"
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"
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"
1649 "@var{lst} may be modified to construct the return lists.")
1650 #define FUNC_NAME s_scm_srfi1_partition_x
1652 SCM tlst
, flst
, *tp
, *fp
;
1653 scm_t_trampoline_1 pred_tramp
;
1655 pred_tramp
= scm_trampoline_1 (pred
);
1656 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
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. */
1667 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
1669 if (scm_is_true (pred_tramp (pred
, SCM_CAR (lst
))))
1672 tp
= SCM_CDRLOC (lst
);
1677 fp
= SCM_CDRLOC (lst
);
1681 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
1683 /* terminate whichever didn't get the last element(s) */
1687 return scm_values (scm_list_2 (tlst
, flst
));
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"
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"
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"
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"
1716 "(reduce + 0 '(5 6 7)) @result{} 18\n"
1718 "(+ 6 5) @result{} 11\n"
1719 "(+ 7 11) @result{} 18\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
1729 scm_t_trampoline_2 proc_tramp
= scm_trampoline_2 (proc
);
1732 SCM_ASSERT (proc_tramp
, proc
, SCM_ARG1
, FUNC_NAME
);
1734 ret
= def
; /* if lst is empty */
1735 if (scm_is_pair (lst
))
1737 ret
= SCM_CAR (lst
); /* if lst has one element */
1739 for (lst
= SCM_CDR (lst
); scm_is_pair (lst
); lst
= SCM_CDR (lst
))
1740 ret
= proc_tramp (proc
, SCM_CAR (lst
), ret
);
1743 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG3
, FUNC_NAME
, "list");
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"
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"
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"
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"
1773 "(reduce-right + 0 '(5 6 7)) @result{} 18\n"
1775 "(+ 6 7) @result{} 13\n"
1776 "(+ 5 13) @result{} 18\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"
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
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. */
1797 scm_t_trampoline_2 proc_tramp
= scm_trampoline_2 (proc
);
1801 SCM_ASSERT (proc_tramp
, proc
, SCM_ARG1
, FUNC_NAME
);
1803 if (SCM_NULL_OR_NIL_P (lst
))
1806 vec
= scm_vector (lst
);
1807 len
= SCM_SIMPLE_VECTOR_LENGTH (vec
);
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
);
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"
1825 #define FUNC_NAME s_scm_srfi1_remove
1827 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
1831 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
1832 SCM_VALIDATE_LIST (2, list
);
1834 for (prev
= &res
, walk
= list
;
1836 walk
= SCM_CDR (walk
))
1838 if (scm_is_false (call (pred
, SCM_CAR (walk
))))
1840 *prev
= scm_cons (SCM_CAR (walk
), SCM_EOL
);
1841 prev
= SCM_CDRLOC (*prev
);
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"
1858 #define FUNC_NAME s_scm_srfi1_remove_x
1860 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
1863 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
1864 SCM_VALIDATE_LIST (2, list
);
1866 for (prev
= &list
, walk
= list
;
1868 walk
= SCM_CDR (walk
))
1870 if (scm_is_false (call (pred
, SCM_CAR (walk
))))
1871 prev
= SCM_CDRLOC (walk
);
1873 *prev
= SCM_CDR (walk
);
1881 SCM_DEFINE (scm_srfi1_seventh
, "seventh", 1, 0, 0,
1883 "Return the seventh element of @var{lst}.")
1884 #define FUNC_NAME s_scm_srfi1_seventh
1886 return scm_list_ref (lst
, scm_from_int (6));
1891 SCM_DEFINE (scm_srfi1_sixth
, "sixth", 1, 0, 0,
1893 "Return the sixth element of @var{lst}.")
1894 #define FUNC_NAME s_scm_srfi1_sixth
1896 return scm_list_ref (lst
, scm_from_int (5));
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
1908 scm_t_trampoline_1 pred_tramp
;
1911 pred_tramp
= scm_trampoline_1 (pred
);
1912 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
1916 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
1918 SCM elem
= SCM_CAR (lst
);
1919 if (scm_is_false (pred_tramp (pred
, elem
)))
1922 /* want this elem, tack it onto the end of ret */
1923 *p
= scm_cons (elem
, SCM_EOL
);
1924 p
= SCM_CDRLOC (*p
);
1926 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
1929 return scm_values (scm_list_2 (ret
, lst
));
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"
1940 #define FUNC_NAME s_scm_srfi1_span_x
1943 scm_t_trampoline_1 pred_tramp
;
1945 pred_tramp
= scm_trampoline_1 (pred
);
1946 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
1949 for (upto
= lst
; scm_is_pair (upto
); upto
= SCM_CDR (upto
))
1951 if (scm_is_false (pred_tramp (pred
, SCM_CAR (upto
))))
1954 /* want this element */
1955 p
= SCM_CDRLOC (upto
);
1957 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
1961 return scm_values (scm_list_2 (lst
, upto
));
1966 SCM_DEFINE (scm_srfi1_split_at
, "split-at", 2, 0, 0,
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"
1971 #define FUNC_NAME s_scm_srfi1_split_at
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 */
1979 for (nn
= scm_to_size_t (n
); nn
!= 0; nn
--)
1981 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
1983 *loc
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
1984 loc
= SCM_CDRLOC (*loc
);
1987 return scm_values (scm_list_2 (pre
, lst
));
1992 SCM_DEFINE (scm_srfi1_split_at_x
, "split-at!", 2, 0, 0,
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
2003 for (nn
= scm_to_size_t (n
); nn
!= 0; nn
--)
2005 SCM_VALIDATE_CONS (SCM_ARG1
, upto
);
2007 loc
= SCM_CDRLOC (upto
);
2008 upto
= SCM_CDR (upto
);
2012 return scm_values (scm_list_2 (lst
, upto
));
2017 SCM_DEFINE (scm_srfi1_take_x
, "take!", 2, 0, 0,
2019 "Return a list containing the first @var{n} elements of\n"
2021 #define FUNC_NAME s_scm_srfi1_take_x
2026 nn
= scm_to_signed_integer (n
, 0, LONG_MAX
);
2030 pos
= scm_list_tail (lst
, scm_from_long (nn
- 1));
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
);
2038 SCM_SETCDR (pos
, SCM_EOL
);
2044 SCM_DEFINE (scm_srfi1_take_right
, "take-right", 2, 0, 0,
2046 "Return the a list containing the @var{n} last elements of\n"
2048 #define FUNC_NAME s_scm_srfi1_take_right
2050 SCM tail
= scm_list_tail (lst
, n
);
2051 while (scm_is_pair (tail
))
2053 lst
= SCM_CDR (lst
);
2054 tail
= SCM_CDR (tail
);
2056 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail
), tail
, SCM_ARG1
, FUNC_NAME
, "list");
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
2068 scm_t_trampoline_1 pred_tramp
;
2071 pred_tramp
= scm_trampoline_1 (pred
);
2072 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
2076 for ( ; scm_is_pair (lst
); lst
= SCM_CDR (lst
))
2078 SCM elem
= SCM_CAR (lst
);
2079 if (scm_is_false (pred_tramp (pred
, elem
)))
2082 /* want this elem, tack it onto the end of ret */
2083 *p
= scm_cons (elem
, SCM_EOL
);
2084 p
= SCM_CDRLOC (*p
);
2086 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
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
2102 scm_t_trampoline_1 pred_tramp
;
2104 pred_tramp
= scm_trampoline_1 (pred
);
2105 SCM_ASSERT (pred_tramp
, pred
, SCM_ARG1
, FUNC_NAME
);
2108 for (upto
= lst
; scm_is_pair (upto
); upto
= SCM_CDR (upto
))
2110 if (scm_is_false (pred_tramp (pred
, SCM_CAR (upto
))))
2113 /* want this element */
2114 p
= SCM_CDRLOC (upto
);
2116 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto
), lst
, SCM_ARG2
, FUNC_NAME
, "list");
2125 SCM_DEFINE (scm_srfi1_tenth
, "tenth", 1, 0, 0,
2127 "Return the tenth element of @var{lst}.")
2128 #define FUNC_NAME s_scm_srfi1_tenth
2130 return scm_list_ref (lst
, scm_from_int (9));
2135 SCM_DEFINE (scm_srfi1_xcons
, "xcons", 2, 0, 0,
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
2141 return scm_cons (a
, d
);
2147 scm_init_srfi_1 (void)
2149 SCM the_root_module
= scm_lookup_closure_module (SCM_BOOL_F
);
2150 #ifndef SCM_MAGIC_SNARFER
2151 #include "srfi/srfi-1.x"
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")));
2161 /* End of srfi-1.c. */