916d0eb7d3db489a7313d1cbec6c4f757f17cb7b
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 */
50 #include "scm_validate.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 (elt
!= SCM_UNDEFINED
)
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,
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,
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,
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 (hare
!= tortoise
);
160 /* If the tortoise ever catches the hare, then the list must contain
166 SCM_DEFINE (scm_length
, "length", 1, 0, 0,
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 "A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs,\n"
184 "The Revised^4 Report on Scheme}). The cdr field of each list's final\n"
185 "pair is changed to point to the head of the next list, so no consing is\n"
186 "performed. Return a pointer to the mutated list.")
187 #define FUNC_NAME s_scm_append
190 SCM
*lloc
= &res
, arg
;
192 SCM_VALIDATE_NULL (SCM_ARGn
, args
);
195 SCM_VALIDATE_CONS (SCM_ARGn
, args
);
198 args
= SCM_CDR(args
);
201 SCM_VALIDATE_NULL (SCM_ARGn
, args
);
204 SCM_VALIDATE_CONS (SCM_ARGn
, args
);
205 for (; SCM_CONSP(arg
); arg
= SCM_CDR(arg
)) {
206 *lloc
= scm_cons(SCM_CAR(arg
), SCM_EOL
);
207 lloc
= SCM_CDRLOC(*lloc
);
209 SCM_VALIDATE_NULL (SCM_ARGn
, arg
);
215 SCM_DEFINE (scm_append_x
, "append!", 0, 0, 1,
218 #define FUNC_NAME s_scm_append_x
222 if (SCM_NULLP(args
)) return SCM_EOL
;
224 args
= SCM_CDR(args
);
225 if (SCM_NULLP(args
)) return arg
;
226 if (SCM_NULLP(arg
)) goto tail
;
227 SCM_VALIDATE_CONS (SCM_ARG1
,arg
);
228 SCM_SETCDR (scm_last_pair (arg
), scm_append_x (args
));
234 SCM_DEFINE (scm_last_pair
, "last-pair", 1, 0, 0,
236 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
237 "@var{lst} is circular.")
238 #define FUNC_NAME s_scm_last_pair
246 SCM_VALIDATE_CONS (SCM_ARG1
, lst
);
248 SCM ahead
= SCM_CDR(hare
);
249 if (SCM_NCONSP(ahead
)) return hare
;
251 ahead
= SCM_CDR(hare
);
252 if (SCM_NCONSP(ahead
)) return hare
;
254 tortoise
= SCM_CDR(tortoise
);
256 while (hare
!= tortoise
);
257 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
262 /* reversing lists */
264 SCM_DEFINE (scm_reverse
, "reverse", 1, 0, 0,
267 #define FUNC_NAME s_scm_reverse
269 SCM result
= SCM_EOL
;
274 if (SCM_NULLP(hare
)) return result
;
275 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
276 result
= scm_cons (SCM_CAR (hare
), result
);
277 hare
= SCM_CDR (hare
);
278 if (SCM_NULLP(hare
)) return result
;
279 SCM_ASSERT(SCM_CONSP(hare
), lst
, 1, FUNC_NAME
);
280 result
= scm_cons (SCM_CAR (hare
), result
);
281 hare
= SCM_CDR (hare
);
282 tortoise
= SCM_CDR (tortoise
);
284 while (hare
!= tortoise
);
285 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst
));
289 SCM_DEFINE (scm_reverse_x
, "reverse!", 1, 1, 0,
290 (SCM lst
, SCM new_tail
),
291 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
292 "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
293 "modified to point to the previous list element. Return a pointer to the\n"
294 "head of the reversed list.\n\n"
295 "Caveat: because the list is modified in place, the tail of the original\n"
296 "list now becomes its head, and the head of the original list now becomes\n"
297 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
298 "original list was bound now points to the tail. To ensure that the head\n"
299 "of the modified list is not lost, it is wise to save the return value of\n"
301 #define FUNC_NAME s_scm_reverse_x
303 SCM_ASSERT (scm_ilength (lst
) >= 0, lst
, SCM_ARG1
, FUNC_NAME
);
304 if (SCM_UNBNDP (new_tail
))
307 SCM_ASSERT (scm_ilength (new_tail
) >= 0, new_tail
, SCM_ARG2
, FUNC_NAME
);
309 while (SCM_NNULLP (lst
))
311 SCM old_tail
= SCM_CDR (lst
);
312 SCM_SETCDR (lst
, new_tail
);
322 /* indexing lists by element number */
324 SCM_DEFINE (scm_list_ref
, "list-ref", 2, 0, 0,
327 #define FUNC_NAME s_scm_list_ref
330 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
332 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
336 SCM_ASSERT(SCM_CONSP(lst
),
337 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
342 SCM_DEFINE (scm_list_set_x
, "list-set!", 3, 0, 0,
343 (SCM lst
, SCM k
, SCM val
),
344 "Set the @var{k}th element of @var{lst} to @var{val}.")
345 #define FUNC_NAME s_scm_list_set_x
348 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
350 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
354 SCM_ASSERT(SCM_CONSP(lst
),
355 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
356 SCM_SETCAR (lst
, val
);
362 SCM_REGISTER_PROC(s_list_cdr_ref
, "list-cdr-ref", 2, 0, 0, scm_list_tail
);
364 SCM_DEFINE (scm_list_tail
, "list-tail", 2, 0, 0,
366 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
367 "The first element of the list is considered to be element 0.\n\n"
368 "@code{list-cdr-ref} and @code{list-tail} are identical. It may help to\n"
369 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
370 "or returning the results of cdring @var{k} times down @var{lst}.")
371 #define FUNC_NAME s_scm_list_tail
374 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
376 SCM_VALIDATE_CONS (1,lst
);
384 SCM_DEFINE (scm_list_cdr_set_x
, "list-cdr-set!", 3, 0, 0,
385 (SCM lst
, SCM k
, SCM val
),
386 "Set the @var{k}th cdr of @var{lst} to @var{val}.")
387 #define FUNC_NAME s_scm_list_cdr_set_x
390 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
392 SCM_ASRTGO(SCM_CONSP(lst
), erout
);
396 SCM_ASSERT(SCM_CONSP(lst
),
397 SCM_NULLP(lst
)?k
:lst
, SCM_NULLP(lst
)?SCM_OUTOFRANGE
:SCM_ARG1
, FUNC_NAME
);
398 SCM_SETCDR (lst
, val
);
405 /* copying lists, perhaps partially */
407 SCM_DEFINE (scm_list_head
, "list-head", 2, 0, 0,
409 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
411 #define FUNC_NAME s_scm_list_head
417 SCM_VALIDATE_INUM_MIN_COPY (2,k
,0,i
);
422 SCM_VALIDATE_CONS (1,lst
);
423 *pos
= scm_cons (SCM_CAR (lst
), SCM_EOL
);
424 pos
= SCM_CDRLOC (*pos
);
432 SCM_DEFINE (scm_list_copy
, "list-copy", 1, 0, 0,
434 "Return a (newly-created) copy of @var{lst}.")
435 #define FUNC_NAME s_scm_list_copy
445 while (SCM_CONSP (from_here
))
448 c
= scm_cons (SCM_CAR (from_here
), SCM_CDR (from_here
));
450 fill_here
= SCM_CDRLOC (c
);
451 from_here
= SCM_CDR (from_here
);
458 /* membership tests (memq, memv, etc.) */
460 SCM_DEFINE (scm_sloppy_memq
, "sloppy-memq", 2, 0, 0,
462 "@deffnx primitive sloppy-memv\n"
463 "@deffnx primitive sloppy-member\n"
464 "These procedures behave like @code{memq}, @code{memv} and @code{member}\n"
465 "(@pxref{Pairs and Lists,,,r4rs, The Revised^4 Report on Scheme}), but do\n"
466 "not perform any type or error checking. Their use is recommended only\n"
467 "in writing Guile internals, not for high-level Scheme programs.")
468 #define FUNC_NAME s_scm_sloppy_memq
470 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
480 SCM_DEFINE (scm_sloppy_memv
, "sloppy-memv", 2, 0, 0,
483 #define FUNC_NAME s_scm_sloppy_memv
485 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
487 if (SCM_BOOL_F
!= scm_eqv_p (SCM_CAR(lst
), x
))
495 SCM_DEFINE (scm_sloppy_member
, "sloppy-member", 2, 0, 0,
498 #define FUNC_NAME s_scm_sloppy_member
500 for(; SCM_CONSP (lst
); lst
= SCM_CDR(lst
))
502 if (SCM_BOOL_F
!= scm_equal_p (SCM_CAR(lst
), x
))
511 SCM_DEFINE (scm_memq
, "memq", 2, 0, 0,
514 #define FUNC_NAME s_scm_memq
517 SCM_VALIDATE_LIST (2,lst
);
518 answer
= scm_sloppy_memq (x
, lst
);
519 return (answer
== SCM_EOL
) ? SCM_BOOL_F
: answer
;
525 SCM_DEFINE (scm_memv
, "memv", 2, 0, 0,
528 #define FUNC_NAME s_scm_memv
531 SCM_VALIDATE_LIST (2,lst
);
532 answer
= scm_sloppy_memv (x
, lst
);
533 return (answer
== SCM_EOL
) ? SCM_BOOL_F
: answer
;
538 SCM_DEFINE (scm_member
, "member", 2, 0, 0,
541 #define FUNC_NAME s_scm_member
544 SCM_VALIDATE_LIST (2,lst
);
545 answer
= scm_sloppy_member (x
, lst
);
546 return (answer
== SCM_EOL
) ? SCM_BOOL_F
: answer
;
552 /* deleting elements from a list (delq, etc.) */
554 SCM_DEFINE (scm_delq_x
, "delq!", 2, 0, 0,
556 "@deffnx primitive delv! item lst\n"
557 "@deffnx primitive delete! item lst\n"
558 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
559 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
560 "rather than creating a new list. Caveat evaluator: Like other\n"
561 "destructive list functions, these functions cannot modify the binding of\n"
562 "@var{lst}, and so cannot be used to delete the first element of\n"
563 "@var{lst} destructively.")
564 #define FUNC_NAME s_scm_delq_x
569 for (prev
= &lst
, walk
= lst
;
571 walk
= SCM_CDR (walk
))
573 if (SCM_CAR (walk
) == item
)
574 *prev
= SCM_CDR (walk
);
576 prev
= SCM_CDRLOC (walk
);
584 SCM_DEFINE (scm_delv_x
, "delv!", 2, 0, 0,
587 #define FUNC_NAME s_scm_delv_x
592 for (prev
= &lst
, walk
= lst
;
594 walk
= SCM_CDR (walk
))
596 if (SCM_BOOL_F
!= scm_eqv_p (SCM_CAR (walk
), item
))
597 *prev
= SCM_CDR (walk
);
599 prev
= SCM_CDRLOC (walk
);
608 SCM_DEFINE (scm_delete_x
, "delete!", 2, 0, 0,
611 #define FUNC_NAME s_scm_delete_x
616 for (prev
= &lst
, walk
= lst
;
618 walk
= SCM_CDR (walk
))
620 if (SCM_BOOL_F
!= scm_equal_p (SCM_CAR (walk
), item
))
621 *prev
= SCM_CDR (walk
);
623 prev
= SCM_CDRLOC (walk
);
634 SCM_DEFINE (scm_delq
, "delq", 2, 0, 0,
636 "@deffnx primitive delv item lst\n"
637 "@deffnx primitive delete item lst\n"
638 "Return a newly-created copy of @var{lst} with @var{item} removed. These\n"
639 "procedures mirror @code{memq}, @code{memv} and @code{member}:\n"
640 "@code{delq} compares elements of @var{lst} against @var{item} with\n"
641 "@code{eq?}, @code{delv} uses @code{eqv?} and @code{delete} uses @code{equal?}")
642 #define FUNC_NAME s_scm_delq
644 SCM copy
= scm_list_copy (lst
);
645 return scm_delq_x (item
, copy
);
649 SCM_DEFINE (scm_delv
, "delv", 2, 0, 0,
652 #define FUNC_NAME s_scm_delv
654 SCM copy
= scm_list_copy (lst
);
655 return scm_delv_x (item
, copy
);
659 SCM_DEFINE (scm_delete
, "delete", 2, 0, 0,
662 #define FUNC_NAME s_scm_delete
664 SCM copy
= scm_list_copy (lst
);
665 return scm_delete_x (item
, copy
);
670 SCM_DEFINE (scm_delq1_x
, "delq1!", 2, 0, 0,
673 #define FUNC_NAME s_scm_delq1_x
678 for (prev
= &lst
, walk
= lst
;
680 walk
= SCM_CDR (walk
))
682 if (SCM_CAR (walk
) == item
)
684 *prev
= SCM_CDR (walk
);
688 prev
= SCM_CDRLOC (walk
);
696 SCM_DEFINE (scm_delv1_x
, "delv1!", 2, 0, 0,
699 #define FUNC_NAME s_scm_delv1_x
704 for (prev
= &lst
, walk
= lst
;
706 walk
= SCM_CDR (walk
))
708 if (SCM_BOOL_F
!= scm_eqv_p (SCM_CAR (walk
), item
))
710 *prev
= SCM_CDR (walk
);
714 prev
= SCM_CDRLOC (walk
);
722 SCM_DEFINE (scm_delete1_x
, "delete1!", 2, 0, 0,
725 #define FUNC_NAME s_scm_delete1_x
730 for (prev
= &lst
, walk
= lst
;
732 walk
= SCM_CDR (walk
))
734 if (SCM_BOOL_F
!= scm_equal_p (SCM_CAR (walk
), item
))
736 *prev
= SCM_CDR (walk
);
740 prev
= SCM_CDRLOC (walk
);