* *.c: Pervasive software-engineering-motivated rewrite of
[bpt/guile.git] / libguile / list.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
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
0f2d19dd
JB
45\f
46#include <stdio.h>
47#include "_scm.h"
20e6290e
JB
48#include "eq.h"
49
1bbd0b84 50#include "scm_validate.h"
20e6290e 51#include "list.h"
0f2d19dd
JB
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
df13742c 62/* creating lists */
0f2d19dd 63
df13742c 64/* SCM_P won't help us deal with varargs here. */
0f2d19dd
JB
65#ifdef __STDC__
66SCM
67scm_listify (SCM elt, ...)
68#else
69SCM
70scm_listify (elt, va_alist)
71 SCM elt;
72 va_dcl
0f2d19dd
JB
73#endif
74{
75 va_list foo;
76 SCM answer;
77 SCM *pos;
78
79 var_start (foo, elt);
80 answer = SCM_EOL;
81 pos = &answer;
82 while (elt != SCM_UNDEFINED)
83 {
84 *pos = scm_cons (elt, SCM_EOL);
25d8012c 85 pos = SCM_CDRLOC (*pos);
0f2d19dd
JB
86 elt = va_arg (foo, SCM);
87 }
88 return answer;
89}
90
91
1bbd0b84
GB
92GUILE_PROC(scm_list, "list", 0, 0, 1,
93 (SCM objs),
94"")
95#define FUNC_NAME s_scm_list
0f2d19dd
JB
96{
97 return objs;
98}
1bbd0b84 99#undef FUNC_NAME
0f2d19dd
JB
100
101
1bbd0b84
GB
102GUILE_PROC (scm_list_star, "list*", 1, 0, 1,
103 (SCM arg, SCM rest),
104"")
105#define FUNC_NAME s_scm_list_star
a610b8d9
MD
106{
107 if (SCM_NIMP (rest))
108 {
109 SCM prev = arg = scm_cons (arg, rest);
110 while (SCM_NIMP (SCM_CDR (rest)))
111 {
112 prev = rest;
113 rest = SCM_CDR (rest);
114 }
115 SCM_SETCDR (prev, SCM_CAR (rest));
116 }
117 return arg;
118}
1bbd0b84 119#undef FUNC_NAME
a610b8d9 120
0f2d19dd
JB
121
122\f
df13742c 123/* general questions about lists --- null?, list?, length, etc. */
0f2d19dd 124
1bbd0b84
GB
125GUILE_PROC(scm_null_p, "null?", 1, 0, 0,
126 (SCM x),
127"")
128#define FUNC_NAME s_scm_null_p
0f2d19dd 129{
1bbd0b84 130 return SCM_BOOL(SCM_NULLP(x));
0f2d19dd 131}
1bbd0b84 132#undef FUNC_NAME
0f2d19dd 133
1bbd0b84
GB
134GUILE_PROC(scm_list_p, "list?", 1, 0, 0,
135 (SCM x),
136"")
137#define FUNC_NAME s_scm_list_p
0f2d19dd 138{
1bbd0b84 139 return SCM_BOOL(scm_ilength(x)>=0);
0f2d19dd 140}
1bbd0b84 141#undef FUNC_NAME
0f2d19dd
JB
142
143
df13742c 144/* Return the length of SX, or -1 if it's not a proper list.
448a3bc2 145 This uses the "tortoise and hare" algorithm to detect "infinitely
df13742c
JB
146 long" lists (i.e. lists with cycles in their cdrs), and returns -1
147 if it does find one. */
0f2d19dd 148long
1bbd0b84 149scm_ilength(SCM sx)
0f2d19dd
JB
150{
151 register long i = 0;
448a3bc2 152 register SCM tortoise = sx;
df13742c
JB
153 register SCM hare = sx;
154
0f2d19dd 155 do {
ff467021
JB
156 if (SCM_IMP(hare)) return SCM_NULLP(hare) ? i : -1;
157 if (SCM_NCONSP(hare)) return -1;
df13742c 158 hare = SCM_CDR(hare);
0f2d19dd 159 i++;
ff467021
JB
160 if (SCM_IMP(hare)) return SCM_NULLP(hare) ? i : -1;
161 if (SCM_NCONSP(hare)) return -1;
df13742c 162 hare = SCM_CDR(hare);
0f2d19dd 163 i++;
448a3bc2
JB
164 /* For every two steps the hare takes, the tortoise takes one. */
165 tortoise = SCM_CDR(tortoise);
0f2d19dd 166 }
448a3bc2 167 while (hare != tortoise);
df13742c 168
448a3bc2 169 /* If the tortoise ever catches the hare, then the list must contain
df13742c 170 a cycle. */
0f2d19dd
JB
171 return -1;
172}
173
1bbd0b84
GB
174GUILE_PROC(scm_length, "length", 1, 0, 0,
175 (SCM lst),
176"")
177#define FUNC_NAME s_scm_length
0f2d19dd
JB
178{
179 int i;
1bbd0b84 180 SCM_VALIDATE_LIST_COPYLEN(1,lst,i);
0f2d19dd
JB
181 return SCM_MAKINUM (i);
182}
1bbd0b84 183#undef FUNC_NAME
0f2d19dd
JB
184
185
186\f
df13742c 187/* appending lists */
0f2d19dd 188
1bbd0b84
GB
189GUILE_PROC (scm_append, "append", 0, 0, 1,
190 (SCM args),
191"")
192#define FUNC_NAME s_scm_append
0f2d19dd
JB
193{
194 SCM res = SCM_EOL;
195 SCM *lloc = &res, arg;
ff467021 196 if (SCM_IMP(args)) {
1bbd0b84 197 SCM_VALIDATE_NULL(SCM_ARGn, args);
0f2d19dd
JB
198 return res;
199 }
1bbd0b84 200 SCM_VALIDATE_CONS(SCM_ARGn, args);
0f2d19dd
JB
201 while (1) {
202 arg = SCM_CAR(args);
203 args = SCM_CDR(args);
ff467021 204 if (SCM_IMP(args)) {
0f2d19dd 205 *lloc = arg;
1bbd0b84 206 SCM_VALIDATE_NULL(SCM_ARGn, args);
0f2d19dd
JB
207 return res;
208 }
1bbd0b84 209 SCM_VALIDATE_CONS(SCM_ARGn, args);
0f2d19dd 210 for(;SCM_NIMP(arg);arg = SCM_CDR(arg)) {
1bbd0b84 211 SCM_VALIDATE_CONS(SCM_ARGn, arg);
0f2d19dd 212 *lloc = scm_cons(SCM_CAR(arg), SCM_EOL);
25d8012c 213 lloc = SCM_CDRLOC(*lloc);
0f2d19dd 214 }
1bbd0b84 215 SCM_VALIDATE_NULL(SCM_ARGn, arg);
0f2d19dd
JB
216 }
217}
1bbd0b84 218#undef FUNC_NAME
0f2d19dd
JB
219
220
1bbd0b84
GB
221GUILE_PROC (scm_append_x, "append!", 0, 0, 1,
222 (SCM args),
223"")
224#define FUNC_NAME s_scm_append_x
0f2d19dd
JB
225{
226 SCM arg;
227 tail:
ff467021 228 if (SCM_NULLP(args)) return SCM_EOL;
0f2d19dd 229 arg = SCM_CAR(args);
0f2d19dd 230 args = SCM_CDR(args);
ff467021
JB
231 if (SCM_NULLP(args)) return arg;
232 if (SCM_NULLP(arg)) goto tail;
1bbd0b84 233 SCM_VALIDATE_NIMCONS(SCM_ARG1,arg);
92396c0a 234 SCM_SETCDR (scm_last_pair (arg), scm_append_x (args));
0f2d19dd
JB
235 return arg;
236}
1bbd0b84 237#undef FUNC_NAME
0f2d19dd
JB
238
239
1bbd0b84
GB
240GUILE_PROC(scm_last_pair, "last-pair", 1, 0, 0,
241 (SCM sx),
242"")
243#define FUNC_NAME s_scm_last_pair
df13742c
JB
244{
245 register SCM res = sx;
246 register SCM x;
0f2d19dd 247
df13742c
JB
248 if (SCM_NULLP (sx))
249 return SCM_EOL;
250
1bbd0b84 251 SCM_VALIDATE_NIMCONS(SCM_ARG1,res);
df13742c
JB
252 while (!0) {
253 x = SCM_CDR(res);
254 if (SCM_IMP(x) || SCM_NCONSP(x)) return res;
255 res = x;
256 x = SCM_CDR(res);
257 if (SCM_IMP(x) || SCM_NCONSP(x)) return res;
258 res = x;
259 sx = SCM_CDR(sx);
1bbd0b84 260 SCM_ASSERT(x != sx, sx, SCM_ARG1, FUNC_NAME);
df13742c
JB
261 }
262}
1bbd0b84 263#undef FUNC_NAME
df13742c
JB
264
265\f
266/* reversing lists */
0f2d19dd 267
1bbd0b84
GB
268GUILE_PROC (scm_reverse, "reverse", 1, 0, 0,
269 (SCM ls),
270"")
271#define FUNC_NAME s_scm_reverse
0f2d19dd 272{
3946f0de
MD
273 SCM res = SCM_EOL;
274 SCM p = ls, t = ls;
275 while (SCM_NIMP (p))
276 {
1bbd0b84 277 SCM_VALIDATE_CONS(1,ls);
3946f0de
MD
278 res = scm_cons (SCM_CAR (p), res);
279 p = SCM_CDR (p);
280 if (SCM_IMP (p))
281 break;
1bbd0b84 282 SCM_VALIDATE_CONS(1,ls);
3946f0de
MD
283 res = scm_cons (SCM_CAR (p), res);
284 p = SCM_CDR (p);
285 t = SCM_CDR (t);
286 if (t == p)
1bbd0b84 287 scm_misc_error (FUNC_NAME, "Circular structure: %S", SCM_LIST1 (ls));
3946f0de 288 }
1bbd0b84
GB
289 ls = p;
290 SCM_VALIDATE_NULL(1,ls);
3946f0de 291 return res;
0f2d19dd 292}
1bbd0b84 293#undef FUNC_NAME
0f2d19dd 294
1bbd0b84
GB
295GUILE_PROC (scm_reverse_x, "reverse!", 1, 1, 0,
296 (SCM ls, SCM new_tail),
297"")
298#define FUNC_NAME s_scm_reverse_x
0f2d19dd
JB
299{
300 SCM old_tail;
1bbd0b84 301 SCM_ASSERT (scm_ilength (ls) >= 0, ls, SCM_ARG1, FUNC_NAME);
3946f0de
MD
302 if (SCM_UNBNDP (new_tail))
303 new_tail = SCM_EOL;
304 else
1bbd0b84 305 SCM_ASSERT (scm_ilength (new_tail) >= 0, new_tail, SCM_ARG2, FUNC_NAME);
0f2d19dd 306
3946f0de
MD
307 while (SCM_NIMP (ls))
308 {
309 old_tail = SCM_CDR (ls);
310 SCM_SETCDR (ls, new_tail);
311 new_tail = ls;
312 ls = old_tail;
313 }
314 return new_tail;
0f2d19dd 315}
1bbd0b84 316#undef FUNC_NAME
0f2d19dd
JB
317
318
319\f
df13742c 320/* indexing lists by element number */
0f2d19dd 321
1bbd0b84
GB
322GUILE_PROC(scm_list_ref, "list-ref", 2, 0, 0,
323 (SCM lst, SCM k),
324"")
325#define FUNC_NAME s_scm_list_ref
326{
327 register long i;
328 SCM_VALIDATE_INT_MIN_COPY(2,k,0,i);
329 while (i-- > 0) {
330 SCM_ASRTGO(SCM_NIMP(lst) && SCM_CONSP(lst), erout);
331 lst = SCM_CDR(lst);
332 }
333 erout:
334 SCM_ASSERT(SCM_NIMP(lst) && SCM_CONSP(lst),
335 SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
336 return SCM_CAR(lst);
0f2d19dd 337}
1bbd0b84 338#undef FUNC_NAME
0f2d19dd 339
1bbd0b84
GB
340GUILE_PROC(scm_list_set_x, "list-set!", 3, 0, 0,
341 (SCM lst, SCM k, SCM val),
342"")
343#define FUNC_NAME s_scm_list_set_x
344{
345 register long i;
346 SCM_VALIDATE_INT_MIN_COPY(2,k,0,i);
347 while (i-- > 0) {
348 SCM_ASRTGO(SCM_NIMP(lst) && SCM_CONSP(lst), erout);
349 lst = SCM_CDR(lst);
350 }
351 erout:
352 SCM_ASSERT(SCM_NIMP(lst) && SCM_CONSP(lst),
353 SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
354 SCM_SETCAR (lst, val);
355 return val;
0f2d19dd 356}
1bbd0b84 357#undef FUNC_NAME
0f2d19dd
JB
358
359
1bbd0b84
GB
360SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail);
361
362GUILE_PROC(scm_list_tail, "list-tail", 2, 0, 0,
363 (SCM lst, SCM k),
364"")
365#define FUNC_NAME s_scm_list_tail
df13742c
JB
366{
367 register long i;
1bbd0b84 368 SCM_VALIDATE_INT_MIN_COPY(2,k,0,i);
df13742c 369 while (i-- > 0) {
1bbd0b84 370 SCM_VALIDATE_NIMCONS(1,lst);
df13742c
JB
371 lst = SCM_CDR(lst);
372 }
373 return lst;
374}
1bbd0b84 375#undef FUNC_NAME
df13742c 376
0f2d19dd 377
1bbd0b84
GB
378GUILE_PROC(scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0,
379 (SCM lst, SCM k, SCM val),
380"")
381#define FUNC_NAME s_scm_list_cdr_set_x
382{
383 register long i;
384 SCM_VALIDATE_INT_MIN_COPY(2,k,0,i);
385 while (i-- > 0) {
386 SCM_ASRTGO(SCM_NIMP(lst) && SCM_CONSP(lst), erout);
387 lst = SCM_CDR(lst);
388 }
389erout:
390 SCM_ASSERT(SCM_NIMP(lst) && SCM_CONSP(lst),
391 SCM_NULLP(lst)?k:lst, SCM_NULLP(lst)?SCM_OUTOFRANGE:SCM_ARG1, FUNC_NAME);
392 SCM_SETCDR (lst, val);
393 return val;
0f2d19dd 394}
1bbd0b84 395#undef FUNC_NAME
0f2d19dd
JB
396
397
398\f
df13742c 399/* copying lists, perhaps partially */
0f2d19dd 400
1bbd0b84
GB
401GUILE_PROC(scm_list_head, "list-head", 2, 0, 0,
402 (SCM lst, SCM k),
403"")
404#define FUNC_NAME s_scm_list_head
0f2d19dd
JB
405{
406 SCM answer;
407 SCM * pos;
408 register long i;
409
1bbd0b84 410 SCM_VALIDATE_INT_MIN_COPY(2,k,0,i);
0f2d19dd
JB
411 answer = SCM_EOL;
412 pos = &answer;
0f2d19dd
JB
413 while (i-- > 0)
414 {
1bbd0b84 415 SCM_VALIDATE_NIMCONS(1,lst);
0f2d19dd 416 *pos = scm_cons (SCM_CAR (lst), SCM_EOL);
25d8012c 417 pos = SCM_CDRLOC (*pos);
0f2d19dd
JB
418 lst = SCM_CDR(lst);
419 }
420 return answer;
421}
1bbd0b84 422#undef FUNC_NAME
0f2d19dd
JB
423
424
1bbd0b84
GB
425GUILE_PROC (scm_list_copy, "list-copy", 1, 0, 0,
426 (SCM lst),
427"")
428#define FUNC_NAME s_scm_list_copy
df13742c
JB
429{
430 SCM newlst;
431 SCM * fill_here;
432 SCM from_here;
433
434 newlst = SCM_EOL;
435 fill_here = &newlst;
436 from_here = lst;
437
438 while (SCM_NIMP (from_here) && SCM_CONSP (from_here))
439 {
440 SCM c;
441 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
442 *fill_here = c;
25d8012c 443 fill_here = SCM_CDRLOC (c);
df13742c
JB
444 from_here = SCM_CDR (from_here);
445 }
446 return newlst;
447}
1bbd0b84 448#undef FUNC_NAME
df13742c 449
0f2d19dd 450\f
df13742c
JB
451/* membership tests (memq, memv, etc.) */
452
1bbd0b84
GB
453GUILE_PROC (scm_sloppy_memq, "sloppy-memq", 2, 0, 0,
454 (SCM x, SCM lst),
455"")
456#define FUNC_NAME s_scm_sloppy_memq
0f2d19dd
JB
457{
458 for(; SCM_NIMP(lst) && SCM_CONSP (lst); lst = SCM_CDR(lst))
459 {
460 if (SCM_CAR(lst)==x)
461 return lst;
462 }
463 return lst;
464}
1bbd0b84 465#undef FUNC_NAME
0f2d19dd
JB
466
467
1bbd0b84
GB
468GUILE_PROC (scm_sloppy_memv, "sloppy-memv", 2, 0, 0,
469 (SCM x, SCM lst),
470"")
471#define FUNC_NAME s_scm_sloppy_memv
0f2d19dd
JB
472{
473 for(; SCM_NIMP(lst) && SCM_CONSP (lst); lst = SCM_CDR(lst))
474 {
475 if (SCM_BOOL_F != scm_eqv_p (SCM_CAR(lst), x))
476 return lst;
477 }
478 return lst;
479}
1bbd0b84 480#undef FUNC_NAME
0f2d19dd
JB
481
482
1bbd0b84
GB
483GUILE_PROC (scm_sloppy_member, "sloppy-member", 2, 0, 0,
484 (SCM x, SCM lst),
485"")
486#define FUNC_NAME s_scm_sloppy_member
0f2d19dd
JB
487{
488 for(; SCM_NIMP(lst) && SCM_CONSP (lst); lst = SCM_CDR(lst))
489 {
490 if (SCM_BOOL_F != scm_equal_p (SCM_CAR(lst), x))
491 return lst;
492 }
493 return lst;
494}
1bbd0b84 495#undef FUNC_NAME
0f2d19dd
JB
496
497
498
1bbd0b84
GB
499GUILE_PROC(scm_memq, "memq", 2, 0, 0,
500 (SCM x, SCM lst),
501"")
502#define FUNC_NAME s_scm_memq
0f2d19dd
JB
503{
504 SCM answer;
1bbd0b84 505 SCM_VALIDATE_LIST(2,lst);
0f2d19dd 506 answer = scm_sloppy_memq (x, lst);
df13742c 507 return (answer == SCM_EOL) ? SCM_BOOL_F : answer;
0f2d19dd 508}
1bbd0b84 509#undef FUNC_NAME
0f2d19dd
JB
510
511
512
1bbd0b84
GB
513GUILE_PROC(scm_memv, "memv", 2, 0, 0,
514 (SCM x, SCM lst),
515"")
516#define FUNC_NAME s_scm_memv
0f2d19dd
JB
517{
518 SCM answer;
1bbd0b84 519 SCM_VALIDATE_LIST(2,lst);
0f2d19dd 520 answer = scm_sloppy_memv (x, lst);
df13742c 521 return (answer == SCM_EOL) ? SCM_BOOL_F : answer;
0f2d19dd 522}
1bbd0b84 523#undef FUNC_NAME
0f2d19dd
JB
524
525
1bbd0b84
GB
526GUILE_PROC(scm_member, "member", 2, 0, 0,
527 (SCM x, SCM lst),
528"")
529#define FUNC_NAME s_scm_member
0f2d19dd
JB
530{
531 SCM answer;
1bbd0b84 532 SCM_VALIDATE_LIST(2,lst);
0f2d19dd 533 answer = scm_sloppy_member (x, lst);
df13742c 534 return (answer == SCM_EOL) ? SCM_BOOL_F : answer;
0f2d19dd 535}
1bbd0b84 536#undef FUNC_NAME
0f2d19dd
JB
537
538
539\f
df13742c 540/* deleting elements from a list (delq, etc.) */
0f2d19dd 541
1bbd0b84
GB
542GUILE_PROC(scm_delq_x, "delq!", 2, 0, 0,
543 (SCM item, SCM lst),
544"")
545#define FUNC_NAME s_scm_delq_x
0f2d19dd 546{
164271a1
JB
547 SCM walk;
548 SCM *prev;
0f2d19dd 549
164271a1
JB
550 for (prev = &lst, walk = lst;
551 SCM_NIMP (walk) && SCM_CONSP (walk);
552 walk = SCM_CDR (walk))
0f2d19dd 553 {
164271a1
JB
554 if (SCM_CAR (walk) == item)
555 *prev = SCM_CDR (walk);
556 else
557 prev = SCM_CDRLOC (walk);
0f2d19dd 558 }
164271a1
JB
559
560 return lst;
0f2d19dd 561}
1bbd0b84 562#undef FUNC_NAME
0f2d19dd
JB
563
564
1bbd0b84
GB
565GUILE_PROC(scm_delv_x, "delv!", 2, 0, 0,
566 (SCM item, SCM lst),
567"")
568#define FUNC_NAME s_scm_delv_x
0f2d19dd 569{
164271a1
JB
570 SCM walk;
571 SCM *prev;
0f2d19dd 572
164271a1
JB
573 for (prev = &lst, walk = lst;
574 SCM_NIMP (walk) && SCM_CONSP (walk);
575 walk = SCM_CDR (walk))
0f2d19dd 576 {
164271a1
JB
577 if (SCM_BOOL_F != scm_eqv_p (SCM_CAR (walk), item))
578 *prev = SCM_CDR (walk);
579 else
580 prev = SCM_CDRLOC (walk);
0f2d19dd 581 }
164271a1
JB
582
583 return lst;
0f2d19dd 584}
1bbd0b84 585#undef FUNC_NAME
0f2d19dd
JB
586
587
588
1bbd0b84
GB
589GUILE_PROC(scm_delete_x, "delete!", 2, 0, 0,
590 (SCM item, SCM lst),
591"")
592#define FUNC_NAME s_scm_delete_x
0f2d19dd 593{
164271a1
JB
594 SCM walk;
595 SCM *prev;
0f2d19dd 596
164271a1
JB
597 for (prev = &lst, walk = lst;
598 SCM_NIMP (walk) && SCM_CONSP (walk);
599 walk = SCM_CDR (walk))
0f2d19dd 600 {
164271a1
JB
601 if (SCM_BOOL_F != scm_equal_p (SCM_CAR (walk), item))
602 *prev = SCM_CDR (walk);
603 else
604 prev = SCM_CDRLOC (walk);
0f2d19dd 605 }
164271a1
JB
606
607 return lst;
0f2d19dd 608}
1bbd0b84 609#undef FUNC_NAME
0f2d19dd
JB
610
611
612\f
613
0f2d19dd 614
1bbd0b84
GB
615GUILE_PROC (scm_delq, "delq", 2, 0, 0,
616 (SCM item, SCM lst),
617"")
618#define FUNC_NAME s_scm_delq
0f2d19dd 619{
1bbd0b84 620 SCM copy = scm_list_copy (lst);
0f2d19dd
JB
621 return scm_delq_x (item, copy);
622}
1bbd0b84 623#undef FUNC_NAME
0f2d19dd 624
1bbd0b84
GB
625GUILE_PROC (scm_delv, "delv", 2, 0, 0,
626 (SCM item, SCM lst),
627"")
628#define FUNC_NAME s_scm_delv
0f2d19dd 629{
1bbd0b84 630 SCM copy = scm_list_copy (lst);
0f2d19dd
JB
631 return scm_delv_x (item, copy);
632}
1bbd0b84 633#undef FUNC_NAME
0f2d19dd 634
1bbd0b84
GB
635GUILE_PROC (scm_delete, "delete", 2, 0, 0,
636 (SCM item, SCM lst),
637"")
638#define FUNC_NAME s_scm_delete
0f2d19dd 639{
1bbd0b84 640 SCM copy = scm_list_copy (lst);
0f2d19dd
JB
641 return scm_delete_x (item, copy);
642}
1bbd0b84 643#undef FUNC_NAME
0f2d19dd
JB
644
645
1bbd0b84
GB
646GUILE_PROC(scm_delq1_x, "delq1!", 2, 0, 0,
647 (SCM item, SCM lst),
648"")
649#define FUNC_NAME s_scm_delq1_x
82dc9f57
MD
650{
651 SCM walk;
652 SCM *prev;
653
654 for (prev = &lst, walk = lst;
655 SCM_NIMP (walk) && SCM_CONSP (walk);
656 walk = SCM_CDR (walk))
657 {
658 if (SCM_CAR (walk) == item)
659 {
660 *prev = SCM_CDR (walk);
661 break;
662 }
663 else
664 prev = SCM_CDRLOC (walk);
665 }
666
667 return lst;
668}
1bbd0b84 669#undef FUNC_NAME
82dc9f57
MD
670
671
1bbd0b84
GB
672GUILE_PROC(scm_delv1_x, "delv1!", 2, 0, 0,
673 (SCM item, SCM lst),
674"")
675#define FUNC_NAME s_scm_delv1_x
82dc9f57
MD
676{
677 SCM walk;
678 SCM *prev;
679
680 for (prev = &lst, walk = lst;
681 SCM_NIMP (walk) && SCM_CONSP (walk);
682 walk = SCM_CDR (walk))
683 {
684 if (SCM_BOOL_F != scm_eqv_p (SCM_CAR (walk), item))
685 {
686 *prev = SCM_CDR (walk);
687 break;
688 }
689 else
690 prev = SCM_CDRLOC (walk);
691 }
692
693 return lst;
694}
1bbd0b84 695#undef FUNC_NAME
82dc9f57
MD
696
697
1bbd0b84
GB
698GUILE_PROC(scm_delete1_x, "delete1!", 2, 0, 0,
699 (SCM item, SCM lst),
700"")
701#define FUNC_NAME s_scm_delete1_x
82dc9f57
MD
702{
703 SCM walk;
704 SCM *prev;
705
706 for (prev = &lst, walk = lst;
707 SCM_NIMP (walk) && SCM_CONSP (walk);
708 walk = SCM_CDR (walk))
709 {
710 if (SCM_BOOL_F != scm_equal_p (SCM_CAR (walk), item))
711 {
712 *prev = SCM_CDR (walk);
713 break;
714 }
715 else
716 prev = SCM_CDRLOC (walk);
717 }
718
719 return lst;
720}
1bbd0b84 721#undef FUNC_NAME
82dc9f57
MD
722
723
0f2d19dd 724\f
0f2d19dd
JB
725void
726scm_init_list ()
0f2d19dd
JB
727{
728#include "list.x"
729}