1 /* Copyright (C) 1995,1996,1997, 2000 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program 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
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
46 #include "libguile/_scm.h"
47 #include "libguile/eq.h"
49 #include "libguile/validate.h"
50 #include "libguile/list.h"
54 #define var_start(x, y) va_start(x, y)
57 #define var_start(x, y) va_start(x)
64 scm_listify (SCM elt
, ...)
71 while (! SCM_UNBNDP (elt
))
73 *pos
= scm_cons (elt
, SCM_EOL
);
74 pos
= SCM_CDRLOC (*pos
);
75 elt
= va_arg (foo
, SCM
);
81 SCM_DEFINE (scm_list
, "list", 0, 0, 1,
83 "Return a list containing @var{objs}, the arguments to\n"
85 #define FUNC_NAME s_scm_list
92 #if (SCM_DEBUG_DEPRECATED == 0)
94 SCM_REGISTER_PROC (s_list_star
, "list*", 1, 0, 1, scm_cons_star
);
96 #endif /* SCM_DEBUG_DEPRECATED == 0 */
98 SCM_DEFINE (scm_cons_star
, "cons*", 1, 0, 1,
100 "Like @code{list}, but the last arg provides the tail of the\n"
101 "constructed list, returning @code{(cons @var{arg1} (cons\n"
102 "@var{arg2} (cons @dots{} @var{argn}))). Requires at least one\n"
103 "argument. If given one argument, that argument is returned as\n"
104 "result. This function is called @code{list*} in some other\n"
105 "Schemes and in Common LISP.")
106 #define FUNC_NAME s_scm_cons_star
108 SCM_VALIDATE_REST_ARGUMENT (rest
);
109 if (!SCM_NULLP (rest
))
111 SCM prev
= arg
= scm_cons (arg
, rest
);
112 while (SCM_NNULLP (SCM_CDR (rest
)))
115 rest
= SCM_CDR (rest
);
117 SCM_SETCDR (prev
, SCM_CAR (rest
));
125 /* general questions about lists --- null?, list?, length, etc. */
127 SCM_DEFINE (scm_null_p
, "null?", 1, 0, 0,
129 "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
130 #define FUNC_NAME s_scm_null_p
132 return SCM_BOOL (SCM_NULLP (x
));
137 SCM_DEFINE (scm_list_p
, "list?", 1, 0, 0,
139 "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
140 #define FUNC_NAME s_scm_list_p
142 return SCM_BOOL (scm_ilength (x
) >= 0);
147 /* Return the length of SX, or -1 if it's not a proper list.
148 This uses the "tortoise and hare" algorithm to detect "infinitely
149 long" lists (i.e. lists with cycles in their cdrs), and returns -1
150 if it does find one. */
159 if (SCM_NULLP(hare
)) return i
;
160 if (SCM_NCONSP(hare
)) return -1;
161 hare
= SCM_CDR(hare
);
163 if (SCM_NULLP(hare
)) return i
;
164 if (SCM_NCONSP(hare
)) return -1;
165 hare
= SCM_CDR(hare
);
167 /* For every two steps the hare takes, the tortoise takes one. */
168 tortoise
= SCM_CDR(tortoise
);
170 while (! SCM_EQ_P (hare
, tortoise
));
172 /* If the tortoise ever catches the hare, then the list must contain
178 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
180 "Return the number of elements in list @var{lst}.")
181 #define FUNC_NAME s_scm_length
184 SCM_VALIDATE_LIST_COPYLEN (1,lst
,i
);
185 return SCM_MAKINUM (i
);
191 /* appending lists */
193 SCM_DEFINE (scm_append
, "append", 0, 0, 1,
195 "Return a list consisting of the elements the lists passed as\n"
198 "(append '(x) '(y)) @result{} (x y)\n"
199 "(append '(a) '(b c d)) @result{} (a b c d)\n"
200 "(append '(a (b)) '((c))) @result{} (a (b) (c))\n"
202 "The resulting list is always newly allocated, except that it\n"
203 "shares structure with the last list argument. The last\n"
204 "argument may actually be any object; an improper list results\n"
205 "if the last argument is not a proper list.\n"
207 "(append '(a b) '(c . d)) @result{} (a b c . d)\n"
208 "(append '() 'a) @result{} a\n"
210 #define FUNC_NAME s_scm_append
212 SCM_VALIDATE_REST_ARGUMENT (args
);
213 if (SCM_NULLP (args
)) {
218 SCM arg
= SCM_CAR (args
);
219 args
= SCM_CDR (args
);
220 while (!SCM_NULLP (args
)) {
221 while (SCM_CONSP (arg
)) {
222 *lloc
= scm_cons (SCM_CAR (arg
), SCM_EOL
);
223 lloc
= SCM_CDRLOC (*lloc
);
226 SCM_VALIDATE_NULL (SCM_ARGn
, arg
);
227 arg
= SCM_CAR (args
);
228 args
= SCM_CDR (args
);
237 SCM_DEFINE (scm_append_x
, "append!", 0, 0, 1,
239 "A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs,\n"
240 "The Revised^4 Report on Scheme}). The cdr field of each list's final\n"
241 "pair is changed to point to the head of the next list, so no consing is\n"
242 "performed. Return a pointer to the mutated list.")
243 #define FUNC_NAME s_scm_append_x
245 SCM_VALIDATE_REST_ARGUMENT (args
);
247 if (SCM_NULLP (args
)) {
250 SCM arg
= SCM_CAR (args
);
251 args
= SCM_CDR (args
);
252 if (SCM_NULLP (args
)) {
254 } else if (!SCM_NULLP (arg
)) {
255 SCM_VALIDATE_CONS (SCM_ARG1
, arg
);
256 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (args
));
265 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
267 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
268 "@var{lst} is circular.")
269 #define FUNC_NAME s_scm_last_pair
277 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
279 SCM ahead
= SCM_CDR(hare
);
280 if (SCM_NCONSP(ahead
)) return hare
;
282 ahead
= SCM_CDR(hare
);
283 if (SCM_NCONSP(ahead
)) return hare
;
285 tortoise
= SCM_CDR(tortoise
);
287 while (! SCM_EQ_P (hare
, tortoise
));
288 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
293 /* reversing lists */
295 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
297 "Return a new list that contains the elements of @var{lst} but\n"
299 #define FUNC_NAME s_scm_reverse
301 SCM result
= SCM_EOL
;
306 if (SCM_NULLP(hare
)) return result
;
307 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
308 result
= scm_cons (SCM_CAR (hare
), result
);
309 hare
= SCM_CDR (hare
);
310 if (SCM_NULLP(hare
)) return result
;
311 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
312 result
= scm_cons (SCM_CAR (hare
), result
);
313 hare
= SCM_CDR (hare
);
314 tortoise
= SCM_CDR (tortoise
);
316 while (! SCM_EQ_P (hare
, tortoise
));
317 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
321 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
322 (SCM lst
, SCM new_tail
),
323 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
324 "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
325 "modified to point to the previous list element. Return a pointer to the\n"
326 "head of the reversed list.\n\n"
327 "Caveat: because the list is modified in place, the tail of the original\n"
328 "list now becomes its head, and the head of the original list now becomes\n"
329 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
330 "original list was bound now points to the tail. To ensure that the head\n"
331 "of the modified list is not lost, it is wise to save the return value of\n"
333 #define FUNC_NAME s_scm_reverse_x
335 SCM_VALIDATE_LIST (1, lst
);
336 if (SCM_UNBNDP (new_tail
))
339 SCM_VALIDATE_LIST (2, new_tail
);
341 while (SCM_NNULLP (lst
))
343 SCM old_tail
= SCM_CDR (lst
);
344 SCM_SETCDR (lst
, new_tail
);
354 /* indexing lists by element number */
356 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
358 "Return the @var{k}th element from @var{list}.")
359 #define FUNC_NAME s_scm_list_ref
363 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
364 while (SCM_CONSP (lst
)) {
366 return SCM_CAR (lst
);
373 SCM_OUT_OF_RANGE (2, k
);
375 SCM_WRONG_TYPE_ARG (1, list
);
380 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
381 (SCM list
, SCM k
, SCM val
),
382 "Set the @var{k}th element of @var{list} to @var{val}.")
383 #define FUNC_NAME s_scm_list_set_x
387 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
388 while (SCM_CONSP (lst
)) {
390 SCM_SETCAR (lst
, val
);
398 SCM_OUT_OF_RANGE (2, k
);
400 SCM_WRONG_TYPE_ARG (1, list
);
405 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
407 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
409 "@deffnx primitive list-cdr-ref lst k\n"
410 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
411 "The first element of the list is considered to be element 0.\n\n"
412 "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
413 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
414 "or returning the results of cdring @var{k} times down @var{lst}.")
415 #define FUNC_NAME s_scm_list_tail
418 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
420 SCM_VALIDATE_CONS (1,lst
);
428 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
429 (SCM list
, SCM k
, SCM val
),
430 "Set the @var{k}th cdr of @var{list} to @var{val}.")
431 #define FUNC_NAME s_scm_list_cdr_set_x
435 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
436 while (SCM_CONSP (lst
)) {
438 SCM_SETCDR (lst
, val
);
446 SCM_OUT_OF_RANGE (2, k
);
448 SCM_WRONG_TYPE_ARG (1, list
);
454 /* copying lists, perhaps partially */
456 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
458 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
460 #define FUNC_NAME s_scm_list_head
466 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
471 SCM_VALIDATE_CONS (1,lst
);
472 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
473 pos
= SCM_CDRLOC (*pos
);
481 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
483 "Return a (newly-created) copy of @var{lst}.")
484 #define FUNC_NAME s_scm_list_copy
490 SCM_VALIDATE_LIST (1, lst
);
496 while (SCM_CONSP (from_here
))
499 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
501 fill_here
= SCM_CDRLOC (c
);
502 from_here
= SCM_CDR (from_here
);
509 /* membership tests (memq, memv, etc.) */
511 #if SCM_DEBUG_DEPRECATED == 0
513 SCM_DEFINE (scm_sloppy_memq
, "sloppy-memq", 2, 0, 0,
515 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
516 "Its use is recommended only in writing Guile internals,\n"
517 "not for high-level Scheme programs.")
518 #define FUNC_NAME s_scm_sloppy_memq
520 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
522 if (SCM_EQ_P (SCM_CAR (lst
), x
))
530 SCM_DEFINE (scm_sloppy_memv
, "sloppy-memv", 2, 0, 0,
532 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
533 "Its use is recommended only in writing Guile internals,\n"
534 "not for high-level Scheme programs.")
535 #define FUNC_NAME s_scm_sloppy_memv
537 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
539 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
547 SCM_DEFINE (scm_sloppy_member
, "sloppy-member", 2, 0, 0,
549 "This procedure behaves like @code{member}, but does no type or error checking.\n"
550 "Its use is recommended only in writing Guile internals,\n"
551 "not for high-level Scheme programs.")
552 #define FUNC_NAME s_scm_sloppy_member
554 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
556 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
563 #endif /* DEPRECATED */
565 /* The function scm_c_memq returns the first sublist of list whose car is
566 * 'eq?' obj, where the sublists of list are the non-empty lists returned by
567 * (list-tail list k) for k less than the length of list. If obj does not
568 * occur in list, then #f (not the empty list) is returned. (r5rs)
569 * List must be a proper list, otherwise scm_c_memq may crash or loop
573 scm_c_memq (SCM obj
, SCM list
)
575 for (; !SCM_NULLP (list
); list
= SCM_CDR (list
))
577 if (SCM_EQ_P (SCM_CAR (list
), obj
))
584 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
586 "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
587 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
588 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
589 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
590 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
592 #define FUNC_NAME s_scm_memq
594 SCM_VALIDATE_LIST (2, lst
);
595 return scm_c_memq (x
, lst
);
601 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
603 "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
604 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
605 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
606 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
607 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
609 #define FUNC_NAME s_scm_memv
611 SCM_VALIDATE_LIST (2, lst
);
612 for (; !SCM_NULLP (lst
); lst
= SCM_CDR (lst
))
614 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
622 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
624 "Return the first sublist of @var{lst} whose car is\n"
625 "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
626 "the non-empty lists returned by @code{(list-tail @var{lst}\n"
627 "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
628 "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
629 "empty list) is returned.")
630 #define FUNC_NAME s_scm_member
632 SCM_VALIDATE_LIST (2, lst
);
633 for (; !SCM_NULLP (lst
); lst
= SCM_CDR (lst
))
635 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
644 /* deleting elements from a list (delq, etc.) */
646 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
648 "@deffnx primitive delv! item lst\n"
649 "@deffnx primitive delete! item lst\n"
650 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
651 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
652 "rather than creating a new list. Caveat evaluator: Like other\n"
653 "destructive list functions, these functions cannot modify the binding of\n"
654 "@var{lst}, and so cannot be used to delete the first element of\n"
655 "@var{lst} destructively.")
656 #define FUNC_NAME s_scm_delq_x
661 for (prev
= &lst
, walk
= lst
;
663 walk
= SCM_CDR (walk
))
665 if (SCM_EQ_P (SCM_CAR (walk
), item
))
666 *prev
= SCM_CDR (walk
);
668 prev
= SCM_CDRLOC (walk
);
676 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
678 "Destructively remove all elements from @var{lst} that are\n"
679 "@code{eqv?} to @var{item}.")
680 #define FUNC_NAME s_scm_delv_x
685 for (prev
= &lst
, walk
= lst
;
687 walk
= SCM_CDR (walk
))
689 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
690 *prev
= SCM_CDR (walk
);
692 prev
= SCM_CDRLOC (walk
);
701 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
703 "Destructively remove all elements from @var{lst} that are\n"
704 "@code{equal?} to @var{item}.")
705 #define FUNC_NAME s_scm_delete_x
710 for (prev
= &lst
, walk
= lst
;
712 walk
= SCM_CDR (walk
))
714 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
715 *prev
= SCM_CDR (walk
);
717 prev
= SCM_CDRLOC (walk
);
728 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
730 "Return a newly-created copy of @var{lst} with elements\n"
731 "@code{eq?} to @var{item} removed. This procedure mirrors\n"
732 "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
733 "@var{item} with @code{eq?}.")
734 #define FUNC_NAME s_scm_delq
736 SCM copy
= scm_list_copy (lst
);
737 return scm_delq_x (item
, copy
);
741 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
743 "Return a newly-created copy of @var{lst} with elements\n"
744 "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
745 "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
746 "@var{item} with @code{eqv?}.")
747 #define FUNC_NAME s_scm_delv
749 SCM copy
= scm_list_copy (lst
);
750 return scm_delv_x (item
, copy
);
754 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
756 "Return a newly-created copy of @var{lst} with elements\n"
757 "@code{equal?} to @var{item} removed. This procedure mirrors\n"
758 "@code{member}: @code{delete} compares elements of @var{lst}\n"
759 "against @var{item} with @code{equal?}.")
760 #define FUNC_NAME s_scm_delete
762 SCM copy
= scm_list_copy (lst
);
763 return scm_delete_x (item
, copy
);
768 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
770 "Like @code{delq!}, but only deletes the first occurrence of\n"
771 "@var{item} from @var{lst}. Tests for equality using\n"
772 "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
773 #define FUNC_NAME s_scm_delq1_x
778 for (prev
= &lst
, walk
= lst
;
780 walk
= SCM_CDR (walk
))
782 if (SCM_EQ_P (SCM_CAR (walk
), item
))
784 *prev
= SCM_CDR (walk
);
788 prev
= SCM_CDRLOC (walk
);
796 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
798 "Like @code{delv!}, but only deletes the first occurrence of\n"
799 "@var{item} from @var{lst}. Tests for equality using\n"
800 "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
801 #define FUNC_NAME s_scm_delv1_x
806 for (prev
= &lst
, walk
= lst
;
808 walk
= SCM_CDR (walk
))
810 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
812 *prev
= SCM_CDR (walk
);
816 prev
= SCM_CDRLOC (walk
);
824 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
826 "Like @code{delete!}, but only deletes the first occurrence of\n"
827 "@var{item} from @var{lst}. Tests for equality using\n"
828 "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
829 #define FUNC_NAME s_scm_delete1_x
834 for (prev
= &lst
, walk
= lst
;
836 walk
= SCM_CDR (walk
))
838 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
840 *prev
= SCM_CDR (walk
);
844 prev
= SCM_CDRLOC (walk
);
856 #ifndef SCM_MAGIC_SNARFER
857 #include "libguile/list.x"