* validate.h
[bpt/guile.git] / libguile / list.c
1 /* Copyright (C) 1995,1996,1997, 2000 Free Software Foundation, Inc.
2 *
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)
6 * any later version.
7 *
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.
12 *
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
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46 #include "libguile/_scm.h"
47 #include "libguile/eq.h"
48
49 #include "libguile/validate.h"
50 #include "libguile/list.h"
51
52 #ifdef __STDC__
53 #include <stdarg.h>
54 #define var_start(x, y) va_start(x, y)
55 #else
56 #include <varargs.h>
57 #define var_start(x, y) va_start(x)
58 #endif
59
60 \f
61 /* creating lists */
62
63 SCM
64 scm_listify (SCM elt, ...)
65 {
66 va_list foo;
67 SCM answer = SCM_EOL;
68 SCM *pos = &answer;
69
70 var_start (foo, elt);
71 while (! SCM_UNBNDP (elt))
72 {
73 *pos = scm_cons (elt, SCM_EOL);
74 pos = SCM_CDRLOC (*pos);
75 elt = va_arg (foo, SCM);
76 }
77 return answer;
78 }
79
80
81 SCM_DEFINE (scm_list, "list", 0, 0, 1,
82 (SCM objs),
83 "Return a list containing @var{objs}, the arguments to\n"
84 "@code{list}.")
85 #define FUNC_NAME s_scm_list
86 {
87 return objs;
88 }
89 #undef FUNC_NAME
90
91
92 #if (SCM_DEBUG_DEPRECATED == 0)
93
94 SCM_REGISTER_PROC (s_list_star, "list*", 1, 0, 1, scm_cons_star);
95
96 #endif /* SCM_DEBUG_DEPRECATED == 0 */
97
98 SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1,
99 (SCM arg, SCM rest),
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
107 {
108 SCM_VALIDATE_REST_ARGUMENT (rest);
109 if (!SCM_NULLP (rest))
110 {
111 SCM prev = arg = scm_cons (arg, rest);
112 while (SCM_NNULLP (SCM_CDR (rest)))
113 {
114 prev = rest;
115 rest = SCM_CDR (rest);
116 }
117 SCM_SETCDR (prev, SCM_CAR (rest));
118 }
119 return arg;
120 }
121 #undef FUNC_NAME
122
123
124 \f
125 /* general questions about lists --- null?, list?, length, etc. */
126
127 SCM_DEFINE (scm_null_p, "null?", 1, 0, 0,
128 (SCM x),
129 "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.")
130 #define FUNC_NAME s_scm_null_p
131 {
132 return SCM_BOOL (SCM_NULLP (x));
133 }
134 #undef FUNC_NAME
135
136
137 SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
138 (SCM x),
139 "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.")
140 #define FUNC_NAME s_scm_list_p
141 {
142 return SCM_BOOL (scm_ilength (x) >= 0);
143 }
144 #undef FUNC_NAME
145
146
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. */
151 scm_bits_t
152 scm_ilength (SCM sx)
153 {
154 scm_bits_t i = 0;
155 SCM tortoise = sx;
156 SCM hare = sx;
157
158 do {
159 if (SCM_NULLP(hare)) return i;
160 if (SCM_NCONSP(hare)) return -1;
161 hare = SCM_CDR(hare);
162 i++;
163 if (SCM_NULLP(hare)) return i;
164 if (SCM_NCONSP(hare)) return -1;
165 hare = SCM_CDR(hare);
166 i++;
167 /* For every two steps the hare takes, the tortoise takes one. */
168 tortoise = SCM_CDR(tortoise);
169 }
170 while (! SCM_EQ_P (hare, tortoise));
171
172 /* If the tortoise ever catches the hare, then the list must contain
173 a cycle. */
174 return -1;
175 }
176
177
178 SCM_DEFINE (scm_length, "length", 1, 0, 0,
179 (SCM lst),
180 "Return the number of elements in list @var{lst}.")
181 #define FUNC_NAME s_scm_length
182 {
183 scm_bits_t i;
184 SCM_VALIDATE_LIST_COPYLEN (1,lst,i);
185 return SCM_MAKINUM (i);
186 }
187 #undef FUNC_NAME
188
189
190 \f
191 /* appending lists */
192
193 SCM_DEFINE (scm_append, "append", 0, 0, 1,
194 (SCM args),
195 "Return a list consisting of the elements the lists passed as\n"
196 "arguments.\n"
197 "@lisp\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"
201 "@end lisp\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"
206 "@lisp\n"
207 "(append '(a b) '(c . d)) @result{} (a b c . d)\n"
208 "(append '() 'a) @result{} a\n"
209 "@end lisp")
210 #define FUNC_NAME s_scm_append
211 {
212 SCM_VALIDATE_REST_ARGUMENT (args);
213 if (SCM_NULLP (args)) {
214 return SCM_EOL;
215 } else {
216 SCM res = SCM_EOL;
217 SCM *lloc = &res;
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);
224 arg = SCM_CDR (arg);
225 }
226 SCM_VALIDATE_NULL (SCM_ARGn, arg);
227 arg = SCM_CAR (args);
228 args = SCM_CDR (args);
229 };
230 *lloc = arg;
231 return res;
232 }
233 }
234 #undef FUNC_NAME
235
236
237 SCM_DEFINE (scm_append_x, "append!", 0, 0, 1,
238 (SCM lists),
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"
243 "the mutated list.")
244 #define FUNC_NAME s_scm_append_x
245 {
246 SCM_VALIDATE_REST_ARGUMENT (lists);
247 while (1) {
248 if (SCM_NULLP (lists)) {
249 return SCM_EOL;
250 } else {
251 SCM arg = SCM_CAR (lists);
252 lists = SCM_CDR (lists);
253 if (SCM_NULLP (lists)) {
254 return arg;
255 } else if (!SCM_NULLP (arg)) {
256 SCM_VALIDATE_CONS (SCM_ARG1, arg);
257 SCM_SETCDR (scm_last_pair (arg), scm_append_x (lists));
258 return arg;
259 }
260 }
261 }
262 }
263 #undef FUNC_NAME
264
265
266 SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
267 (SCM lst),
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
271 {
272 SCM tortoise = lst;
273 SCM hare = lst;
274
275 if (SCM_NULLP (lst))
276 return SCM_EOL;
277
278 SCM_VALIDATE_CONS (SCM_ARG1, lst);
279 do {
280 SCM ahead = SCM_CDR(hare);
281 if (SCM_NCONSP(ahead)) return hare;
282 hare = ahead;
283 ahead = SCM_CDR(hare);
284 if (SCM_NCONSP(ahead)) return hare;
285 hare = ahead;
286 tortoise = SCM_CDR(tortoise);
287 }
288 while (! SCM_EQ_P (hare, tortoise));
289 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
290 }
291 #undef FUNC_NAME
292
293 \f
294 /* reversing lists */
295
296 SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
297 (SCM lst),
298 "Return a new list that contains the elements of @var{lst} but\n"
299 "in reverse order.")
300 #define FUNC_NAME s_scm_reverse
301 {
302 SCM result = SCM_EOL;
303 SCM tortoise = lst;
304 SCM hare = lst;
305
306 do {
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);
316 }
317 while (! SCM_EQ_P (hare, tortoise));
318 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
319 }
320 #undef FUNC_NAME
321
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"
333 "@code{reverse!}")
334 #define FUNC_NAME s_scm_reverse_x
335 {
336 SCM_VALIDATE_LIST (1, lst);
337 if (SCM_UNBNDP (new_tail))
338 new_tail = SCM_EOL;
339 else
340 SCM_VALIDATE_LIST (2, new_tail);
341
342 while (SCM_NNULLP (lst))
343 {
344 SCM old_tail = SCM_CDR (lst);
345 SCM_SETCDR (lst, new_tail);
346 new_tail = lst;
347 lst = old_tail;
348 }
349 return new_tail;
350 }
351 #undef FUNC_NAME
352
353 \f
354
355 /* indexing lists by element number */
356
357 SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
358 (SCM list, SCM k),
359 "Return the @var{k}th element from @var{list}.")
360 #define FUNC_NAME s_scm_list_ref
361 {
362 SCM lst = list;
363 register scm_bits_t i;
364 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
365 while (SCM_CONSP (lst)) {
366 if (i == 0)
367 return SCM_CAR (lst);
368 else {
369 --i;
370 lst = SCM_CDR (lst);
371 }
372 };
373 if (SCM_NULLP (lst))
374 SCM_OUT_OF_RANGE (2, k);
375 else
376 SCM_WRONG_TYPE_ARG (1, list);
377 }
378 #undef FUNC_NAME
379
380
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
385 {
386 SCM lst = list;
387 register scm_bits_t i;
388 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
389 while (SCM_CONSP (lst)) {
390 if (i == 0) {
391 SCM_SETCAR (lst, val);
392 return val;
393 } else {
394 --i;
395 lst = SCM_CDR (lst);
396 }
397 };
398 if (SCM_NULLP (lst))
399 SCM_OUT_OF_RANGE (2, k);
400 else
401 SCM_WRONG_TYPE_ARG (1, list);
402 }
403 #undef FUNC_NAME
404
405
406 SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
407
408 SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
409 (SCM lst, SCM k),
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
417 {
418 register scm_bits_t i;
419 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
420 while (i-- > 0) {
421 SCM_VALIDATE_CONS (1,lst);
422 lst = SCM_CDR(lst);
423 }
424 return lst;
425 }
426 #undef FUNC_NAME
427
428
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
433 {
434 SCM lst = list;
435 scm_bits_t i;
436 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
437 while (SCM_CONSP (lst)) {
438 if (i == 0) {
439 SCM_SETCDR (lst, val);
440 return val;
441 } else {
442 --i;
443 lst = SCM_CDR (lst);
444 }
445 };
446 if (SCM_NULLP (lst))
447 SCM_OUT_OF_RANGE (2, k);
448 else
449 SCM_WRONG_TYPE_ARG (1, list);
450 }
451 #undef FUNC_NAME
452
453
454 \f
455 /* copying lists, perhaps partially */
456
457 SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
458 (SCM lst, SCM k),
459 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
460 "return it.")
461 #define FUNC_NAME s_scm_list_head
462 {
463 SCM answer;
464 SCM * pos;
465 register scm_bits_t i;
466
467 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
468 answer = SCM_EOL;
469 pos = &answer;
470 while (i-- > 0)
471 {
472 SCM_VALIDATE_CONS (1,lst);
473 *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
474 pos = SCM_CDRLOC (*pos);
475 lst = SCM_CDR(lst);
476 }
477 return answer;
478 }
479 #undef FUNC_NAME
480
481
482 SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
483 (SCM lst),
484 "Return a (newly-created) copy of @var{lst}.")
485 #define FUNC_NAME s_scm_list_copy
486 {
487 SCM newlst;
488 SCM * fill_here;
489 SCM from_here;
490
491 SCM_VALIDATE_LIST (1, lst);
492
493 newlst = SCM_EOL;
494 fill_here = &newlst;
495 from_here = lst;
496
497 while (SCM_CONSP (from_here))
498 {
499 SCM c;
500 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
501 *fill_here = c;
502 fill_here = SCM_CDRLOC (c);
503 from_here = SCM_CDR (from_here);
504 }
505 return newlst;
506 }
507 #undef FUNC_NAME
508
509 \f
510 /* membership tests (memq, memv, etc.) */
511
512 #if SCM_DEBUG_DEPRECATED == 0
513
514 SCM_DEFINE (scm_sloppy_memq, "sloppy-memq", 2, 0, 0,
515 (SCM x, SCM lst),
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
520 {
521 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
522 {
523 if (SCM_EQ_P (SCM_CAR (lst), x))
524 return lst;
525 }
526 return lst;
527 }
528 #undef FUNC_NAME
529
530
531 SCM_DEFINE (scm_sloppy_memv, "sloppy-memv", 2, 0, 0,
532 (SCM x, SCM lst),
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
537 {
538 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
539 {
540 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
541 return lst;
542 }
543 return lst;
544 }
545 #undef FUNC_NAME
546
547
548 SCM_DEFINE (scm_sloppy_member, "sloppy-member", 2, 0, 0,
549 (SCM x, SCM lst),
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
554 {
555 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
556 {
557 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
558 return lst;
559 }
560 return lst;
561 }
562 #undef FUNC_NAME
563
564 #endif /* DEPRECATED */
565
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
571 * endlessly.
572 */
573 SCM
574 scm_c_memq (SCM obj, SCM list)
575 {
576 for (; !SCM_NULLP (list); list = SCM_CDR (list))
577 {
578 if (SCM_EQ_P (SCM_CAR (list), obj))
579 return list;
580 }
581 return SCM_BOOL_F;
582 }
583
584
585 SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
586 (SCM x, SCM lst),
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"
592 "returned.")
593 #define FUNC_NAME s_scm_memq
594 {
595 SCM_VALIDATE_LIST (2, lst);
596 return scm_c_memq (x, lst);
597 }
598 #undef FUNC_NAME
599
600
601
602 SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
603 (SCM x, SCM lst),
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"
609 "returned.")
610 #define FUNC_NAME s_scm_memv
611 {
612 SCM_VALIDATE_LIST (2, lst);
613 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
614 {
615 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
616 return lst;
617 }
618 return SCM_BOOL_F;
619 }
620 #undef FUNC_NAME
621
622
623 SCM_DEFINE (scm_member, "member", 2, 0, 0,
624 (SCM x, SCM lst),
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
632 {
633 SCM_VALIDATE_LIST (2, lst);
634 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
635 {
636 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
637 return lst;
638 }
639 return SCM_BOOL_F;
640 }
641 #undef FUNC_NAME
642
643
644 \f
645 /* deleting elements from a list (delq, etc.) */
646
647 SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
648 (SCM item, SCM lst),
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
658 {
659 SCM walk;
660 SCM *prev;
661
662 for (prev = &lst, walk = lst;
663 SCM_CONSP (walk);
664 walk = SCM_CDR (walk))
665 {
666 if (SCM_EQ_P (SCM_CAR (walk), item))
667 *prev = SCM_CDR (walk);
668 else
669 prev = SCM_CDRLOC (walk);
670 }
671
672 return lst;
673 }
674 #undef FUNC_NAME
675
676
677 SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
678 (SCM item, SCM lst),
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
682 {
683 SCM walk;
684 SCM *prev;
685
686 for (prev = &lst, walk = lst;
687 SCM_CONSP (walk);
688 walk = SCM_CDR (walk))
689 {
690 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
691 *prev = SCM_CDR (walk);
692 else
693 prev = SCM_CDRLOC (walk);
694 }
695
696 return lst;
697 }
698 #undef FUNC_NAME
699
700
701
702 SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
703 (SCM item, SCM lst),
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
707 {
708 SCM walk;
709 SCM *prev;
710
711 for (prev = &lst, walk = lst;
712 SCM_CONSP (walk);
713 walk = SCM_CDR (walk))
714 {
715 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
716 *prev = SCM_CDR (walk);
717 else
718 prev = SCM_CDRLOC (walk);
719 }
720
721 return lst;
722 }
723 #undef FUNC_NAME
724
725
726 \f
727
728
729 SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
730 (SCM item, SCM lst),
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
736 {
737 SCM copy = scm_list_copy (lst);
738 return scm_delq_x (item, copy);
739 }
740 #undef FUNC_NAME
741
742 SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
743 (SCM item, SCM lst),
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
749 {
750 SCM copy = scm_list_copy (lst);
751 return scm_delv_x (item, copy);
752 }
753 #undef FUNC_NAME
754
755 SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
756 (SCM item, SCM lst),
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
762 {
763 SCM copy = scm_list_copy (lst);
764 return scm_delete_x (item, copy);
765 }
766 #undef FUNC_NAME
767
768
769 SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
770 (SCM item, SCM lst),
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
775 {
776 SCM walk;
777 SCM *prev;
778
779 for (prev = &lst, walk = lst;
780 SCM_CONSP (walk);
781 walk = SCM_CDR (walk))
782 {
783 if (SCM_EQ_P (SCM_CAR (walk), item))
784 {
785 *prev = SCM_CDR (walk);
786 break;
787 }
788 else
789 prev = SCM_CDRLOC (walk);
790 }
791
792 return lst;
793 }
794 #undef FUNC_NAME
795
796
797 SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
798 (SCM item, SCM lst),
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
803 {
804 SCM walk;
805 SCM *prev;
806
807 for (prev = &lst, walk = lst;
808 SCM_CONSP (walk);
809 walk = SCM_CDR (walk))
810 {
811 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
812 {
813 *prev = SCM_CDR (walk);
814 break;
815 }
816 else
817 prev = SCM_CDRLOC (walk);
818 }
819
820 return lst;
821 }
822 #undef FUNC_NAME
823
824
825 SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
826 (SCM item, SCM lst),
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
831 {
832 SCM walk;
833 SCM *prev;
834
835 for (prev = &lst, walk = lst;
836 SCM_CONSP (walk);
837 walk = SCM_CDR (walk))
838 {
839 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
840 {
841 *prev = SCM_CDR (walk);
842 break;
843 }
844 else
845 prev = SCM_CDRLOC (walk);
846 }
847
848 return lst;
849 }
850 #undef FUNC_NAME
851
852
853 \f
854 void
855 scm_init_list ()
856 {
857 #ifndef SCM_MAGIC_SNARFER
858 #include "libguile/list.x"
859 #endif
860 }
861
862 /*
863 Local Variables:
864 c-file-style: "gnu"
865 End:
866 */