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\n"
240 "Lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field\n"
241 "of each list's final pair is changed to point to the head of\n"
242 "the next list, so no consing is performed. Return a pointer to\n"
244 #define FUNC_NAME s_scm_append_x
246 SCM_VALIDATE_REST_ARGUMENT (lists
);
248 if (SCM_NULLP (lists
)) {
251 SCM arg
= SCM_CAR (lists
);
252 lists
= SCM_CDR (lists
);
253 if (SCM_NULLP (lists
)) {
255 } else if (!SCM_NULLP (arg
)) {
256 SCM_VALIDATE_CONS (SCM_ARG1
, arg
);
257 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (lists
));
266 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
268 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
269 "@var{lst} is circular.")
270 #define FUNC_NAME s_scm_last_pair
278 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
280 SCM ahead
= SCM_CDR(hare
);
281 if (SCM_NCONSP(ahead
)) return hare
;
283 ahead
= SCM_CDR(hare
);
284 if (SCM_NCONSP(ahead
)) return hare
;
286 tortoise
= SCM_CDR(tortoise
);
288 while (! SCM_EQ_P (hare
, tortoise
));
289 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
294 /* reversing lists */
296 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
298 "Return a new list that contains the elements of @var{lst} but\n"
300 #define FUNC_NAME s_scm_reverse
302 SCM result
= SCM_EOL
;
307 if (SCM_NULLP(hare
)) return result
;
308 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
309 result
= scm_cons (SCM_CAR (hare
), result
);
310 hare
= SCM_CDR (hare
);
311 if (SCM_NULLP(hare
)) return result
;
312 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
313 result
= scm_cons (SCM_CAR (hare
), result
);
314 hare
= SCM_CDR (hare
);
315 tortoise
= SCM_CDR (tortoise
);
317 while (! SCM_EQ_P (hare
, tortoise
));
318 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
322 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
323 (SCM lst
, SCM new_tail
),
324 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n"
325 "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
326 "modified to point to the previous list element. Return a pointer to the\n"
327 "head of the reversed list.\n\n"
328 "Caveat: because the list is modified in place, the tail of the original\n"
329 "list now becomes its head, and the head of the original list now becomes\n"
330 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
331 "original list was bound now points to the tail. To ensure that the head\n"
332 "of the modified list is not lost, it is wise to save the return value of\n"
334 #define FUNC_NAME s_scm_reverse_x
336 SCM_VALIDATE_LIST (1, lst
);
337 if (SCM_UNBNDP (new_tail
))
340 SCM_VALIDATE_LIST (2, new_tail
);
342 while (SCM_NNULLP (lst
))
344 SCM old_tail
= SCM_CDR (lst
);
345 SCM_SETCDR (lst
, new_tail
);
355 /* indexing lists by element number */
357 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
359 "Return the @var{k}th element from @var{list}.")
360 #define FUNC_NAME s_scm_list_ref
363 register scm_bits_t i
;
364 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
365 while (SCM_CONSP (lst
)) {
367 return SCM_CAR (lst
);
374 SCM_OUT_OF_RANGE (2, k
);
376 SCM_WRONG_TYPE_ARG (1, list
);
381 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
382 (SCM list
, SCM k
, SCM val
),
383 "Set the @var{k}th element of @var{list} to @var{val}.")
384 #define FUNC_NAME s_scm_list_set_x
387 register scm_bits_t i
;
388 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
389 while (SCM_CONSP (lst
)) {
391 SCM_SETCAR (lst
, val
);
399 SCM_OUT_OF_RANGE (2, k
);
401 SCM_WRONG_TYPE_ARG (1, list
);
406 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
408 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
410 "@deffnx primitive list-cdr-ref lst k\n"
411 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
412 "The first element of the list is considered to be element 0.\n\n"
413 "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
414 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
415 "or returning the results of cdring @var{k} times down @var{lst}.")
416 #define FUNC_NAME s_scm_list_tail
418 register scm_bits_t i
;
419 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
421 SCM_VALIDATE_CONS (1,lst
);
429 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
430 (SCM list
, SCM k
, SCM val
),
431 "Set the @var{k}th cdr of @var{list} to @var{val}.")
432 #define FUNC_NAME s_scm_list_cdr_set_x
436 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
437 while (SCM_CONSP (lst
)) {
439 SCM_SETCDR (lst
, val
);
447 SCM_OUT_OF_RANGE (2, k
);
449 SCM_WRONG_TYPE_ARG (1, list
);
455 /* copying lists, perhaps partially */
457 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
459 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
461 #define FUNC_NAME s_scm_list_head
465 register scm_bits_t i
;
467 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
472 SCM_VALIDATE_CONS (1,lst
);
473 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
474 pos
= SCM_CDRLOC (*pos
);
482 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
484 "Return a (newly-created) copy of @var{lst}.")
485 #define FUNC_NAME s_scm_list_copy
491 SCM_VALIDATE_LIST (1, lst
);
497 while (SCM_CONSP (from_here
))
500 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
502 fill_here
= SCM_CDRLOC (c
);
503 from_here
= SCM_CDR (from_here
);
510 /* membership tests (memq, memv, etc.) */
512 #if SCM_DEBUG_DEPRECATED == 0
514 SCM_DEFINE (scm_sloppy_memq
, "sloppy-memq", 2, 0, 0,
516 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
517 "Its use is recommended only in writing Guile internals,\n"
518 "not for high-level Scheme programs.")
519 #define FUNC_NAME s_scm_sloppy_memq
521 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
523 if (SCM_EQ_P (SCM_CAR (lst
), x
))
531 SCM_DEFINE (scm_sloppy_memv
, "sloppy-memv", 2, 0, 0,
533 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
534 "Its use is recommended only in writing Guile internals,\n"
535 "not for high-level Scheme programs.")
536 #define FUNC_NAME s_scm_sloppy_memv
538 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
540 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
548 SCM_DEFINE (scm_sloppy_member
, "sloppy-member", 2, 0, 0,
550 "This procedure behaves like @code{member}, but does no type or error checking.\n"
551 "Its use is recommended only in writing Guile internals,\n"
552 "not for high-level Scheme programs.")
553 #define FUNC_NAME s_scm_sloppy_member
555 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
557 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
564 #endif /* DEPRECATED */
566 /* The function scm_c_memq returns the first sublist of list whose car is
567 * 'eq?' obj, where the sublists of list are the non-empty lists returned by
568 * (list-tail list k) for k less than the length of list. If obj does not
569 * occur in list, then #f (not the empty list) is returned.
570 * List must be a proper list, otherwise scm_c_memq may crash or loop
574 scm_c_memq (SCM obj
, SCM list
)
576 for (; !SCM_NULLP (list
); list
= SCM_CDR (list
))
578 if (SCM_EQ_P (SCM_CAR (list
), obj
))
585 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
587 "Return the first sublist of @var{lst} whose car is @code{eq?}\n"
588 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
589 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
590 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
591 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
593 #define FUNC_NAME s_scm_memq
595 SCM_VALIDATE_LIST (2, lst
);
596 return scm_c_memq (x
, lst
);
602 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
604 "Return the first sublist of @var{lst} whose car is @code{eqv?}\n"
605 "to @var{x} where the sublists of @var{lst} are the non-empty\n"
606 "lists returned by @code{(list-tail @var{lst} @var{k})} for\n"
607 "@var{k} less than the length of @var{lst}. If @var{x} does not\n"
608 "occur in @var{lst}, then @code{#f} (not the empty list) is\n"
610 #define FUNC_NAME s_scm_memv
612 SCM_VALIDATE_LIST (2, lst
);
613 for (; !SCM_NULLP (lst
); lst
= SCM_CDR (lst
))
615 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
623 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
625 "Return the first sublist of @var{lst} whose car is\n"
626 "@code{equal?} to @var{x} where the sublists of @var{lst} are\n"
627 "the non-empty lists returned by @code{(list-tail @var{lst}\n"
628 "@var{k})} for @var{k} less than the length of @var{lst}. If\n"
629 "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n"
630 "empty list) is returned.")
631 #define FUNC_NAME s_scm_member
633 SCM_VALIDATE_LIST (2, lst
);
634 for (; !SCM_NULLP (lst
); lst
= SCM_CDR (lst
))
636 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
645 /* deleting elements from a list (delq, etc.) */
647 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
649 "@deffnx primitive delv! item lst\n"
650 "@deffnx primitive delete! item lst\n"
651 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
652 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
653 "rather than creating a new list. Caveat evaluator: Like other\n"
654 "destructive list functions, these functions cannot modify the binding of\n"
655 "@var{lst}, and so cannot be used to delete the first element of\n"
656 "@var{lst} destructively.")
657 #define FUNC_NAME s_scm_delq_x
662 for (prev
= &lst
, walk
= lst
;
664 walk
= SCM_CDR (walk
))
666 if (SCM_EQ_P (SCM_CAR (walk
), item
))
667 *prev
= SCM_CDR (walk
);
669 prev
= SCM_CDRLOC (walk
);
677 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
679 "Destructively remove all elements from @var{lst} that are\n"
680 "@code{eqv?} to @var{item}.")
681 #define FUNC_NAME s_scm_delv_x
686 for (prev
= &lst
, walk
= lst
;
688 walk
= SCM_CDR (walk
))
690 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
691 *prev
= SCM_CDR (walk
);
693 prev
= SCM_CDRLOC (walk
);
702 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
704 "Destructively remove all elements from @var{lst} that are\n"
705 "@code{equal?} to @var{item}.")
706 #define FUNC_NAME s_scm_delete_x
711 for (prev
= &lst
, walk
= lst
;
713 walk
= SCM_CDR (walk
))
715 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
716 *prev
= SCM_CDR (walk
);
718 prev
= SCM_CDRLOC (walk
);
729 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
731 "Return a newly-created copy of @var{lst} with elements\n"
732 "@code{eq?} to @var{item} removed. This procedure mirrors\n"
733 "@code{memq}: @code{delq} compares elements of @var{lst} against\n"
734 "@var{item} with @code{eq?}.")
735 #define FUNC_NAME s_scm_delq
737 SCM copy
= scm_list_copy (lst
);
738 return scm_delq_x (item
, copy
);
742 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
744 "Return a newly-created copy of @var{lst} with elements\n"
745 "@code{eqv?} to @var{item} removed. This procedure mirrors\n"
746 "@code{memv}: @code{delv} compares elements of @var{lst} against\n"
747 "@var{item} with @code{eqv?}.")
748 #define FUNC_NAME s_scm_delv
750 SCM copy
= scm_list_copy (lst
);
751 return scm_delv_x (item
, copy
);
755 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
757 "Return a newly-created copy of @var{lst} with elements\n"
758 "@code{equal?} to @var{item} removed. This procedure mirrors\n"
759 "@code{member}: @code{delete} compares elements of @var{lst}\n"
760 "against @var{item} with @code{equal?}.")
761 #define FUNC_NAME s_scm_delete
763 SCM copy
= scm_list_copy (lst
);
764 return scm_delete_x (item
, copy
);
769 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
771 "Like @code{delq!}, but only deletes the first occurrence of\n"
772 "@var{item} from @var{lst}. Tests for equality using\n"
773 "@code{eq?}. See also @code{delv1!} and @code{delete1!}.")
774 #define FUNC_NAME s_scm_delq1_x
779 for (prev
= &lst
, walk
= lst
;
781 walk
= SCM_CDR (walk
))
783 if (SCM_EQ_P (SCM_CAR (walk
), item
))
785 *prev
= SCM_CDR (walk
);
789 prev
= SCM_CDRLOC (walk
);
797 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
799 "Like @code{delv!}, but only deletes the first occurrence of\n"
800 "@var{item} from @var{lst}. Tests for equality using\n"
801 "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.")
802 #define FUNC_NAME s_scm_delv1_x
807 for (prev
= &lst
, walk
= lst
;
809 walk
= SCM_CDR (walk
))
811 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
813 *prev
= SCM_CDR (walk
);
817 prev
= SCM_CDRLOC (walk
);
825 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
827 "Like @code{delete!}, but only deletes the first occurrence of\n"
828 "@var{item} from @var{lst}. Tests for equality using\n"
829 "@code{equal?}. See also @code{delq1!} and @code{delv1!}.")
830 #define FUNC_NAME s_scm_delete1_x
835 for (prev
= &lst
, walk
= lst
;
837 walk
= SCM_CDR (walk
))
839 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
841 *prev
= SCM_CDR (walk
);
845 prev
= SCM_CDRLOC (walk
);
857 #ifndef SCM_MAGIC_SNARFER
858 #include "libguile/list.x"