6493df8de265e1eab58205afcc197eaabc4f6e08
[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 SCM_VALIDATE_LIST (1, lst);
489
490 newlst = SCM_EOL;
491 fill_here = &newlst;
492 from_here = lst;
493
494 while (SCM_CONSP (from_here))
495 {
496 SCM c;
497 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
498 *fill_here = c;
499 fill_here = SCM_CDRLOC (c);
500 from_here = SCM_CDR (from_here);
501 }
502 return newlst;
503 }
504 #undef FUNC_NAME
505
506 \f
507 /* membership tests (memq, memv, etc.) */
508
509 #if SCM_DEBUG_DEPRECATED == 0
510
511 SCM_DEFINE (scm_sloppy_memq, "sloppy-memq", 2, 0, 0,
512 (SCM x, SCM lst),
513 "This procedure behaves like @code{memq}, but does no type or error checking.\n"
514 "Its use is recommended only in writing Guile internals,\n"
515 "not for high-level Scheme programs.")
516 #define FUNC_NAME s_scm_sloppy_memq
517 {
518 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
519 {
520 if (SCM_EQ_P (SCM_CAR (lst), x))
521 return lst;
522 }
523 return lst;
524 }
525 #undef FUNC_NAME
526
527
528 SCM_DEFINE (scm_sloppy_memv, "sloppy-memv", 2, 0, 0,
529 (SCM x, SCM lst),
530 "This procedure behaves like @code{memv}, but does no type or error checking.\n"
531 "Its use is recommended only in writing Guile internals,\n"
532 "not for high-level Scheme programs.")
533 #define FUNC_NAME s_scm_sloppy_memv
534 {
535 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
536 {
537 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
538 return lst;
539 }
540 return lst;
541 }
542 #undef FUNC_NAME
543
544
545 SCM_DEFINE (scm_sloppy_member, "sloppy-member", 2, 0, 0,
546 (SCM x, SCM lst),
547 "This procedure behaves like @code{member}, but does no type or error checking.\n"
548 "Its use is recommended only in writing Guile internals,\n"
549 "not for high-level Scheme programs.")
550 #define FUNC_NAME s_scm_sloppy_member
551 {
552 for(; SCM_CONSP (lst); lst = SCM_CDR(lst))
553 {
554 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
555 return lst;
556 }
557 return lst;
558 }
559 #undef FUNC_NAME
560
561 #endif /* DEPRECATED */
562
563 /* The function scm_c_memq returns the first sublist of list whose car is
564 * 'eq?' obj, where the sublists of list are the non-empty lists returned by
565 * (list-tail list k) for k less than the length of list. If obj does not
566 * occur in list, then #f (not the empty list) is returned. (r5rs)
567 * List must be a proper list, otherwise scm_c_memq may crash or loop
568 * endlessly.
569 */
570 SCM
571 scm_c_memq (SCM obj, SCM list)
572 {
573 for (; !SCM_NULLP (list); list = SCM_CDR (list))
574 {
575 if (SCM_EQ_P (SCM_CAR (list), obj))
576 return list;
577 }
578 return SCM_BOOL_F;
579 }
580
581
582 SCM_DEFINE (scm_memq, "memq", 2, 0, 0,
583 (SCM x, SCM lst),
584 "Return the first sublist of LST whose car is `eq?' 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_memq
590 {
591 SCM_VALIDATE_LIST (2, lst);
592 return scm_c_memq (x, lst);
593 }
594 #undef FUNC_NAME
595
596
597
598 SCM_DEFINE (scm_memv, "memv", 2, 0, 0,
599 (SCM x, SCM lst),
600 "Return the first sublist of LST whose car is `eqv?' to X\n"
601 "where the sublists of LST are the non-empty lists returned\n"
602 "by `(list-tail LST K)' for K less than the length of LST. If\n"
603 "X does not occur in LST, then `#f' (not the empty list) is\n"
604 "returned.")
605 #define FUNC_NAME s_scm_memv
606 {
607 SCM_VALIDATE_LIST (2, lst);
608 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
609 {
610 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x)))
611 return lst;
612 }
613 return SCM_BOOL_F;
614 }
615 #undef FUNC_NAME
616
617
618 SCM_DEFINE (scm_member, "member", 2, 0, 0,
619 (SCM x, SCM lst),
620 "Return the first sublist of LST whose car is `equal?' to X\n"
621 "where the sublists of LST are the non-empty lists returned\n"
622 "by `(list-tail LST K)' for K less than the length of LST. If\n"
623 "X does not occur in LST, then `#f' (not the empty list) is\n"
624 "returned.")
625 #define FUNC_NAME s_scm_member
626 {
627 SCM_VALIDATE_LIST (2, lst);
628 for (; !SCM_NULLP (lst); lst = SCM_CDR (lst))
629 {
630 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x)))
631 return lst;
632 }
633 return SCM_BOOL_F;
634 }
635 #undef FUNC_NAME
636
637
638 \f
639 /* deleting elements from a list (delq, etc.) */
640
641 SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0,
642 (SCM item, SCM lst),
643 "@deffnx primitive delv! item lst\n"
644 "@deffnx primitive delete! item lst\n"
645 "These procedures are destructive versions of @code{delq}, @code{delv}\n"
646 "and @code{delete}: they modify the pointers in the existing @var{lst}\n"
647 "rather than creating a new list. Caveat evaluator: Like other\n"
648 "destructive list functions, these functions cannot modify the binding of\n"
649 "@var{lst}, and so cannot be used to delete the first element of\n"
650 "@var{lst} destructively.")
651 #define FUNC_NAME s_scm_delq_x
652 {
653 SCM walk;
654 SCM *prev;
655
656 for (prev = &lst, walk = lst;
657 SCM_CONSP (walk);
658 walk = SCM_CDR (walk))
659 {
660 if (SCM_EQ_P (SCM_CAR (walk), item))
661 *prev = SCM_CDR (walk);
662 else
663 prev = SCM_CDRLOC (walk);
664 }
665
666 return lst;
667 }
668 #undef FUNC_NAME
669
670
671 SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0,
672 (SCM item, SCM lst),
673 "Destructively remove all elements from LST that are `eqv?' to ITEM.")
674 #define FUNC_NAME s_scm_delv_x
675 {
676 SCM walk;
677 SCM *prev;
678
679 for (prev = &lst, walk = lst;
680 SCM_CONSP (walk);
681 walk = SCM_CDR (walk))
682 {
683 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
684 *prev = SCM_CDR (walk);
685 else
686 prev = SCM_CDRLOC (walk);
687 }
688
689 return lst;
690 }
691 #undef FUNC_NAME
692
693
694
695 SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0,
696 (SCM item, SCM lst),
697 "Destructively remove all elements from LST that are `equal?' to ITEM.")
698 #define FUNC_NAME s_scm_delete_x
699 {
700 SCM walk;
701 SCM *prev;
702
703 for (prev = &lst, walk = lst;
704 SCM_CONSP (walk);
705 walk = SCM_CDR (walk))
706 {
707 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
708 *prev = SCM_CDR (walk);
709 else
710 prev = SCM_CDRLOC (walk);
711 }
712
713 return lst;
714 }
715 #undef FUNC_NAME
716
717
718 \f
719
720
721 SCM_DEFINE (scm_delq, "delq", 2, 0, 0,
722 (SCM item, SCM lst),
723 "Return a newly-created copy of @var{lst} with elements `eq?' to @var{item} removed.\n"
724 "This procedure mirrors @code{memq}:\n"
725 "@code{delq} compares elements of @var{lst} against @var{item} with\n"
726 "@code{eq?}.")
727 #define FUNC_NAME s_scm_delq
728 {
729 SCM copy = scm_list_copy (lst);
730 return scm_delq_x (item, copy);
731 }
732 #undef FUNC_NAME
733
734 SCM_DEFINE (scm_delv, "delv", 2, 0, 0,
735 (SCM item, SCM lst),
736 "Return a newly-created copy of @var{lst} with elements `eqv?' to @var{item} removed.\n"
737 "This procedure mirrors @code{memv}:\n"
738 "@code{delv} compares elements of @var{lst} against @var{item} with\n"
739 "@code{eqv?}.")
740 #define FUNC_NAME s_scm_delv
741 {
742 SCM copy = scm_list_copy (lst);
743 return scm_delv_x (item, copy);
744 }
745 #undef FUNC_NAME
746
747 SCM_DEFINE (scm_delete, "delete", 2, 0, 0,
748 (SCM item, SCM lst),
749 "Return a newly-created copy of @var{lst} with elements `equal?' to @var{item} removed.\n"
750 "This procedure mirrors @code{member}:\n"
751 "@code{delete} compares elements of @var{lst} against @var{item} with\n"
752 "@code{equal?}.")
753 #define FUNC_NAME s_scm_delete
754 {
755 SCM copy = scm_list_copy (lst);
756 return scm_delete_x (item, copy);
757 }
758 #undef FUNC_NAME
759
760
761 SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0,
762 (SCM item, SCM lst),
763 "Like `delq!', but only deletes the first occurrence of ITEM from LST.\n"
764 "Tests for equality using `eq?'. See also `delv1!' and `delete1!'.")
765 #define FUNC_NAME s_scm_delq1_x
766 {
767 SCM walk;
768 SCM *prev;
769
770 for (prev = &lst, walk = lst;
771 SCM_CONSP (walk);
772 walk = SCM_CDR (walk))
773 {
774 if (SCM_EQ_P (SCM_CAR (walk), item))
775 {
776 *prev = SCM_CDR (walk);
777 break;
778 }
779 else
780 prev = SCM_CDRLOC (walk);
781 }
782
783 return lst;
784 }
785 #undef FUNC_NAME
786
787
788 SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0,
789 (SCM item, SCM lst),
790 "Like `delv!', but only deletes the first occurrence of ITEM from LST.\n"
791 "Tests for equality using `eqv?'. See also `delq1!' and `delete1!'.")
792 #define FUNC_NAME s_scm_delv1_x
793 {
794 SCM walk;
795 SCM *prev;
796
797 for (prev = &lst, walk = lst;
798 SCM_CONSP (walk);
799 walk = SCM_CDR (walk))
800 {
801 if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item)))
802 {
803 *prev = SCM_CDR (walk);
804 break;
805 }
806 else
807 prev = SCM_CDRLOC (walk);
808 }
809
810 return lst;
811 }
812 #undef FUNC_NAME
813
814
815 SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0,
816 (SCM item, SCM lst),
817 "Like `delete!', but only deletes the first occurrence of ITEM from LST.\n"
818 "Tests for equality using `equal?'. See also `delq1!' and `delv1!'.")
819 #define FUNC_NAME s_scm_delete1_x
820 {
821 SCM walk;
822 SCM *prev;
823
824 for (prev = &lst, walk = lst;
825 SCM_CONSP (walk);
826 walk = SCM_CDR (walk))
827 {
828 if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item)))
829 {
830 *prev = SCM_CDR (walk);
831 break;
832 }
833 else
834 prev = SCM_CDRLOC (walk);
835 }
836
837 return lst;
838 }
839 #undef FUNC_NAME
840
841
842 \f
843 void
844 scm_init_list ()
845 {
846 #ifndef SCM_MAGIC_SNARFER
847 #include "libguile/list.x"
848 #endif
849 }
850
851 /*
852 Local Variables:
853 c-file-style: "gnu"
854 End:
855 */