1 /* Copyright (C) 1995,1996,1997,2000,2001, 2003 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
))
94 SCM_VALIDATE_CELL(elt
, 0);
96 *pos
= scm_cons (elt
, SCM_EOL
);
97 pos
= SCM_CDRLOC (*pos
);
98 elt
= va_arg (foo
, SCM
);
105 SCM_DEFINE (scm_list
, "list", 0, 0, 1,
107 "Return a list containing @var{objs}, the arguments to\n"
109 #define FUNC_NAME s_scm_list
116 SCM_DEFINE (scm_cons_star
, "cons*", 1, 0, 1,
118 "Like @code{list}, but the last arg provides the tail of the\n"
119 "constructed list, returning @code{(cons @var{arg1} (cons\n"
120 "@var{arg2} (cons @dots{} @var{argn})))}. Requires at least one\n"
121 "argument. If given one argument, that argument is returned as\n"
122 "result. This function is called @code{list*} in some other\n"
123 "Schemes and in Common LISP.")
124 #define FUNC_NAME s_scm_cons_star
126 SCM_VALIDATE_REST_ARGUMENT (rest
);
127 if (!SCM_NULLP (rest
))
129 SCM prev
= arg
= scm_cons (arg
, rest
);
130 while (!SCM_NULLP (SCM_CDR (rest
)))
133 rest
= SCM_CDR (rest
);
135 SCM_SETCDR (prev
, SCM_CAR (rest
));
143 /* general questions about lists --- null?, list?, length, etc. */
145 SCM_DEFINE (scm_null_p
, "null?", 1, 0, 0,
147 "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
148 #define FUNC_NAME s_scm_null_p
150 return SCM_BOOL (SCM_NULL_OR_NIL_P (x
));
155 SCM_DEFINE (scm_list_p
, "list?", 1, 0, 0,
157 "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
158 #define FUNC_NAME s_scm_list_p
160 return SCM_BOOL (scm_ilength (x
) >= 0);
165 /* Return the length of SX, or -1 if it's not a proper list.
166 This uses the "tortoise and hare" algorithm to detect "infinitely
167 long" lists (i.e. lists with cycles in their cdrs), and returns -1
168 if it does find one. */
177 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
178 if (!SCM_CONSP (hare
)) return -1;
179 hare
= SCM_CDR(hare
);
181 if (SCM_NULL_OR_NIL_P(hare
)) return i
;
182 if (!SCM_CONSP (hare
)) return -1;
183 hare
= SCM_CDR(hare
);
185 /* For every two steps the hare takes, the tortoise takes one. */
186 tortoise
= SCM_CDR(tortoise
);
188 while (! SCM_EQ_P (hare
, tortoise
));
190 /* If the tortoise ever catches the hare, then the list must contain
196 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
198 "Return the number of elements in list @var{lst}.")
199 #define FUNC_NAME s_scm_length
202 SCM_VALIDATE_LIST_COPYLEN (1, lst
, i
);
203 return SCM_MAKINUM (i
);
209 /* appending lists */
211 SCM_DEFINE (scm_append
, "append", 0, 0, 1,
213 "Return a list consisting of the elements the lists passed as\n"
216 "(append '(x) '(y)) @result{} (x y)\n"
217 "(append '(a) '(b c d)) @result{} (a b c d)\n"
218 "(append '(a (b)) '((c))) @result{} (a (b) (c))\n"
220 "The resulting list is always newly allocated, except that it\n"
221 "shares structure with the last list argument. The last\n"
222 "argument may actually be any object; an improper list results\n"
223 "if the last argument is not a proper list.\n"
225 "(append '(a b) '(c . d)) @result{} (a b c . d)\n"
226 "(append '() 'a) @result{} a\n"
228 #define FUNC_NAME s_scm_append
230 SCM_VALIDATE_REST_ARGUMENT (args
);
231 if (SCM_NULLP (args
)) {
236 SCM arg
= SCM_CAR (args
);
238 args
= SCM_CDR (args
);
239 while (!SCM_NULLP (args
)) {
240 while (SCM_CONSP (arg
)) {
241 *lloc
= scm_cons (SCM_CAR (arg
), SCM_EOL
);
242 lloc
= SCM_CDRLOC (*lloc
);
245 SCM_VALIDATE_NULL_OR_NIL (argnum
, arg
);
246 arg
= SCM_CAR (args
);
247 args
= SCM_CDR (args
);
257 SCM_DEFINE (scm_append_x
, "append!", 0, 0, 1,
259 "A destructive version of @code{append} (@pxref{Pairs and\n"
260 "Lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field\n"
261 "of each list's final pair is changed to point to the head of\n"
262 "the next list, so no consing is performed. Return a pointer to\n"
264 #define FUNC_NAME s_scm_append_x
266 SCM_VALIDATE_REST_ARGUMENT (lists
);
268 if (SCM_NULLP (lists
)) {
271 SCM arg
= SCM_CAR (lists
);
272 lists
= SCM_CDR (lists
);
273 if (SCM_NULLP (lists
)) {
275 } else if (!SCM_NULL_OR_NIL_P (arg
)) {
276 SCM_VALIDATE_CONS (SCM_ARG1
, arg
);
277 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (lists
));
286 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
288 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
289 "@var{lst} is circular.")
290 #define FUNC_NAME s_scm_last_pair
295 if (SCM_NULL_OR_NIL_P (lst
))
298 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
300 SCM ahead
= SCM_CDR(hare
);
301 if (!SCM_CONSP (ahead
)) return hare
;
303 ahead
= SCM_CDR(hare
);
304 if (!SCM_CONSP (ahead
)) return hare
;
306 tortoise
= SCM_CDR(tortoise
);
308 while (! SCM_EQ_P (hare
, tortoise
));
309 SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst
));
314 /* reversing lists */
316 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
318 "Return a new list that contains the elements of @var{lst} but\n"
320 #define FUNC_NAME s_scm_reverse
322 SCM result
= SCM_EOL
;
327 if (SCM_NULL_OR_NIL_P(hare
)) return result
;
328 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
329 result
= scm_cons (SCM_CAR (hare
), result
);
330 hare
= SCM_CDR (hare
);
331 if (SCM_NULL_OR_NIL_P(hare
)) return result
;
332 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
333 result
= scm_cons (SCM_CAR (hare
), result
);
334 hare
= SCM_CDR (hare
);
335 tortoise
= SCM_CDR (tortoise
);
337 while (! SCM_EQ_P (hare
, tortoise
));
338 SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst
));
342 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
343 (SCM lst
, SCM new_tail
),
344 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
345 "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
346 "modified to point to the previous list element. Return a pointer to the\n"
347 "head of the reversed list.\n\n"
348 "Caveat: because the list is modified in place, the tail of the original\n"
349 "list now becomes its head, and the head of the original list now becomes\n"
350 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
351 "original list was bound now points to the tail. To ensure that the head\n"
352 "of the modified list is not lost, it is wise to save the return value of\n"
354 #define FUNC_NAME s_scm_reverse_x
356 SCM_VALIDATE_LIST (1, lst
);
357 if (SCM_UNBNDP (new_tail
))
360 SCM_VALIDATE_LIST (2, new_tail
);
362 while (!SCM_NULL_OR_NIL_P (lst
))
364 SCM old_tail
= SCM_CDR (lst
);
365 SCM_SETCDR (lst
, new_tail
);
375 /* indexing lists by element number */
377 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
379 "Return the @var{k}th element from @var{list}.")
380 #define FUNC_NAME s_scm_list_ref
384 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
385 while (SCM_CONSP (lst
)) {
387 return SCM_CAR (lst
);
393 if (SCM_NULL_OR_NIL_P (lst
))
394 SCM_OUT_OF_RANGE (2, k
);
396 SCM_WRONG_TYPE_ARG (1, list
);
401 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
402 (SCM list
, SCM k
, SCM val
),
403 "Set the @var{k}th element of @var{list} to @var{val}.")
404 #define FUNC_NAME s_scm_list_set_x
408 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
409 while (SCM_CONSP (lst
)) {
411 SCM_SETCAR (lst
, val
);
418 if (SCM_NULL_OR_NIL_P (lst
))
419 SCM_OUT_OF_RANGE (2, k
);
421 SCM_WRONG_TYPE_ARG (1, list
);
426 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
428 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
430 "@deffnx {Scheme Procedure} list-cdr-ref lst k\n"
431 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
432 "The first element of the list is considered to be element 0.\n\n"
433 "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
434 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
435 "or returning the results of cdring @var{k} times down @var{lst}.")
436 #define FUNC_NAME s_scm_list_tail
439 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
441 SCM_VALIDATE_CONS (1, lst
);
449 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
450 (SCM list
, SCM k
, SCM val
),
451 "Set the @var{k}th cdr of @var{list} to @var{val}.")
452 #define FUNC_NAME s_scm_list_cdr_set_x
456 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
457 while (SCM_CONSP (lst
)) {
459 SCM_SETCDR (lst
, val
);
466 if (SCM_NULL_OR_NIL_P (lst
))
467 SCM_OUT_OF_RANGE (2, k
);
469 SCM_WRONG_TYPE_ARG (1, list
);
475 /* copying lists, perhaps partially */
477 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
479 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
481 #define FUNC_NAME s_scm_list_head
487 SCM_VALIDATE_INUM_MIN_COPY (2, k
,0, i
);
492 SCM_VALIDATE_CONS (1, lst
);
493 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
494 pos
= SCM_CDRLOC (*pos
);
502 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
504 "Return a (newly-created) copy of @var{lst}.")
505 #define FUNC_NAME s_scm_list_copy
511 SCM_VALIDATE_LIST (1, lst
);
517 while (SCM_CONSP (from_here
))
520 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
522 fill_here
= SCM_CDRLOC (c
);
523 from_here
= SCM_CDR (from_here
);
530 /* membership tests (memq, memv, etc.) */
532 /* The function scm_c_memq returns the first sublist of list whose car is
533 * 'eq?' obj, where the sublists of list are the non-empty lists returned by
534 * (list-tail list k) for k less than the length of list. If obj does not
535 * occur in list, then #f (not the empty list) is returned.
536 * List must be a proper list, otherwise scm_c_memq may crash or loop
540 scm_c_memq (SCM obj
, SCM list
)
542 for (; !SCM_NULL_OR_NIL_P (list
); list
= SCM_CDR (list
))
544 if (SCM_EQ_P (SCM_CAR (list
), obj
))
551 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
553 "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
554 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
555 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
556 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
557 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
559 #define FUNC_NAME s_scm_memq
561 SCM_VALIDATE_LIST (2, lst
);
562 return scm_c_memq (x
, lst
);
567 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
569 "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
570 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
571 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
572 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
573 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
575 #define FUNC_NAME s_scm_memv
577 SCM_VALIDATE_LIST (2, lst
);
578 for (; !SCM_NULL_OR_NIL_P (lst
); lst
= SCM_CDR (lst
))
580 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
588 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
590 "Return the first sublist of @var{lst} whose car is\n"
591 "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
592 "the non-empty lists returned by @code{(list-tail @var{lst}\n"
593 "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
594 "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
595 "empty list) is returned.")
596 #define FUNC_NAME s_scm_member
598 SCM_VALIDATE_LIST (2, lst
);
599 for (; !SCM_NULL_OR_NIL_P (lst
); lst
= SCM_CDR (lst
))
601 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
609 /* deleting elements from a list (delq, etc.) */
611 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
613 "@deffnx {Scheme Procedure} delv! item lst\n"
614 "@deffnx {Scheme Procedure} delete! item lst\n"
615 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
616 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
617 "rather than creating a new list. Caveat evaluator: Like other\n"
618 "destructive list functions, these functions cannot modify the binding of\n"
619 "@var{lst}, and so cannot be used to delete the first element of\n"
620 "@var{lst} destructively.")
621 #define FUNC_NAME s_scm_delq_x
626 for (prev
= &lst
, walk
= lst
;
628 walk
= SCM_CDR (walk
))
630 if (SCM_EQ_P (SCM_CAR (walk
), item
))
631 *prev
= SCM_CDR (walk
);
633 prev
= SCM_CDRLOC (walk
);
641 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
643 "Destructively remove all elements from @var{lst} that are\n"
644 "@code{eqv?} to @var{item}.")
645 #define FUNC_NAME s_scm_delv_x
650 for (prev
= &lst
, walk
= lst
;
652 walk
= SCM_CDR (walk
))
654 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
655 *prev
= SCM_CDR (walk
);
657 prev
= SCM_CDRLOC (walk
);
666 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
668 "Destructively remove all elements from @var{lst} that are\n"
669 "@code{equal?} to @var{item}.")
670 #define FUNC_NAME s_scm_delete_x
675 for (prev
= &lst
, walk
= lst
;
677 walk
= SCM_CDR (walk
))
679 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
680 *prev
= SCM_CDR (walk
);
682 prev
= SCM_CDRLOC (walk
);
693 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
695 "Return a newly-created copy of @var{lst} with elements\n"
696 "@code{eq?} to @var{item} removed. This procedure mirrors\n"
697 "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
698 "@var{item} with @code{eq?}.")
699 #define FUNC_NAME s_scm_delq
701 SCM copy
= scm_list_copy (lst
);
702 return scm_delq_x (item
, copy
);
706 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
708 "Return a newly-created copy of @var{lst} with elements\n"
709 "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
710 "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
711 "@var{item} with @code{eqv?}.")
712 #define FUNC_NAME s_scm_delv
714 SCM copy
= scm_list_copy (lst
);
715 return scm_delv_x (item
, copy
);
719 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
721 "Return a newly-created copy of @var{lst} with elements\n"
722 "@code{equal?} to @var{item} removed. This procedure mirrors\n"
723 "@code{member}: @code{delete} compares elements of @var{lst}\n"
724 "against @var{item} with @code{equal?}.")
725 #define FUNC_NAME s_scm_delete
727 SCM copy
= scm_list_copy (lst
);
728 return scm_delete_x (item
, copy
);
733 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
735 "Like @code{delq!}, but only deletes the first occurrence of\n"
736 "@var{item} from @var{lst}. Tests for equality using\n"
737 "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
738 #define FUNC_NAME s_scm_delq1_x
743 for (prev
= &lst
, walk
= lst
;
745 walk
= SCM_CDR (walk
))
747 if (SCM_EQ_P (SCM_CAR (walk
), item
))
749 *prev
= SCM_CDR (walk
);
753 prev
= SCM_CDRLOC (walk
);
761 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
763 "Like @code{delv!}, but only deletes the first occurrence of\n"
764 "@var{item} from @var{lst}. Tests for equality using\n"
765 "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
766 #define FUNC_NAME s_scm_delv1_x
771 for (prev
= &lst
, walk
= lst
;
773 walk
= SCM_CDR (walk
))
775 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
777 *prev
= SCM_CDR (walk
);
781 prev
= SCM_CDRLOC (walk
);
789 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
791 "Like @code{delete!}, but only deletes the first occurrence of\n"
792 "@var{item} from @var{lst}. Tests for equality using\n"
793 "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
794 #define FUNC_NAME s_scm_delete1_x
799 for (prev
= &lst
, walk
= lst
;
801 walk
= SCM_CDR (walk
))
803 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
805 *prev
= SCM_CDR (walk
);
809 prev
= SCM_CDRLOC (walk
);
816 SCM_DEFINE (scm_filter
, "filter", 2, 0, 0,
817 (SCM pred
, SCM list
),
818 "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n"
819 "The list is not disordered -- elements that appear in the result list occur\n"
820 "in the same order as they occur in the argument list. The returned list may\n"
821 "share a common tail with the argument list. The dynamic order in which the\n"
822 "various applications of pred are made is not specified.\n\n"
824 "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n"
826 #define FUNC_NAME s_scm_filter
828 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
832 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
833 SCM_VALIDATE_LIST (2, list
);
835 for (prev
= &res
, walk
= list
;
837 walk
= SCM_CDR (walk
))
839 if (!SCM_FALSEP (call (pred
, SCM_CAR (walk
))))
841 *prev
= scm_cons (SCM_CAR (walk
), SCM_EOL
);
842 prev
= SCM_CDRLOC (*prev
);
850 SCM_DEFINE (scm_filter_x
, "filter!", 2, 0, 0,
851 (SCM pred
, SCM list
),
852 "Linear-update variant of @code{filter}.")
853 #define FUNC_NAME s_scm_filter_x
855 scm_t_trampoline_1 call
= scm_trampoline_1 (pred
);
858 SCM_ASSERT (call
, pred
, 1, FUNC_NAME
);
859 SCM_VALIDATE_LIST (2, list
);
861 for (prev
= &list
, walk
= list
;
863 walk
= SCM_CDR (walk
))
865 if (!SCM_FALSEP (call (pred
, SCM_CAR (walk
))))
866 prev
= SCM_CDRLOC (walk
);
868 *prev
= SCM_CDR (walk
);
879 #include "libguile/list.x"