d4305ee1fd0a370cc9432551c391574adf3cde0a
[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 <stdio.h>
47 #include "libguile/_scm.h"
48 #include "libguile/eq.h"
49
50 #include "libguile/validate.h"
51 #include "libguile/list.h"
52
53 #ifdef __STDC__
54 #include <stdarg.h>
55 #define var_start(x, y) va_start(x, y)
56 #else
57 #include <varargs.h>
58 #define var_start(x, y) va_start(x)
59 #endif
60
61 \f
62 /* creating lists */
63
64 SCM
65 scm_listify (SCM elt, ...)
66 {
67 va_list foo;
68 SCM answer = SCM_EOL;
69 SCM *pos = &answer;
70
71 var_start (foo, elt);
72 while (! SCM_UNBNDP (elt))
73 {
74 *pos = scm_cons (elt, SCM_EOL);
75 pos = SCM_CDRLOC (*pos);
76 elt = va_arg (foo, SCM);
77 }
78 return answer;
79 }
80
81
82 SCM_DEFINE (scm_list, "list", 0, 0, 1,
83 (SCM objs),
84 "Return a list containing OBJS, the arguments to `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 `list', but the last arg provides the tail of the constructed list,\n"
101 "returning (cons ARG1 (cons ARG2 (cons ... ARGn))).\n"
102 "Requires at least one argument. If given one argument, that argument\n"
103 "is returned as result.\n"
104 "This function is called `list*' in some other Schemes and in Common LISP.")
105 #define FUNC_NAME s_scm_cons_star
106 {
107 SCM_VALIDATE_REST_ARGUMENT (rest);
108 if (!SCM_NULLP (rest))
109 {
110 SCM prev = arg = scm_cons (arg, rest);
111 while (SCM_NNULLP (SCM_CDR (rest)))
112 {
113 prev = rest;
114 rest = SCM_CDR (rest);
115 }
116 SCM_SETCDR (prev, SCM_CAR (rest));
117 }
118 return arg;
119 }
120 #undef FUNC_NAME
121
122
123 \f
124 /* general questions about lists --- null?, list?, length, etc. */
125
126 SCM_DEFINE (scm_null_p, "null?", 1, 0, 0,
127 (SCM x),
128 "Return #t iff X is the empty list, else #f.")
129 #define FUNC_NAME s_scm_null_p
130 {
131 return SCM_BOOL (SCM_NULLP (x));
132 }
133 #undef FUNC_NAME
134
135
136 SCM_DEFINE (scm_list_p, "list?", 1, 0, 0,
137 (SCM x),
138 "Return #t iff X is a proper list, else #f.")
139 #define FUNC_NAME s_scm_list_p
140 {
141 return SCM_BOOL (scm_ilength (x) >= 0);
142 }
143 #undef FUNC_NAME
144
145
146 /* Return the length of SX, or -1 if it's not a proper list.
147 This uses the "tortoise and hare" algorithm to detect "infinitely
148 long" lists (i.e. lists with cycles in their cdrs), and returns -1
149 if it does find one. */
150 long
151 scm_ilength(SCM sx)
152 {
153 long i = 0;
154 SCM tortoise = sx;
155 SCM hare = sx;
156
157 do {
158 if (SCM_NULLP(hare)) return i;
159 if (SCM_NCONSP(hare)) return -1;
160 hare = SCM_CDR(hare);
161 i++;
162 if (SCM_NULLP(hare)) return i;
163 if (SCM_NCONSP(hare)) return -1;
164 hare = SCM_CDR(hare);
165 i++;
166 /* For every two steps the hare takes, the tortoise takes one. */
167 tortoise = SCM_CDR(tortoise);
168 }
169 while (! SCM_EQ_P (hare, tortoise));
170
171 /* If the tortoise ever catches the hare, then the list must contain
172 a cycle. */
173 return -1;
174 }
175
176
177 SCM_DEFINE (scm_length, "length", 1, 0, 0,
178 (SCM lst),
179 "Return the number of elements in list LST.")
180 #define FUNC_NAME s_scm_length
181 {
182 int i;
183 SCM_VALIDATE_LIST_COPYLEN (1,lst,i);
184 return SCM_MAKINUM (i);
185 }
186 #undef FUNC_NAME
187
188
189 \f
190 /* appending lists */
191
192 SCM_DEFINE (scm_append, "append", 0, 0, 1,
193 (SCM args),
194 "Returns a list consisting of the elements of the first LIST\n"
195 "followed by the elements of the other LISTs.\n\n"
196 "@example\n"
197 " (append '(x) '(y)) => (x y)\n"
198 " (append '(a) '(b c d)) => (a b c d)\n"
199 " (append '(a (b)) '((c))) => (a (b) (c))\n"
200 "@end example\n\n"
201 "The resulting list is always newly allocated, except that it shares\n"
202 "structure with the last LIST argument. The last argument may\n"
203 "actually be any object; an improper list results if the last\n"
204 "argument is not a proper list.\n\n"
205 "@example\n"
206 " (append '(a b) '(c . d)) => (a b c . d)\n"
207 " (append '() 'a) => a\n"
208 "@end example")
209 #define FUNC_NAME s_scm_append
210 {
211 SCM_VALIDATE_REST_ARGUMENT (args);
212 if (SCM_NULLP (args)) {
213 return SCM_EOL;
214 } else {
215 SCM res = SCM_EOL;
216 SCM *lloc = &res;
217 SCM arg = SCM_CAR (args);
218 args = SCM_CDR (args);
219 while (!SCM_NULLP (args)) {
220 while (SCM_CONSP (arg)) {
221 *lloc = scm_cons (SCM_CAR (arg), SCM_EOL);
222 lloc = SCM_CDRLOC (*lloc);
223 arg = SCM_CDR (arg);
224 }
225 SCM_VALIDATE_NULL (SCM_ARGn, arg);
226 arg = SCM_CAR (args);
227 args = SCM_CDR (args);
228 };
229 *lloc = arg;
230 return res;
231 }
232 }
233 #undef FUNC_NAME
234
235
236 SCM_DEFINE (scm_append_x, "append!", 0, 0, 1,
237 (SCM args),
238 "A destructive version of @code{append} (@pxref{Pairs and Lists,,,r4rs,\n"
239 "The Revised^4 Report on Scheme}). The cdr field of each list's final\n"
240 "pair is changed to point to the head of the next list, so no consing is\n"
241 "performed. Return a pointer to the mutated list.")
242 #define FUNC_NAME s_scm_append_x
243 {
244 SCM_VALIDATE_REST_ARGUMENT (args);
245 while (1) {
246 if (SCM_NULLP (args)) {
247 return SCM_EOL;
248 } else {
249 SCM arg = SCM_CAR (args);
250 args = SCM_CDR (args);
251 if (SCM_NULLP (args)) {
252 return arg;
253 } else if (!SCM_NULLP (arg)) {
254 SCM_VALIDATE_CONS (SCM_ARG1, arg);
255 SCM_SETCDR (scm_last_pair (arg), scm_append_x (args));
256 return arg;
257 }
258 }
259 }
260 }
261 #undef FUNC_NAME
262
263
264 SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0,
265 (SCM lst),
266 "Return a pointer to the last pair in @var{lst}, signalling an error if\n"
267 "@var{lst} is circular.")
268 #define FUNC_NAME s_scm_last_pair
269 {
270 SCM tortoise = lst;
271 SCM hare = lst;
272
273 if (SCM_NULLP (lst))
274 return SCM_EOL;
275
276 SCM_VALIDATE_CONS (SCM_ARG1, lst);
277 do {
278 SCM ahead = SCM_CDR(hare);
279 if (SCM_NCONSP(ahead)) return hare;
280 hare = ahead;
281 ahead = SCM_CDR(hare);
282 if (SCM_NCONSP(ahead)) return hare;
283 hare = ahead;
284 tortoise = SCM_CDR(tortoise);
285 }
286 while (! SCM_EQ_P (hare, tortoise));
287 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
288 }
289 #undef FUNC_NAME
290
291 \f
292 /* reversing lists */
293
294 SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0,
295 (SCM lst),
296 "Return a new list that contains the elements of LST but in reverse order.")
297 #define FUNC_NAME s_scm_reverse
298 {
299 SCM result = SCM_EOL;
300 SCM tortoise = lst;
301 SCM hare = lst;
302
303 do {
304 if (SCM_NULLP(hare)) return result;
305 SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
306 result = scm_cons (SCM_CAR (hare), result);
307 hare = SCM_CDR (hare);
308 if (SCM_NULLP(hare)) return result;
309 SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME);
310 result = scm_cons (SCM_CAR (hare), result);
311 hare = SCM_CDR (hare);
312 tortoise = SCM_CDR (tortoise);
313 }
314 while (! SCM_EQ_P (hare, tortoise));
315 SCM_MISC_ERROR ("Circular structure in position 1: ~S", SCM_LIST1 (lst));
316 }
317 #undef FUNC_NAME
318
319 SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0,
320 (SCM lst, SCM new_tail),
321 "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r4rs,\n"
322 "The Revised^4 Report on Scheme}). The cdr of each cell in @var{lst} is\n"
323 "modified to point to the previous list element. Return a pointer to the\n"
324 "head of the reversed list.\n\n"
325 "Caveat: because the list is modified in place, the tail of the original\n"
326 "list now becomes its head, and the head of the original list now becomes\n"
327 "the tail. Therefore, the @var{lst} symbol to which the head of the\n"
328 "original list was bound now points to the tail. To ensure that the head\n"
329 "of the modified list is not lost, it is wise to save the return value of\n"
330 "@code{reverse!}")
331 #define FUNC_NAME s_scm_reverse_x
332 {
333 SCM_ASSERT (scm_ilength (lst) >= 0, lst, SCM_ARG1, FUNC_NAME);
334 if (SCM_UNBNDP (new_tail))
335 new_tail = SCM_EOL;
336 else
337 SCM_ASSERT (scm_ilength (new_tail) >= 0, new_tail, SCM_ARG2, FUNC_NAME);
338
339 while (SCM_NNULLP (lst))
340 {
341 SCM old_tail = SCM_CDR (lst);
342 SCM_SETCDR (lst, new_tail);
343 new_tail = lst;
344 lst = old_tail;
345 }
346 return new_tail;
347 }
348 #undef FUNC_NAME
349
350 \f
351
352 /* indexing lists by element number */
353
354 SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0,
355 (SCM list, SCM k),
356 "Return the Kth element from LIST.")
357 #define FUNC_NAME s_scm_list_ref
358 {
359 SCM lst = list;
360 unsigned long int i;
361 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
362 while (SCM_CONSP (lst)) {
363 if (i == 0)
364 return SCM_CAR (lst);
365 else {
366 --i;
367 lst = SCM_CDR (lst);
368 }
369 };
370 if (SCM_NULLP (lst))
371 SCM_OUT_OF_RANGE (2, k);
372 else
373 SCM_WRONG_TYPE_ARG (1, list);
374 }
375 #undef FUNC_NAME
376
377
378 SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0,
379 (SCM list, SCM k, SCM val),
380 "Set the @var{k}th element of @var{list} to @var{val}.")
381 #define FUNC_NAME s_scm_list_set_x
382 {
383 SCM lst = list;
384 unsigned long int i;
385 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
386 while (SCM_CONSP (lst)) {
387 if (i == 0) {
388 SCM_SETCAR (lst, val);
389 return val;
390 } else {
391 --i;
392 lst = SCM_CDR (lst);
393 }
394 };
395 if (SCM_NULLP (lst))
396 SCM_OUT_OF_RANGE (2, k);
397 else
398 SCM_WRONG_TYPE_ARG (1, list);
399 }
400 #undef FUNC_NAME
401
402
403 SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
404
405 SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0,
406 (SCM lst, SCM k),
407 "@deffnx primitive list-cdr-ref lst k\n"
408 "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n"
409 "The first element of the list is considered to be element 0.\n\n"
410 "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n"
411 "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n"
412 "or returning the results of cdring @var{k} times down @var{lst}.")
413 #define FUNC_NAME s_scm_list_tail
414 {
415 register long i;
416 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
417 while (i-- > 0) {
418 SCM_VALIDATE_CONS (1,lst);
419 lst = SCM_CDR(lst);
420 }
421 return lst;
422 }
423 #undef FUNC_NAME
424
425
426 SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
427 (SCM list, SCM k, SCM val),
428 "Set the @var{k}th cdr of @var{list} to @var{val}.")
429 #define FUNC_NAME s_scm_list_cdr_set_x
430 {
431 SCM lst = list;
432 unsigned long int i;
433 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
434 while (SCM_CONSP (lst)) {
435 if (i == 0) {
436 SCM_SETCDR (lst, val);
437 return val;
438 } else {
439 --i;
440 lst = SCM_CDR (lst);
441 }
442 };
443 if (SCM_NULLP (lst))
444 SCM_OUT_OF_RANGE (2, k);
445 else
446 SCM_WRONG_TYPE_ARG (1, list);
447 }
448 #undef FUNC_NAME
449
450
451 \f
452 /* copying lists, perhaps partially */
453
454 SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0,
455 (SCM lst, SCM k),
456 "Copy the first @var{k} elements from @var{lst} into a new list, and\n"
457 "return it.")
458 #define FUNC_NAME s_scm_list_head
459 {
460 SCM answer;
461 SCM * pos;
462 register long i;
463
464 SCM_VALIDATE_INUM_MIN_COPY (2,k,0,i);
465 answer = SCM_EOL;
466 pos = &answer;
467 while (i-- > 0)
468 {
469 SCM_VALIDATE_CONS (1,lst);
470 *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
471 pos = SCM_CDRLOC (*pos);
472 lst = SCM_CDR(lst);
473 }
474 return answer;
475 }
476 #undef FUNC_NAME
477
478
479 SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0,
480 (SCM lst),
481 "Return a (newly-created) copy of @var{lst}.")
482 #define FUNC_NAME s_scm_list_copy
483 {
484 SCM newlst;
485 SCM * fill_here;
486 SCM from_here;
487
488 newlst = SCM_EOL;
489 fill_here = &newlst;
490 from_here = lst;
491
492 while (SCM_CONSP (from_here))
493 {
494 SCM c;
495 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
496 *fill_here = c;
497 fill_here = SCM_CDRLOC (c);
498 from_here = SCM_CDR (from_here);
499 }
500 return newlst;
501 }
502 #undef FUNC_NAME
503
504 \f
505 /* membership tests (memq, memv, etc.) */
506
507 #if SCM_DEBUG_DEPRECATED == 0
508
509 SCM_DEFINE (scm_sloppy_memq, "sloppy-memq", 2, 0, 0,
510 (SCM x, SCM lst),
511 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
512 "Its use is recommended only in writing Guile internals,\n"
513 "not for high-level Scheme programs.")
514 #define FUNC_NAME s_scm_sloppy_memq
515 {
516 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
517 {
518 if (SCM_EQ_P (SCM_CAR (lst), x))
519 return lst;
520 }
521 return lst;
522 }
523 #undef FUNC_NAME
524
525
526 SCM_DEFINE (scm_sloppy_memv, "sloppy-memv", 2, 0, 0,
527 (SCM x, SCM lst),
528 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
529 "Its use is recommended only in writing Guile internals,\n"
530 "not for high-level Scheme programs.")
531 #define FUNC_NAME s_scm_sloppy_memv
532 {
533 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
534 {
535 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
536 return lst;
537 }
538 return lst;
539 }
540 #undef FUNC_NAME
541
542
543 SCM_DEFINE (scm_sloppy_member, "sloppy-member", 2, 0, 0,
544 (SCM x, SCM lst),
545 "This procedure behaves like @code{member}, but does no type or error checking.\n"
546 "Its use is recommended only in writing Guile internals,\n"
547 "not for high-level Scheme programs.")
548 #define FUNC_NAME s_scm_sloppy_member
549 {
550 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
551 {
552 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
553 return lst;
554 }
555 return lst;
556 }
557 #undef FUNC_NAME
558
559 #endif /* DEPRECATED */
560
561 SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
562 (SCM x, SCM lst),
563 "Return the first sublist of LST whose car is `eq?' to X\n"
564 "where the sublists of LST are the non-empty lists returned\n"
565 "by `(list-tail LST K)' for K less than the length of LST. If\n"
566 "X does not occur in LST, then `#f' (not the empty list) is\n"
567 "returned.")
568 #define FUNC_NAME s_scm_memq
569 {
570 SCM_VALIDATE_LIST (2, lst);
571 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
572 {
573 if (SCM_EQ_P (SCM_CAR (lst), x))
574 return lst;
575 }
576 return SCM_BOOL_F;
577 }
578 #undef FUNC_NAME
579
580
581
582 SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
583 (SCM x, SCM lst),
584 "Return the first sublist of LST whose car is `eqv?' to X\n"
585 "where the sublists of LST are the non-empty lists returned\n"
586 "by `(list-tail LST K)' for K less than the length of LST. If\n"
587 "X does not occur in LST, then `#f' (not the empty list) is\n"
588 "returned.")
589 #define FUNC_NAME s_scm_memv
590 {
591 SCM_VALIDATE_LIST (2, lst);
592 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
593 {
594 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
595 return lst;
596 }
597 return SCM_BOOL_F;
598 }
599 #undef FUNC_NAME
600
601
602 SCM_DEFINE (scm_member, "member", 2, 0, 0,
603 (SCM x, SCM lst),
604 "Return the first sublist of LST whose car is `equal?' to X\n"
605 "where the sublists of LST are the non-empty lists returned\n"
606 "by `(list-tail LST K)' for K less than the length of LST. If\n"
607 "X does not occur in LST, then `#f' (not the empty list) is\n"
608 "returned.")
609 #define FUNC_NAME s_scm_member
610 {
611 SCM_VALIDATE_LIST (2, lst);
612 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
613 {
614 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
615 return lst;
616 }
617 return SCM_BOOL_F;
618 }
619 #undef FUNC_NAME
620
621
622 \f
623 /* deleting elements from a list (delq, etc.) */
624
625 SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
626 (SCM item, SCM lst),
627 "@deffnx primitive delv! item lst\n"
628 "@deffnx primitive delete! item lst\n"
629 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
630 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
631 "rather than creating a new list. Caveat evaluator: Like other\n"
632 "destructive list functions, these functions cannot modify the binding of\n"
633 "@var{lst}, and so cannot be used to delete the first element of\n"
634 "@var{lst} destructively.")
635 #define FUNC_NAME s_scm_delq_x
636 {
637 SCM walk;
638 SCM *prev;
639
640 for (prev = &lst, walk = lst;
641 SCM_CONSP (walk);
642 walk = SCM_CDR (walk))
643 {
644 if (SCM_EQ_P (SCM_CAR (walk), item))
645 *prev = SCM_CDR (walk);
646 else
647 prev = SCM_CDRLOC (walk);
648 }
649
650 return lst;
651 }
652 #undef FUNC_NAME
653
654
655 SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
656 (SCM item, SCM lst),
657 "Destructively remove all elements from LST that are `eqv?' to ITEM.")
658 #define FUNC_NAME s_scm_delv_x
659 {
660 SCM walk;
661 SCM *prev;
662
663 for (prev = &lst, walk = lst;
664 SCM_CONSP (walk);
665 walk = SCM_CDR (walk))
666 {
667 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
668 *prev = SCM_CDR (walk);
669 else
670 prev = SCM_CDRLOC (walk);
671 }
672
673 return lst;
674 }
675 #undef FUNC_NAME
676
677
678
679 SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
680 (SCM item, SCM lst),
681 "Destructively remove all elements from LST that are `equal?' to ITEM.")
682 #define FUNC_NAME s_scm_delete_x
683 {
684 SCM walk;
685 SCM *prev;
686
687 for (prev = &lst, walk = lst;
688 SCM_CONSP (walk);
689 walk = SCM_CDR (walk))
690 {
691 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
692 *prev = SCM_CDR (walk);
693 else
694 prev = SCM_CDRLOC (walk);
695 }
696
697 return lst;
698 }
699 #undef FUNC_NAME
700
701
702 \f
703
704
705 SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
706 (SCM item, SCM lst),
707 "Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed.\n"
708 "This procedure mirrors @code{memq}:\n"
709 "@code{delq} compares elements of @var{lst} against @var{item} with\n"
710 "@code{eq?}.")
711 #define FUNC_NAME s_scm_delq
712 {
713 SCM copy = scm_list_copy (lst);
714 return scm_delq_x (item, copy);
715 }
716 #undef FUNC_NAME
717
718 SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
719 (SCM item, SCM lst),
720 "Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed.\n"
721 "This procedure mirrors @code{memv}:\n"
722 "@code{delv} compares elements of @var{lst} against @var{item} with\n"
723 "@code{eqv?}.")
724 #define FUNC_NAME s_scm_delv
725 {
726 SCM copy = scm_list_copy (lst);
727 return scm_delv_x (item, copy);
728 }
729 #undef FUNC_NAME
730
731 SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
732 (SCM item, SCM lst),
733 "Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed.\n"
734 "This procedure mirrors @code{member}:\n"
735 "@code{delete} compares elements of @var{lst} against @var{item} with\n"
736 "@code{equal?}.")
737 #define FUNC_NAME s_scm_delete
738 {
739 SCM copy = scm_list_copy (lst);
740 return scm_delete_x (item, copy);
741 }
742 #undef FUNC_NAME
743
744
745 SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
746 (SCM item, SCM lst),
747 "Like `delq!', but only deletes the first occurrence of ITEM from LST.\n"
748 "Tests for equality using `eq?'. See also `delv1!' and `delete1!'.")
749 #define FUNC_NAME s_scm_delq1_x
750 {
751 SCM walk;
752 SCM *prev;
753
754 for (prev = &lst, walk = lst;
755 SCM_CONSP (walk);
756 walk = SCM_CDR (walk))
757 {
758 if (SCM_EQ_P (SCM_CAR (walk), item))
759 {
760 *prev = SCM_CDR (walk);
761 break;
762 }
763 else
764 prev = SCM_CDRLOC (walk);
765 }
766
767 return lst;
768 }
769 #undef FUNC_NAME
770
771
772 SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
773 (SCM item, SCM lst),
774 "Like `delv!', but only deletes the first occurrence of ITEM from LST.\n"
775 "Tests for equality using `eqv?'. See also `delq1!' and `delete1!'.")
776 #define FUNC_NAME s_scm_delv1_x
777 {
778 SCM walk;
779 SCM *prev;
780
781 for (prev = &lst, walk = lst;
782 SCM_CONSP (walk);
783 walk = SCM_CDR (walk))
784 {
785 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
786 {
787 *prev = SCM_CDR (walk);
788 break;
789 }
790 else
791 prev = SCM_CDRLOC (walk);
792 }
793
794 return lst;
795 }
796 #undef FUNC_NAME
797
798
799 SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
800 (SCM item, SCM lst),
801 "Like `delete!', but only deletes the first occurrence of ITEM from LST.\n"
802 "Tests for equality using `equal?'. See also `delq1!' and `delv1!'.")
803 #define FUNC_NAME s_scm_delete1_x
804 {
805 SCM walk;
806 SCM *prev;
807
808 for (prev = &lst, walk = lst;
809 SCM_CONSP (walk);
810 walk = SCM_CDR (walk))
811 {
812 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
813 {
814 *prev = SCM_CDR (walk);
815 break;
816 }
817 else
818 prev = SCM_CDRLOC (walk);
819 }
820
821 return lst;
822 }
823 #undef FUNC_NAME
824
825
826 \f
827 void
828 scm_init_list ()
829 {
830 #include "libguile/list.x"
831 }
832
833 /*
834 Local Variables:
835 c-file-style: "gnu"
836 End:
837 */