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 if (SCM_NNULLP (rest
))
99 SCM prev
= arg
= scm_cons (arg
, rest
);
100 while (SCM_NNULLP (SCM_CDR (rest
)))
103 rest
= SCM_CDR (rest
);
105 SCM_SETCDR (prev
, SCM_CAR (rest
));
113 /* general questions about lists --- null?, list?, length, etc. */
115 SCM_DEFINE (scm_null_p
, "null?", 1, 0, 0,
117 "Return #t iff X is the empty list, else #f.")
118 #define FUNC_NAME s_scm_null_p
120 return SCM_BOOL (SCM_NULLP (x
));
125 SCM_DEFINE (scm_list_p
, "list?", 1, 0, 0,
127 "Return #t iff X is a proper list, else #f.")
128 #define FUNC_NAME s_scm_list_p
130 return SCM_BOOL (scm_ilength (x
) >= 0);
135 /* Return the length of SX, or -1 if it's not a proper list.
136 This uses the "tortoise and hare" algorithm to detect "infinitely
137 long" lists (i.e. lists with cycles in their cdrs), and returns -1
138 if it does find one. */
147 if (SCM_NULLP(hare
)) return i
;
148 if (SCM_NCONSP(hare
)) return -1;
149 hare
= SCM_CDR(hare
);
151 if (SCM_NULLP(hare
)) return i
;
152 if (SCM_NCONSP(hare
)) return -1;
153 hare
= SCM_CDR(hare
);
155 /* For every two steps the hare takes, the tortoise takes one. */
156 tortoise
= SCM_CDR(tortoise
);
158 while (! SCM_EQ_P (hare
, tortoise
));
160 /* If the tortoise ever catches the hare, then the list must contain
166 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
168 "Return the number of elements in list LST.")
169 #define FUNC_NAME s_scm_length
172 SCM_VALIDATE_LIST_COPYLEN (1,lst
,i
);
173 return SCM_MAKINUM (i
);
179 /* appending lists */
181 SCM_DEFINE (scm_append
, "append", 0, 0, 1,
183 "Returns a list consisting of the elements of the first LIST\n"
184 "followed by the elements of the other LISTs.\n"
186 " (append '(x) '(y)) => (x y)\n"
187 " (append '(a) '(b c d)) => (a b c d)\n"
188 " (append '(a (b)) '((c))) => (a (b) (c))\n"
190 "The resulting list is always newly allocated, except that it shares\n"
191 "structure with the last LIST argument. The last argument may\n"
192 "actually be any object; an improper list results if the last\n"
193 "argument is not a proper list.\n"
195 " (append '(a b) '(c . d)) => (a b c . d)\n"
196 " (append '() 'a) => a\n")
197 #define FUNC_NAME s_scm_append
200 SCM
*lloc
= &res
, arg
;
202 SCM_VALIDATE_NULL (SCM_ARGn
, args
);
205 SCM_VALIDATE_CONS (SCM_ARGn
, args
);
208 args
= SCM_CDR(args
);
211 SCM_VALIDATE_NULL (SCM_ARGn
, args
);
214 SCM_VALIDATE_CONS (SCM_ARGn
, args
);
215 for (; SCM_CONSP(arg
); arg
= SCM_CDR(arg
)) {
216 *lloc
= scm_cons(SCM_CAR(arg
), SCM_EOL
);
217 lloc
= SCM_CDRLOC(*lloc
);
219 SCM_VALIDATE_NULL (SCM_ARGn
, arg
);
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
235 if (SCM_NULLP(args
)) return SCM_EOL
;
237 args
= SCM_CDR(args
);
238 if (SCM_NULLP(args
)) return arg
;
239 if (SCM_NULLP(arg
)) goto tail
;
240 SCM_VALIDATE_CONS (SCM_ARG1
,arg
);
241 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (args
));
247 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
249 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
250 "@var{lst} is circular.")
251 #define FUNC_NAME s_scm_last_pair
259 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
261 SCM ahead
= SCM_CDR(hare
);
262 if (SCM_NCONSP(ahead
)) return hare
;
264 ahead
= SCM_CDR(hare
);
265 if (SCM_NCONSP(ahead
)) return hare
;
267 tortoise
= SCM_CDR(tortoise
);
269 while (! SCM_EQ_P (hare
, tortoise
));
270 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
275 /* reversing lists */
277 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
279 "Return a new list that contains the elements of LST but in reverse order.")
280 #define FUNC_NAME s_scm_reverse
282 SCM result
= SCM_EOL
;
287 if (SCM_NULLP(hare
)) return result
;
288 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
289 result
= scm_cons (SCM_CAR (hare
), result
);
290 hare
= SCM_CDR (hare
);
291 if (SCM_NULLP(hare
)) return result
;
292 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
293 result
= scm_cons (SCM_CAR (hare
), result
);
294 hare
= SCM_CDR (hare
);
295 tortoise
= SCM_CDR (tortoise
);
297 while (! SCM_EQ_P (hare
, tortoise
));
298 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
302 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
303 (SCM lst
, SCM new_tail
),
304 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
305 "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
306 "modified to point to the previous list element. Return a pointer to the\n"
307 "head of the reversed list.\n\n"
308 "Caveat: because the list is modified in place, the tail of the original\n"
309 "list now becomes its head, and the head of the original list now becomes\n"
310 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
311 "original list was bound now points to the tail. To ensure that the head\n"
312 "of the modified list is not lost, it is wise to save the return value of\n"
314 #define FUNC_NAME s_scm_reverse_x
316 SCM_ASSERT (scm_ilength (lst
) >= 0, lst
, SCM_ARG1
, FUNC_NAME
);
317 if (SCM_UNBNDP (new_tail
))
320 SCM_ASSERT (scm_ilength (new_tail
) >= 0, new_tail
, SCM_ARG2
, FUNC_NAME
);
322 while (SCM_NNULLP (lst
))
324 SCM old_tail
= SCM_CDR (lst
);
325 SCM_SETCDR (lst
, new_tail
);
335 /* indexing lists by element number */
337 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
339 "Return the Kth element from list LST.")
340 #define FUNC_NAME s_scm_list_ref
343 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
345 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
349 SCM_ASSERT(SCM_CONSP(lst
),
350 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
355 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
356 (SCM lst
, SCM k
, SCM val
),
357 "Set the @var{k}th element of @var{lst} to @var{val}.")
358 #define FUNC_NAME s_scm_list_set_x
361 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
363 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
367 SCM_ASSERT(SCM_CONSP(lst
),
368 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
369 SCM_SETCAR (lst
, val
);
375 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
377 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
379 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
380 "The first element of the list is considered to be element 0.\n\n"
381 "@code{list-cdr-ref} and @code{list-tail} are identical. It may help to\n"
382 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
383 "or returning the results of cdring @var{k} times down @var{lst}.")
384 #define FUNC_NAME s_scm_list_tail
387 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
389 SCM_VALIDATE_CONS (1,lst
);
397 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
398 (SCM lst
, SCM k
, SCM val
),
399 "Set the @var{k}th cdr of @var{lst} to @var{val}.")
400 #define FUNC_NAME s_scm_list_cdr_set_x
403 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
405 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
409 SCM_ASSERT(SCM_CONSP(lst
),
410 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
411 SCM_SETCDR (lst
, val
);
418 /* copying lists, perhaps partially */
420 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
422 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
424 #define FUNC_NAME s_scm_list_head
430 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
435 SCM_VALIDATE_CONS (1,lst
);
436 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
437 pos
= SCM_CDRLOC (*pos
);
445 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
447 "Return a (newly-created) copy of @var{lst}.")
448 #define FUNC_NAME s_scm_list_copy
458 while (SCM_CONSP (from_here
))
461 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
463 fill_here
= SCM_CDRLOC (c
);
464 from_here
= SCM_CDR (from_here
);
471 /* membership tests (memq, memv, etc.) */
473 SCM_DEFINE (scm_sloppy_memq
, "sloppy-memq", 2, 0, 0,
475 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
476 "Its use is recommended only in writing Guile internals,\n"
477 "not for high-level Scheme programs.")
478 #define FUNC_NAME s_scm_sloppy_memq
480 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
482 if (SCM_EQ_P (SCM_CAR (lst
), x
))
490 SCM_DEFINE (scm_sloppy_memv
, "sloppy-memv", 2, 0, 0,
492 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
493 "Its use is recommended only in writing Guile internals,\n"
494 "not for high-level Scheme programs.")
495 #define FUNC_NAME s_scm_sloppy_memv
497 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
499 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst
), x
)))
507 SCM_DEFINE (scm_sloppy_member
, "sloppy-member", 2, 0, 0,
509 "This procedure behaves like @code{member}, but does no type or error checking.\n"
510 "Its use is recommended only in writing Guile internals,\n"
511 "not for high-level Scheme programs.")
512 #define FUNC_NAME s_scm_sloppy_member
514 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
516 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst
), x
)))
525 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
527 "Return the first sublist of LST whose car is `eq?' to X\n"
528 "where the sublists of LST are the non-empty lists returned\n"
529 "by `(list-tail LST K)' for K less than the length of LST. If\n"
530 "X does not occur in LST, then `#f' (not the empty list) is\n"
532 #define FUNC_NAME s_scm_memq
535 SCM_VALIDATE_LIST (2,lst
);
536 answer
= scm_sloppy_memq (x
, lst
);
537 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
543 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
545 "Return the first sublist of LST whose car is `eqv?' to X\n"
546 "where the sublists of LST are the non-empty lists returned\n"
547 "by `(list-tail LST K)' for K less than the length of LST. If\n"
548 "X does not occur in LST, then `#f' (not the empty list) is\n"
550 #define FUNC_NAME s_scm_memv
553 SCM_VALIDATE_LIST (2,lst
);
554 answer
= scm_sloppy_memv (x
, lst
);
555 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
560 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
562 "Return the first sublist of LST whose car is `equal?' to X\n"
563 "where the sublists of LST are the non-empty lists returned\n"
564 "by `(list-tail LST K)' for K less than the length of LST. If\n"
565 "X does not occur in LST, then `#f' (not the empty list) is\n"
567 #define FUNC_NAME s_scm_member
570 SCM_VALIDATE_LIST (2,lst
);
571 answer
= scm_sloppy_member (x
, lst
);
572 return (SCM_NULLP (answer
)) ? SCM_BOOL_F
: answer
;
578 /* deleting elements from a list (delq, etc.) */
580 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
582 "@deffnx primitive delv! item lst\n"
583 "@deffnx primitive delete! item lst\n"
584 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
585 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
586 "rather than creating a new list. Caveat evaluator: Like other\n"
587 "destructive list functions, these functions cannot modify the binding of\n"
588 "@var{lst}, and so cannot be used to delete the first element of\n"
589 "@var{lst} destructively.")
590 #define FUNC_NAME s_scm_delq_x
595 for (prev
= &lst
, walk
= lst
;
597 walk
= SCM_CDR (walk
))
599 if (SCM_EQ_P (SCM_CAR (walk
), item
))
600 *prev
= SCM_CDR (walk
);
602 prev
= SCM_CDRLOC (walk
);
610 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
612 "Destructively remove all elements from LST that are `eqv?' to ITEM.")
613 #define FUNC_NAME s_scm_delv_x
618 for (prev
= &lst
, walk
= lst
;
620 walk
= SCM_CDR (walk
))
622 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
623 *prev
= SCM_CDR (walk
);
625 prev
= SCM_CDRLOC (walk
);
634 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
636 "Destructively remove all elements from LST that are `equal?' to ITEM.")
637 #define FUNC_NAME s_scm_delete_x
642 for (prev
= &lst
, walk
= lst
;
644 walk
= SCM_CDR (walk
))
646 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
647 *prev
= SCM_CDR (walk
);
649 prev
= SCM_CDRLOC (walk
);
660 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
662 "Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed.\n"
663 "This procedure mirrors @code{memq}:\n"
664 "@code{delq} compares elements of @var{lst} against @var{item} with\n"
666 #define FUNC_NAME s_scm_delq
668 SCM copy
= scm_list_copy (lst
);
669 return scm_delq_x (item
, copy
);
673 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
675 "Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed.\n"
676 "This procedure mirrors @code{memv}:\n"
677 "@code{delv} compares elements of @var{lst} against @var{item} with\n"
679 #define FUNC_NAME s_scm_delv
681 SCM copy
= scm_list_copy (lst
);
682 return scm_delv_x (item
, copy
);
686 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
688 "Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed.\n"
689 "This procedure mirrors @code{member}:\n"
690 "@code{delete} compares elements of @var{lst} against @var{item} with\n"
692 #define FUNC_NAME s_scm_delete
694 SCM copy
= scm_list_copy (lst
);
695 return scm_delete_x (item
, copy
);
700 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
702 "Like `delq!', but only deletes the first occurrence of ITEM from LST.\n"
703 "Tests for equality using `eq?'. See also `delv1!' and `delete1!'.")
704 #define FUNC_NAME s_scm_delq1_x
709 for (prev
= &lst
, walk
= lst
;
711 walk
= SCM_CDR (walk
))
713 if (SCM_EQ_P (SCM_CAR (walk
), item
))
715 *prev
= SCM_CDR (walk
);
719 prev
= SCM_CDRLOC (walk
);
727 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
729 "Like `delv!', but only deletes the first occurrence of ITEM from LST.\n"
730 "Tests for equality using `eqv?'. See also `delq1!' and `delete1!'.")
731 #define FUNC_NAME s_scm_delv1_x
736 for (prev
= &lst
, walk
= lst
;
738 walk
= SCM_CDR (walk
))
740 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk
), item
)))
742 *prev
= SCM_CDR (walk
);
746 prev
= SCM_CDRLOC (walk
);
754 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
756 "Like `delete!', but only deletes the first occurrence of ITEM from LST.\n"
757 "Tests for equality using `equal?'. See also `delq1!' and `delv1!'.")
758 #define FUNC_NAME s_scm_delete1_x
763 for (prev
= &lst
, walk
= lst
;
765 walk
= SCM_CDR (walk
))
767 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk
), item
)))
769 *prev
= SCM_CDR (walk
);
773 prev
= SCM_CDRLOC (walk
);
785 #include "libguile/list.x"