1 /* Copyright (C) 1995,1996,1997 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 */
47 #include "libguile/_scm.h"
48 #include "libguile/eq.h"
50 #include "libguile/validate.h"
51 #include "libguile/list.h"
55 #define var_start(x, y) va_start(x, y)
58 #define var_start(x, y) va_start(x)
65 scm_listify (SCM elt
, ...)
72 while (! SCM_UNBNDP (elt
))
74 *pos
= scm_cons (elt
, SCM_EOL
);
75 pos
= SCM_CDRLOC (*pos
);
76 elt
= va_arg (foo
, SCM
);
82 SCM_DEFINE (scm_list
, "list", 0, 0, 1,
84 "Return a list containing OBJS, the arguments to `list'.")
85 #define FUNC_NAME s_scm_list
92 SCM_DEFINE (scm_list_star
, "list*", 1, 0, 1,
94 "Return an improper list of the arguments.")
95 #define FUNC_NAME s_scm_list_star
97 SCM_VALIDATE_REST_ARGUMENT (rest
);
98 if (!SCM_NULLP (rest
))
100 SCM prev
= arg
= scm_cons (arg
, rest
);
101 while (SCM_NNULLP (SCM_CDR (rest
)))
104 rest
= SCM_CDR (rest
);
106 SCM_SETCDR (prev
, SCM_CAR (rest
));
114 /* general questions about lists --- null?, list?, length, etc. */
116 SCM_DEFINE (scm_null_p
, "null?", 1, 0, 0,
118 "Return #t iff X is the empty list, else #f.")
119 #define FUNC_NAME s_scm_null_p
121 return SCM_BOOL (SCM_NULLP (x
));
126 SCM_DEFINE (scm_list_p
, "list?", 1, 0, 0,
128 "Return #t iff X is a proper list, else #f.")
129 #define FUNC_NAME s_scm_list_p
131 return SCM_BOOL (scm_ilength (x
) >= 0);
136 /* Return the length of SX, or -1 if it's not a proper list.
137 This uses the "tortoise and hare" algorithm to detect "infinitely
138 long" lists (i.e. lists with cycles in their cdrs), and returns -1
139 if it does find one. */
148 if (SCM_NULLP(hare
)) return i
;
149 if (SCM_NCONSP(hare
)) return -1;
150 hare
= SCM_CDR(hare
);
152 if (SCM_NULLP(hare
)) return i
;
153 if (SCM_NCONSP(hare
)) return -1;
154 hare
= SCM_CDR(hare
);
156 /* For every two steps the hare takes, the tortoise takes one. */
157 tortoise
= SCM_CDR(tortoise
);
159 while (! SCM_EQ_P (hare
, tortoise
));
161 /* If the tortoise ever catches the hare, then the list must contain
167 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
169 "Return the number of elements in list LST.")
170 #define FUNC_NAME s_scm_length
173 SCM_VALIDATE_LIST_COPYLEN (1,lst
,i
);
174 return SCM_MAKINUM (i
);
180 /* appending lists */
182 SCM_DEFINE (scm_append
, "append", 0, 0, 1,
184 "Returns a list consisting of the elements of the first LIST\n"
185 "followed by the elements of the other LISTs.\n"
187 " (append '(x) '(y)) => (x y)\n"
188 " (append '(a) '(b c d)) => (a b c d)\n"
189 " (append '(a (b)) '((c))) => (a (b) (c))\n"
191 "The resulting list is always newly allocated, except that it shares\n"
192 "structure with the last LIST argument. The last argument may\n"
193 "actually be any object; an improper list results if the last\n"
194 "argument is not a proper list.\n"
196 " (append '(a b) '(c . d)) => (a b c . d)\n"
197 " (append '() 'a) => a\n")
198 #define FUNC_NAME s_scm_append
200 SCM_VALIDATE_REST_ARGUMENT (args
);
201 if (SCM_NULLP (args
)) {
206 SCM arg
= SCM_CAR (args
);
207 args
= SCM_CDR (args
);
208 while (!SCM_NULLP (args
)) {
209 while (SCM_CONSP (arg
)) {
210 *lloc
= scm_cons (SCM_CAR (arg
), SCM_EOL
);
211 lloc
= SCM_CDRLOC (*lloc
);
214 SCM_VALIDATE_NULL (SCM_ARGn
, arg
);
215 arg
= SCM_CAR (args
);
216 args
= SCM_CDR (args
);
225 SCM_DEFINE (scm_append_x
, "append!", 0, 0, 1,
227 "A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs,\n"
228 "The Revised^4 Report on Scheme}). The cdr field of each list's final\n"
229 "pair is changed to point to the head of the next list, so no consing is\n"
230 "performed. Return a pointer to the mutated list.")
231 #define FUNC_NAME s_scm_append_x
233 SCM_VALIDATE_REST_ARGUMENT (args
);
235 if (SCM_NULLP (args
)) {
238 SCM arg
= SCM_CAR (args
);
239 args
= SCM_CDR (args
);
240 if (SCM_NULLP (args
)) {
242 } else if (!SCM_NULLP (arg
)) {
243 SCM_VALIDATE_CONS (SCM_ARG1
, arg
);
244 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (args
));
253 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
255 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
256 "@var{lst} is circular.")
257 #define FUNC_NAME s_scm_last_pair
265 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
267 SCM ahead
= SCM_CDR(hare
);
268 if (SCM_NCONSP(ahead
)) return hare
;
270 ahead
= SCM_CDR(hare
);
271 if (SCM_NCONSP(ahead
)) return hare
;
273 tortoise
= SCM_CDR(tortoise
);
275 while (! SCM_EQ_P (hare
, tortoise
));
276 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
281 /* reversing lists */
283 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
285 "Return a new list that contains the elements of LST but in reverse order.")
286 #define FUNC_NAME s_scm_reverse
288 SCM result
= SCM_EOL
;
293 if (SCM_NULLP(hare
)) return result
;
294 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
295 result
= scm_cons (SCM_CAR (hare
), result
);
296 hare
= SCM_CDR (hare
);
297 if (SCM_NULLP(hare
)) return result
;
298 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
299 result
= scm_cons (SCM_CAR (hare
), result
);
300 hare
= SCM_CDR (hare
);
301 tortoise
= SCM_CDR (tortoise
);
303 while (! SCM_EQ_P (hare
, tortoise
));
304 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
308 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
309 (SCM lst
, SCM new_tail
),
310 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
311 "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
312 "modified to point to the previous list element. Return a pointer to the\n"
313 "head of the reversed list.\n\n"
314 "Caveat: because the list is modified in place, the tail of the original\n"
315 "list now becomes its head, and the head of the original list now becomes\n"
316 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
317 "original list was bound now points to the tail. To ensure that the head\n"
318 "of the modified list is not lost, it is wise to save the return value of\n"
320 #define FUNC_NAME s_scm_reverse_x
322 SCM_ASSERT (scm_ilength (lst
) >= 0, lst
, SCM_ARG1
, FUNC_NAME
);
323 if (SCM_UNBNDP (new_tail
))
326 SCM_ASSERT (scm_ilength (new_tail
) >= 0, new_tail
, SCM_ARG2
, FUNC_NAME
);
328 while (SCM_NNULLP (lst
))
330 SCM old_tail
= SCM_CDR (lst
);
331 SCM_SETCDR (lst
, new_tail
);
341 /* indexing lists by element number */
343 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
345 "Return the Kth element from list LST.")
346 #define FUNC_NAME s_scm_list_ref
349 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
351 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
355 SCM_ASSERT(SCM_CONSP(lst
),
356 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
361 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
362 (SCM lst
, SCM k
, SCM val
),
363 "Set the @var{k}th element of @var{lst} to @var{val}.")
364 #define FUNC_NAME s_scm_list_set_x
367 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
369 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
373 SCM_ASSERT(SCM_CONSP(lst
),
374 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
375 SCM_SETCAR (lst
, val
);
381 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
383 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
385 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
386 "The first element of the list is considered to be element 0.\n\n"
387 "@code{list-cdr-ref} and @code{list-tail} are identical. It may help to\n"
388 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
389 "or returning the results of cdring @var{k} times down @var{lst}.")
390 #define FUNC_NAME s_scm_list_tail
393 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
395 SCM_VALIDATE_CONS (1,lst
);
403 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
404 (SCM lst
, SCM k
, SCM val
),
405 "Set the @var{k}th cdr of @var{lst} to @var{val}.")
406 #define FUNC_NAME s_scm_list_cdr_set_x
409 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
411 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
415 SCM_ASSERT(SCM_CONSP(lst
),
416 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
417 SCM_SETCDR (lst
, val
);
424 /* copying lists, perhaps partially */
426 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
428 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
430 #define FUNC_NAME s_scm_list_head
436 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
441 SCM_VALIDATE_CONS (1,lst
);
442 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
443 pos
= SCM_CDRLOC (*pos
);
451 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
453 "Return a (newly-created) copy of @var{lst}.")
454 #define FUNC_NAME s_scm_list_copy
464 while (SCM_CONSP (from_here
))
467 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
469 fill_here
= SCM_CDRLOC (c
);
470 from_here
= SCM_CDR (from_here
);
477 /* membership tests (memq, memv, etc.) */
479 SCM_DEFINE (scm_sloppy_memq
, "sloppy-memq", 2, 0, 0,
481 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
482 "Its use is recommended only in writing Guile internals,\n"
483 "not for high-level Scheme programs.")
484 #define FUNC_NAME s_scm_sloppy_memq
486 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
488 if (SCM_EQ_P (SCM_CAR (lst
), x
))
496 SCM_DEFINE (scm_sloppy_memv
, "sloppy-memv", 2, 0, 0,
498 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
499 "Its use is recommended only in writing Guile internals,\n"
500 "not for high-level Scheme programs.")
501 #define FUNC_NAME s_scm_sloppy_memv
503 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
505 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
513 SCM_DEFINE (scm_sloppy_member
, "sloppy-member", 2, 0, 0,
515 "This procedure behaves like @code{member}, 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_member
520 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
522 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
531 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
533 "Return the first sublist of LST whose car is `eq?' to X\n"
534 "where the sublists of LST are the non-empty lists returned\n"
535 "by `(list-tail LST K)' for K less than the length of LST. If\n"
536 "X does not occur in LST, then `#f' (not the empty list) is\n"
538 #define FUNC_NAME s_scm_memq
541 SCM_VALIDATE_LIST (2,lst
);
542 answer
= scm_sloppy_memq (x
, lst
);
543 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
549 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
551 "Return the first sublist of LST whose car is `eqv?' to X\n"
552 "where the sublists of LST are the non-empty lists returned\n"
553 "by `(list-tail LST K)' for K less than the length of LST. If\n"
554 "X does not occur in LST, then `#f' (not the empty list) is\n"
556 #define FUNC_NAME s_scm_memv
559 SCM_VALIDATE_LIST (2,lst
);
560 answer
= scm_sloppy_memv (x
, lst
);
561 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
566 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
568 "Return the first sublist of LST whose car is `equal?' to X\n"
569 "where the sublists of LST are the non-empty lists returned\n"
570 "by `(list-tail LST K)' for K less than the length of LST. If\n"
571 "X does not occur in LST, then `#f' (not the empty list) is\n"
573 #define FUNC_NAME s_scm_member
576 SCM_VALIDATE_LIST (2,lst
);
577 answer
= scm_sloppy_member (x
, lst
);
578 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
584 /* deleting elements from a list (delq, etc.) */
586 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
588 "@deffnx primitive delv! item lst\n"
589 "@deffnx primitive delete! item lst\n"
590 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
591 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
592 "rather than creating a new list. Caveat evaluator: Like other\n"
593 "destructive list functions, these functions cannot modify the binding of\n"
594 "@var{lst}, and so cannot be used to delete the first element of\n"
595 "@var{lst} destructively.")
596 #define FUNC_NAME s_scm_delq_x
601 for (prev
= &lst
, walk
= lst
;
603 walk
= SCM_CDR (walk
))
605 if (SCM_EQ_P (SCM_CAR (walk
), item
))
606 *prev
= SCM_CDR (walk
);
608 prev
= SCM_CDRLOC (walk
);
616 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
618 "Destructively remove all elements from LST that are `eqv?' to ITEM.")
619 #define FUNC_NAME s_scm_delv_x
624 for (prev
= &lst
, walk
= lst
;
626 walk
= SCM_CDR (walk
))
628 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
629 *prev
= SCM_CDR (walk
);
631 prev
= SCM_CDRLOC (walk
);
640 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
642 "Destructively remove all elements from LST that are `equal?' to ITEM.")
643 #define FUNC_NAME s_scm_delete_x
648 for (prev
= &lst
, walk
= lst
;
650 walk
= SCM_CDR (walk
))
652 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
653 *prev
= SCM_CDR (walk
);
655 prev
= SCM_CDRLOC (walk
);
666 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
668 "Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed.\n"
669 "This procedure mirrors @code{memq}:\n"
670 "@code{delq} compares elements of @var{lst} against @var{item} with\n"
672 #define FUNC_NAME s_scm_delq
674 SCM copy
= scm_list_copy (lst
);
675 return scm_delq_x (item
, copy
);
679 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
681 "Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed.\n"
682 "This procedure mirrors @code{memv}:\n"
683 "@code{delv} compares elements of @var{lst} against @var{item} with\n"
685 #define FUNC_NAME s_scm_delv
687 SCM copy
= scm_list_copy (lst
);
688 return scm_delv_x (item
, copy
);
692 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
694 "Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed.\n"
695 "This procedure mirrors @code{member}:\n"
696 "@code{delete} compares elements of @var{lst} against @var{item} with\n"
698 #define FUNC_NAME s_scm_delete
700 SCM copy
= scm_list_copy (lst
);
701 return scm_delete_x (item
, copy
);
706 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
708 "Like `delq!', but only deletes the first occurrence of ITEM from LST.\n"
709 "Tests for equality using `eq?'. See also `delv1!' and `delete1!'.")
710 #define FUNC_NAME s_scm_delq1_x
715 for (prev
= &lst
, walk
= lst
;
717 walk
= SCM_CDR (walk
))
719 if (SCM_EQ_P (SCM_CAR (walk
), item
))
721 *prev
= SCM_CDR (walk
);
725 prev
= SCM_CDRLOC (walk
);
733 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
735 "Like `delv!', but only deletes the first occurrence of ITEM from LST.\n"
736 "Tests for equality using `eqv?'. See also `delq1!' and `delete1!'.")
737 #define FUNC_NAME s_scm_delv1_x
742 for (prev
= &lst
, walk
= lst
;
744 walk
= SCM_CDR (walk
))
746 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
748 *prev
= SCM_CDR (walk
);
752 prev
= SCM_CDRLOC (walk
);
760 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
762 "Like `delete!', but only deletes the first occurrence of ITEM from LST.\n"
763 "Tests for equality using `equal?'. See also `delq1!' and `delv1!'.")
764 #define FUNC_NAME s_scm_delete1_x
769 for (prev
= &lst
, walk
= lst
;
771 walk
= SCM_CDR (walk
))
773 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
775 *prev
= SCM_CDR (walk
);
779 prev
= SCM_CDRLOC (walk
);
791 #include "libguile/list.x"