1 /* Copyright (C) 1995,1996,1997,2000,2001, 2003, 2004 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "libguile/_scm.h"
21 #include "libguile/eq.h"
22 #include "libguile/lang.h"
24 #include "libguile/validate.h"
25 #include "libguile/list.h"
26 #include "libguile/eval.h"
30 #define var_start(x, y) va_start(x, y)
33 #define var_start(x, y) va_start(x)
39 #define SCM_I_CONS(cell, x, y) \
41 cell = scm_cell ((scm_t_bits)x, (scm_t_bits)y); \
48 SCM_I_CONS (c1
, e1
, SCM_EOL
);
53 scm_list_2 (SCM e1
, SCM e2
)
56 SCM_I_CONS (c2
, e2
, SCM_EOL
);
57 SCM_I_CONS (c1
, e1
, c2
);
62 scm_list_3 (SCM e1
, SCM e2
, SCM e3
)
65 SCM_I_CONS (c3
, e3
, SCM_EOL
);
66 SCM_I_CONS (c2
, e2
, c3
);
67 SCM_I_CONS (c1
, e1
, c2
);
72 scm_list_4 (SCM e1
, SCM e2
, SCM e3
, SCM e4
)
74 return scm_cons2 (e1
, e2
, scm_list_2 (e3
, e4
));
78 scm_list_5 (SCM e1
, SCM e2
, SCM e3
, SCM e4
, SCM e5
)
80 return scm_cons2 (e1
, e2
, scm_list_3 (e3
, e4
, e5
));
84 scm_list_n (SCM elt
, ...)
91 while (! SCM_UNBNDP (elt
))
93 #if (SCM_DEBUG_CELL_ACCESSES == 1)
95 SCM_VALIDATE_CELL(elt
, 0);
97 *pos
= scm_cons (elt
, SCM_EOL
);
98 pos
= SCM_CDRLOC (*pos
);
99 elt
= va_arg (foo
, SCM
);
106 SCM_DEFINE (scm_list
, "list", 0, 0, 1,
108 "Return a list containing @var{objs}, the arguments to\n"
110 #define FUNC_NAME s_scm_list
117 SCM_DEFINE (scm_cons_star
, "cons*", 1, 0, 1,
119 "Like @code{list}, but the last arg provides the tail of the\n"
120 "constructed list, returning @code{(cons @var{arg1} (cons\n"
121 "@var{arg2} (cons @dots{} @var{argn})))}. Requires at least one\n"
122 "argument. If given one argument, that argument is returned as\n"
123 "result. This function is called @code{list*} in some other\n"
124 "Schemes and in Common LISP.")
125 #define FUNC_NAME s_scm_cons_star
127 SCM_VALIDATE_REST_ARGUMENT (rest
);
128 if (!SCM_NULLP (rest
))
130 SCM prev
= arg
= scm_cons (arg
, rest
);
131 while (!SCM_NULLP (SCM_CDR (rest
)))
134 rest
= SCM_CDR (rest
);
136 SCM_SETCDR (prev
, SCM_CAR (rest
));
144 /* general questions about lists --- null?, list?, length, etc. */
146 SCM_DEFINE (scm_null_p
, "null?", 1, 0, 0,
148 "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
149 #define FUNC_NAME s_scm_null_p
151 return SCM_BOOL (SCM_NULL_OR_NIL_P (x
));
156 SCM_DEFINE (scm_list_p
, "list?", 1, 0, 0,
158 "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
159 #define FUNC_NAME s_scm_list_p
161 return SCM_BOOL (scm_ilength (x
) >= 0);
166 /* Return the length of SX, or -1 if it's not a proper list.
167 This uses the "tortoise and hare" algorithm to detect "infinitely
168 long" lists (i.e. lists with cycles in their cdrs), and returns -1
169 if it does find one. */
178 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
179 if (!SCM_CONSP (hare
)) return -1;
180 hare
= SCM_CDR(hare
);
182 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
183 if (!SCM_CONSP (hare
)) return -1;
184 hare
= SCM_CDR(hare
);
186 /* For every two steps the hare takes, the tortoise takes one. */
187 tortoise
= SCM_CDR(tortoise
);
189 while (! SCM_EQ_P (hare
, tortoise
));
191 /* If the tortoise ever catches the hare, then the list must contain
197 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
199 "Return the number of elements in list @var{lst}.")
200 #define FUNC_NAME s_scm_length
203 SCM_VALIDATE_LIST_COPYLEN (1, lst
, i
);
204 return SCM_MAKINUM (i
);
210 /* appending lists */
212 SCM_DEFINE (scm_append
, "append", 0, 0, 1,
214 "Return a list consisting of the elements the lists passed as\n"
217 "(append '(x) '(y)) @result{} (x y)\n"
218 "(append '(a) '(b c d)) @result{} (a b c d)\n"
219 "(append '(a (b)) '((c))) @result{} (a (b) (c))\n"
221 "The resulting list is always newly allocated, except that it\n"
222 "shares structure with the last list argument. The last\n"
223 "argument may actually be any object; an improper list results\n"
224 "if the last argument is not a proper list.\n"
226 "(append '(a b) '(c . d)) @result{} (a b c . d)\n"
227 "(append '() 'a) @result{} a\n"
229 #define FUNC_NAME s_scm_append
231 SCM_VALIDATE_REST_ARGUMENT (args
);
232 if (SCM_NULLP (args
)) {
237 SCM arg
= SCM_CAR (args
);
239 args
= SCM_CDR (args
);
240 while (!SCM_NULLP (args
)) {
241 while (SCM_CONSP (arg
)) {
242 *lloc
= scm_cons (SCM_CAR (arg
), SCM_EOL
);
243 lloc
= SCM_CDRLOC (*lloc
);
246 SCM_VALIDATE_NULL_OR_NIL (argnum
, arg
);
247 arg
= SCM_CAR (args
);
248 args
= SCM_CDR (args
);
258 SCM_DEFINE (scm_append_x
, "append!", 0, 0, 1,
260 "A destructive version of @code{append} (@pxref{Pairs and\n"
261 "Lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field\n"
262 "of each list's final pair is changed to point to the head of\n"
263 "the next list, so no consing is performed. Return\n"
265 #define FUNC_NAME s_scm_append_x
268 SCM_VALIDATE_REST_ARGUMENT (lists
);
270 if (SCM_NULLP (lists
))
276 SCM arg
= SCM_CAR (lists
);
279 lists
= SCM_CDR (lists
);
280 if (SCM_NULLP (lists
))
283 if (!SCM_NULL_OR_NIL_P (arg
))
285 SCM_VALIDATE_CONS (SCM_ARG1
, arg
);
286 loc
= SCM_CDRLOC (scm_last_pair (arg
));
293 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
295 "Return the last pair in @var{lst}, signalling an error if\n"
296 "@var{lst} is circular.")
297 #define FUNC_NAME s_scm_last_pair
302 if (SCM_NULL_OR_NIL_P (lst
))
305 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
307 SCM ahead
= SCM_CDR(hare
);
308 if (!SCM_CONSP (ahead
)) return hare
;
310 ahead
= SCM_CDR(hare
);
311 if (!SCM_CONSP (ahead
)) return hare
;
313 tortoise
= SCM_CDR(tortoise
);
315 while (! SCM_EQ_P (hare
, tortoise
));
316 SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst
));
321 /* reversing lists */
323 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
325 "Return a new list that contains the elements of @var{lst} but\n"
327 #define FUNC_NAME s_scm_reverse
329 SCM result
= SCM_EOL
;
334 if (SCM_NULL_OR_NIL_P(hare
)) return result
;
335 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
336 result
= scm_cons (SCM_CAR (hare
), result
);
337 hare
= SCM_CDR (hare
);
338 if (SCM_NULL_OR_NIL_P(hare
)) return result
;
339 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
340 result
= scm_cons (SCM_CAR (hare
), result
);
341 hare
= SCM_CDR (hare
);
342 tortoise
= SCM_CDR (tortoise
);
344 while (! SCM_EQ_P (hare
, tortoise
));
345 SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst
));
349 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
350 (SCM lst
, SCM new_tail
),
351 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
352 "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
353 "modified to point to the previous list element. Return the\n"
355 "Caveat: because the list is modified in place, the tail of the original\n"
356 "list now becomes its head, and the head of the original list now becomes\n"
357 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
358 "original list was bound now points to the tail. To ensure that the head\n"
359 "of the modified list is not lost, it is wise to save the return value of\n"
361 #define FUNC_NAME s_scm_reverse_x
363 SCM_VALIDATE_LIST (1, lst
);
364 if (SCM_UNBNDP (new_tail
))
367 SCM_VALIDATE_LIST (2, new_tail
);
369 while (!SCM_NULL_OR_NIL_P (lst
))
371 SCM old_tail
= SCM_CDR (lst
);
372 SCM_SETCDR (lst
, new_tail
);
382 /* indexing lists by element number */
384 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
386 "Return the @var{k}th element from @var{list}.")
387 #define FUNC_NAME s_scm_list_ref
391 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
392 while (SCM_CONSP (lst
)) {
394 return SCM_CAR (lst
);
400 if (SCM_NULL_OR_NIL_P (lst
))
401 SCM_OUT_OF_RANGE (2, k
);
403 SCM_WRONG_TYPE_ARG (1, list
);
408 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
409 (SCM list
, SCM k
, SCM val
),
410 "Set the @var{k}th element of @var{list} to @var{val}.")
411 #define FUNC_NAME s_scm_list_set_x
415 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
416 while (SCM_CONSP (lst
)) {
418 SCM_SETCAR (lst
, val
);
425 if (SCM_NULL_OR_NIL_P (lst
))
426 SCM_OUT_OF_RANGE (2, k
);
428 SCM_WRONG_TYPE_ARG (1, list
);
433 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
435 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
437 "@deffnx {Scheme Procedure} list-cdr-ref lst k\n"
438 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
439 "The first element of the list is considered to be element 0.\n\n"
440 "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
441 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
442 "or returning the results of cdring @var{k} times down @var{lst}.")
443 #define FUNC_NAME s_scm_list_tail
446 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
448 SCM_VALIDATE_CONS (1, lst
);
456 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
457 (SCM list
, SCM k
, SCM val
),
458 "Set the @var{k}th cdr of @var{list} to @var{val}.")
459 #define FUNC_NAME s_scm_list_cdr_set_x
463 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
464 while (SCM_CONSP (lst
)) {
466 SCM_SETCDR (lst
, val
);
473 if (SCM_NULL_OR_NIL_P (lst
))
474 SCM_OUT_OF_RANGE (2, k
);
476 SCM_WRONG_TYPE_ARG (1, list
);
482 /* copying lists, perhaps partially */
484 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
486 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
488 #define FUNC_NAME s_scm_list_head
494 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
499 SCM_VALIDATE_CONS (1, lst
);
500 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
501 pos
= SCM_CDRLOC (*pos
);
509 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
511 "Return a (newly-created) copy of @var{lst}.")
512 #define FUNC_NAME s_scm_list_copy
518 SCM_VALIDATE_LIST (1, lst
);
524 while (SCM_CONSP (from_here
))
527 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
529 fill_here
= SCM_CDRLOC (c
);
530 from_here
= SCM_CDR (from_here
);
537 /* membership tests (memq, memv, etc.) */
539 /* The function scm_c_memq returns the first sublist of list whose car is
540 * 'eq?' obj, where the sublists of list are the non-empty lists returned by
541 * (list-tail list k) for k less than the length of list. If obj does not
542 * occur in list, then #f (not the empty list) is returned.
543 * List must be a proper list, otherwise scm_c_memq may crash or loop
547 scm_c_memq (SCM obj
, SCM list
)
549 for (; !SCM_NULL_OR_NIL_P (list
); list
= SCM_CDR (list
))
551 if (SCM_EQ_P (SCM_CAR (list
), obj
))
558 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
560 "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
561 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
562 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
563 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
564 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
566 #define FUNC_NAME s_scm_memq
568 SCM_VALIDATE_LIST (2, lst
);
569 return scm_c_memq (x
, lst
);
574 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
576 "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
577 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
578 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
579 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
580 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
582 #define FUNC_NAME s_scm_memv
584 SCM_VALIDATE_LIST (2, lst
);
585 for (; !SCM_NULL_OR_NIL_P (lst
); lst
= SCM_CDR (lst
))
587 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
595 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
597 "Return the first sublist of @var{lst} whose car is\n"
598 "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
599 "the non-empty lists returned by @code{(list-tail @var{lst}\n"
600 "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
601 "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
602 "empty list) is returned.")
603 #define FUNC_NAME s_scm_member
605 SCM_VALIDATE_LIST (2, lst
);
606 for (; !SCM_NULL_OR_NIL_P (lst
); lst
= SCM_CDR (lst
))
608 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
616 /* deleting elements from a list (delq, etc.) */
618 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
620 "@deffnx {Scheme Procedure} delv! item lst\n"
621 "@deffnx {Scheme Procedure} delete! item lst\n"
622 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
623 "and @code{delete}: they modify the existing @var{lst}\n"
624 "rather than creating a new list. Caveat evaluator: Like other\n"
625 "destructive list functions, these functions cannot modify the binding of\n"
626 "@var{lst}, and so cannot be used to delete the first element of\n"
627 "@var{lst} destructively.")
628 #define FUNC_NAME s_scm_delq_x
633 for (prev
= &lst
, walk
= lst
;
635 walk
= SCM_CDR (walk
))
637 if (SCM_EQ_P (SCM_CAR (walk
), item
))
638 *prev
= SCM_CDR (walk
);
640 prev
= SCM_CDRLOC (walk
);
648 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
650 "Destructively remove all elements from @var{lst} that are\n"
651 "@code{eqv?} to @var{item}.")
652 #define FUNC_NAME s_scm_delv_x
657 for (prev
= &lst
, walk
= lst
;
659 walk
= SCM_CDR (walk
))
661 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
662 *prev
= SCM_CDR (walk
);
664 prev
= SCM_CDRLOC (walk
);
673 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
675 "Destructively remove all elements from @var{lst} that are\n"
676 "@code{equal?} to @var{item}.")
677 #define FUNC_NAME s_scm_delete_x
682 for (prev
= &lst
, walk
= lst
;
684 walk
= SCM_CDR (walk
))
686 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
687 *prev
= SCM_CDR (walk
);
689 prev
= SCM_CDRLOC (walk
);
700 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
702 "Return a newly-created copy of @var{lst} with elements\n"
703 "@code{eq?} to @var{item} removed. This procedure mirrors\n"
704 "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
705 "@var{item} with @code{eq?}.")
706 #define FUNC_NAME s_scm_delq
708 SCM copy
= scm_list_copy (lst
);
709 return scm_delq_x (item
, copy
);
713 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
715 "Return a newly-created copy of @var{lst} with elements\n"
716 "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
717 "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
718 "@var{item} with @code{eqv?}.")
719 #define FUNC_NAME s_scm_delv
721 SCM copy
= scm_list_copy (lst
);
722 return scm_delv_x (item
, copy
);
726 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
728 "Return a newly-created copy of @var{lst} with elements\n"
729 "@code{equal?} to @var{item} removed. This procedure mirrors\n"
730 "@code{member}: @code{delete} compares elements of @var{lst}\n"
731 "against @var{item} with @code{equal?}.")
732 #define FUNC_NAME s_scm_delete
734 SCM copy
= scm_list_copy (lst
);
735 return scm_delete_x (item
, copy
);
740 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
742 "Like @code{delq!}, but only deletes the first occurrence of\n"
743 "@var{item} from @var{lst}. Tests for equality using\n"
744 "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
745 #define FUNC_NAME s_scm_delq1_x
750 for (prev
= &lst
, walk
= lst
;
752 walk
= SCM_CDR (walk
))
754 if (SCM_EQ_P (SCM_CAR (walk
), item
))
756 *prev
= SCM_CDR (walk
);
760 prev
= SCM_CDRLOC (walk
);
768 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
770 "Like @code{delv!}, but only deletes the first occurrence of\n"
771 "@var{item} from @var{lst}. Tests for equality using\n"
772 "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
773 #define FUNC_NAME s_scm_delv1_x
778 for (prev
= &lst
, walk
= lst
;
780 walk
= SCM_CDR (walk
))
782 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
784 *prev
= SCM_CDR (walk
);
788 prev
= SCM_CDRLOC (walk
);
796 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
798 "Like @code{delete!}, but only deletes the first occurrence of\n"
799 "@var{item} from @var{lst}. Tests for equality using\n"
800 "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
801 #define FUNC_NAME s_scm_delete1_x
806 for (prev
= &lst
, walk
= lst
;
808 walk
= SCM_CDR (walk
))
810 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
812 *prev
= SCM_CDR (walk
);
816 prev
= SCM_CDRLOC (walk
);
823 SCM_DEFINE (scm_filter
, "filter", 2, 0, 0,
824 (SCM pred
, SCM list
),
825 "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n"
826 "The list is not disordered -- elements that appear in the result list occur\n"
827 "in the same order as they occur in the argument list. The returned list may\n"
828 "share a common tail with the argument list. The dynamic order in which the\n"
829 "various applications of pred are made is not specified.\n\n"
831 "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n"
833 #define FUNC_NAME s_scm_filter
835 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
839 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
840 SCM_VALIDATE_LIST (2, list
);
842 for (prev
= &res
, walk
= list
;
844 walk
= SCM_CDR (walk
))
846 if (!SCM_FALSEP (call (pred
, SCM_CAR (walk
))))
848 *prev
= scm_cons (SCM_CAR (walk
), SCM_EOL
);
849 prev
= SCM_CDRLOC (*prev
);
857 SCM_DEFINE (scm_filter_x
, "filter!", 2, 0, 0,
858 (SCM pred
, SCM list
),
859 "Linear-update variant of @code{filter}.")
860 #define FUNC_NAME s_scm_filter_x
862 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
865 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
866 SCM_VALIDATE_LIST (2, list
);
868 for (prev
= &list
, walk
= list
;
870 walk
= SCM_CDR (walk
))
872 if (!SCM_FALSEP (call (pred
, SCM_CAR (walk
))))
873 prev
= SCM_CDRLOC (walk
);
875 *prev
= SCM_CDR (walk
);
886 #include "libguile/list.x"