Commit | Line | Data |
---|---|---|
71a89639 | 1 | /* Copyright (C) 1995,1996,1997,2000,2001, 2003, 2004 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
73be1d9e MV |
8 | * This library 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 GNU | |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
a0599745 MD |
20 | #include "libguile/_scm.h" |
21 | #include "libguile/eq.h" | |
c96d76b8 | 22 | #include "libguile/lang.h" |
20e6290e | 23 | |
a0599745 MD |
24 | #include "libguile/validate.h" |
25 | #include "libguile/list.h" | |
c614a00b | 26 | #include "libguile/eval.h" |
0f2d19dd | 27 | |
0f2d19dd | 28 | #include <stdarg.h> |
0f2d19dd JB |
29 | |
30 | \f | |
df13742c | 31 | /* creating lists */ |
0f2d19dd | 32 | |
34d19ef6 | 33 | #define SCM_I_CONS(cell, x, y) \ |
1afff620 | 34 | do { \ |
228a24ef | 35 | cell = scm_cell ((scm_t_bits)x, (scm_t_bits)y); \ |
1afff620 KN |
36 | } while (0) |
37 | ||
38 | SCM | |
39 | scm_list_1 (SCM e1) | |
40 | { | |
41 | SCM c1; | |
42 | SCM_I_CONS (c1, e1, SCM_EOL); | |
43 | return c1; | |
44 | } | |
45 | ||
46 | SCM | |
47 | scm_list_2 (SCM e1, SCM e2) | |
48 | { | |
49 | SCM c1, c2; | |
50 | SCM_I_CONS (c2, e2, SCM_EOL); | |
51 | SCM_I_CONS (c1, e1, c2); | |
52 | return c1; | |
53 | } | |
54 | ||
55 | SCM | |
56 | scm_list_3 (SCM e1, SCM e2, SCM e3) | |
57 | { | |
58 | SCM c1, c2, c3; | |
59 | SCM_I_CONS (c3, e3, SCM_EOL); | |
60 | SCM_I_CONS (c2, e2, c3); | |
61 | SCM_I_CONS (c1, e1, c2); | |
62 | return c1; | |
63 | } | |
64 | ||
65 | SCM | |
66 | scm_list_4 (SCM e1, SCM e2, SCM e3, SCM e4) | |
67 | { | |
68 | return scm_cons2 (e1, e2, scm_list_2 (e3, e4)); | |
69 | } | |
70 | ||
71 | SCM | |
72 | scm_list_5 (SCM e1, SCM e2, SCM e3, SCM e4, SCM e5) | |
73 | { | |
74 | return scm_cons2 (e1, e2, scm_list_3 (e3, e4, e5)); | |
75 | } | |
76 | ||
0f2d19dd | 77 | SCM |
1afff620 | 78 | scm_list_n (SCM elt, ...) |
0f2d19dd JB |
79 | { |
80 | va_list foo; | |
2de14ecd GB |
81 | SCM answer = SCM_EOL; |
82 | SCM *pos = &answer; | |
0f2d19dd | 83 | |
0dbfb25a | 84 | va_start (foo, elt); |
fbd485ba | 85 | while (! SCM_UNBNDP (elt)) |
0f2d19dd | 86 | { |
1e55d288 | 87 | #if (SCM_DEBUG_CELL_ACCESSES == 1) |
eb741d98 HWN |
88 | if (SCM_NIMP (elt)) |
89 | SCM_VALIDATE_CELL(elt, 0); | |
1e55d288 | 90 | #endif |
0f2d19dd | 91 | *pos = scm_cons (elt, SCM_EOL); |
25d8012c | 92 | pos = SCM_CDRLOC (*pos); |
0f2d19dd JB |
93 | elt = va_arg (foo, SCM); |
94 | } | |
de90ef0f | 95 | va_end (foo); |
0f2d19dd JB |
96 | return answer; |
97 | } | |
98 | ||
99 | ||
3b3b36dd | 100 | SCM_DEFINE (scm_list, "list", 0, 0, 1, |
1bbd0b84 | 101 | (SCM objs), |
5352393c MG |
102 | "Return a list containing @var{objs}, the arguments to\n" |
103 | "@code{list}.") | |
1bbd0b84 | 104 | #define FUNC_NAME s_scm_list |
0f2d19dd JB |
105 | { |
106 | return objs; | |
107 | } | |
1bbd0b84 | 108 | #undef FUNC_NAME |
0f2d19dd JB |
109 | |
110 | ||
26a379b2 | 111 | SCM_DEFINE (scm_cons_star, "cons*", 1, 0, 1, |
1bbd0b84 | 112 | (SCM arg, SCM rest), |
5352393c MG |
113 | "Like @code{list}, but the last arg provides the tail of the\n" |
114 | "constructed list, returning @code{(cons @var{arg1} (cons\n" | |
a17bb5fd | 115 | "@var{arg2} (cons @dots{} @var{argn})))}. Requires at least one\n" |
5352393c MG |
116 | "argument. If given one argument, that argument is returned as\n" |
117 | "result. This function is called @code{list*} in some other\n" | |
118 | "Schemes and in Common LISP.") | |
26a379b2 | 119 | #define FUNC_NAME s_scm_cons_star |
a610b8d9 | 120 | { |
af45e3b0 DH |
121 | SCM_VALIDATE_REST_ARGUMENT (rest); |
122 | if (!SCM_NULLP (rest)) | |
a610b8d9 MD |
123 | { |
124 | SCM prev = arg = scm_cons (arg, rest); | |
9ff1720f | 125 | while (!SCM_NULLP (SCM_CDR (rest))) |
a610b8d9 MD |
126 | { |
127 | prev = rest; | |
128 | rest = SCM_CDR (rest); | |
129 | } | |
130 | SCM_SETCDR (prev, SCM_CAR (rest)); | |
131 | } | |
132 | return arg; | |
133 | } | |
1bbd0b84 | 134 | #undef FUNC_NAME |
a610b8d9 | 135 | |
0f2d19dd JB |
136 | |
137 | \f | |
df13742c | 138 | /* general questions about lists --- null?, list?, length, etc. */ |
0f2d19dd | 139 | |
3b3b36dd | 140 | SCM_DEFINE (scm_null_p, "null?", 1, 0, 0, |
1bbd0b84 | 141 | (SCM x), |
5352393c | 142 | "Return @code{#t} iff @var{x} is the empty list, else @code{#f}.") |
1bbd0b84 | 143 | #define FUNC_NAME s_scm_null_p |
0f2d19dd | 144 | { |
c96d76b8 | 145 | return SCM_BOOL (SCM_NULL_OR_NIL_P (x)); |
0f2d19dd | 146 | } |
1bbd0b84 | 147 | #undef FUNC_NAME |
0f2d19dd | 148 | |
2de14ecd | 149 | |
3b3b36dd | 150 | SCM_DEFINE (scm_list_p, "list?", 1, 0, 0, |
1bbd0b84 | 151 | (SCM x), |
5352393c | 152 | "Return @code{#t} iff @var{x} is a proper list, else @code{#f}.") |
1bbd0b84 | 153 | #define FUNC_NAME s_scm_list_p |
0f2d19dd | 154 | { |
2de14ecd | 155 | return SCM_BOOL (scm_ilength (x) >= 0); |
0f2d19dd | 156 | } |
1bbd0b84 | 157 | #undef FUNC_NAME |
0f2d19dd JB |
158 | |
159 | ||
df13742c | 160 | /* Return the length of SX, or -1 if it's not a proper list. |
448a3bc2 | 161 | This uses the "tortoise and hare" algorithm to detect "infinitely |
df13742c JB |
162 | long" lists (i.e. lists with cycles in their cdrs), and returns -1 |
163 | if it does find one. */ | |
c014a02e ML |
164 | long |
165 | scm_ilength(SCM sx) | |
0f2d19dd | 166 | { |
c014a02e | 167 | long i = 0; |
e1385ffc GB |
168 | SCM tortoise = sx; |
169 | SCM hare = sx; | |
df13742c | 170 | |
0f2d19dd | 171 | do { |
c96d76b8 | 172 | if (SCM_NULL_OR_NIL_P(hare)) return i; |
1685446c | 173 | if (!SCM_CONSP (hare)) return -1; |
df13742c | 174 | hare = SCM_CDR(hare); |
0f2d19dd | 175 | i++; |
c96d76b8 | 176 | if (SCM_NULL_OR_NIL_P(hare)) return i; |
1685446c | 177 | if (!SCM_CONSP (hare)) return -1; |
df13742c | 178 | hare = SCM_CDR(hare); |
0f2d19dd | 179 | i++; |
448a3bc2 JB |
180 | /* For every two steps the hare takes, the tortoise takes one. */ |
181 | tortoise = SCM_CDR(tortoise); | |
0f2d19dd | 182 | } |
fbd485ba | 183 | while (! SCM_EQ_P (hare, tortoise)); |
df13742c | 184 | |
448a3bc2 | 185 | /* If the tortoise ever catches the hare, then the list must contain |
df13742c | 186 | a cycle. */ |
0f2d19dd JB |
187 | return -1; |
188 | } | |
189 | ||
2de14ecd | 190 | |
3b3b36dd | 191 | SCM_DEFINE (scm_length, "length", 1, 0, 0, |
1bbd0b84 | 192 | (SCM lst), |
5352393c | 193 | "Return the number of elements in list @var{lst}.") |
1bbd0b84 | 194 | #define FUNC_NAME s_scm_length |
0f2d19dd | 195 | { |
c014a02e | 196 | long i; |
34d19ef6 | 197 | SCM_VALIDATE_LIST_COPYLEN (1, lst, i); |
0f2d19dd JB |
198 | return SCM_MAKINUM (i); |
199 | } | |
1bbd0b84 | 200 | #undef FUNC_NAME |
0f2d19dd JB |
201 | |
202 | ||
203 | \f | |
df13742c | 204 | /* appending lists */ |
0f2d19dd | 205 | |
a1ec6916 | 206 | SCM_DEFINE (scm_append, "append", 0, 0, 1, |
1bbd0b84 | 207 | (SCM args), |
5352393c MG |
208 | "Return a list consisting of the elements the lists passed as\n" |
209 | "arguments.\n" | |
1e6808ea | 210 | "@lisp\n" |
5352393c MG |
211 | "(append '(x) '(y)) @result{} (x y)\n" |
212 | "(append '(a) '(b c d)) @result{} (a b c d)\n" | |
213 | "(append '(a (b)) '((c))) @result{} (a (b) (c))\n" | |
1e6808ea | 214 | "@end lisp\n" |
5352393c MG |
215 | "The resulting list is always newly allocated, except that it\n" |
216 | "shares structure with the last list argument. The last\n" | |
217 | "argument may actually be any object; an improper list results\n" | |
218 | "if the last argument is not a proper list.\n" | |
1e6808ea | 219 | "@lisp\n" |
5352393c MG |
220 | "(append '(a b) '(c . d)) @result{} (a b c . d)\n" |
221 | "(append '() 'a) @result{} a\n" | |
1e6808ea | 222 | "@end lisp") |
1bbd0b84 | 223 | #define FUNC_NAME s_scm_append |
0f2d19dd | 224 | { |
af45e3b0 DH |
225 | SCM_VALIDATE_REST_ARGUMENT (args); |
226 | if (SCM_NULLP (args)) { | |
227 | return SCM_EOL; | |
228 | } else { | |
229 | SCM res = SCM_EOL; | |
230 | SCM *lloc = &res; | |
231 | SCM arg = SCM_CAR (args); | |
4c13270f | 232 | int argnum = 1; |
af45e3b0 DH |
233 | args = SCM_CDR (args); |
234 | while (!SCM_NULLP (args)) { | |
235 | while (SCM_CONSP (arg)) { | |
236 | *lloc = scm_cons (SCM_CAR (arg), SCM_EOL); | |
237 | lloc = SCM_CDRLOC (*lloc); | |
238 | arg = SCM_CDR (arg); | |
239 | } | |
4c13270f | 240 | SCM_VALIDATE_NULL_OR_NIL (argnum, arg); |
af45e3b0 DH |
241 | arg = SCM_CAR (args); |
242 | args = SCM_CDR (args); | |
4c13270f | 243 | argnum++; |
af45e3b0 DH |
244 | }; |
245 | *lloc = arg; | |
0f2d19dd JB |
246 | return res; |
247 | } | |
0f2d19dd | 248 | } |
1bbd0b84 | 249 | #undef FUNC_NAME |
0f2d19dd JB |
250 | |
251 | ||
a1ec6916 | 252 | SCM_DEFINE (scm_append_x, "append!", 0, 0, 1, |
1e6808ea MG |
253 | (SCM lists), |
254 | "A destructive version of @code{append} (@pxref{Pairs and\n" | |
7a095584 | 255 | "Lists,,,r5rs, The Revised^5 Report on Scheme}). The cdr field\n" |
1e6808ea | 256 | "of each list's final pair is changed to point to the head of\n" |
71a89639 | 257 | "the next list, so no consing is performed. Return\n" |
1e6808ea | 258 | "the mutated list.") |
1bbd0b84 | 259 | #define FUNC_NAME s_scm_append_x |
0f2d19dd | 260 | { |
d4641098 | 261 | SCM ret, *loc; |
1e6808ea | 262 | SCM_VALIDATE_REST_ARGUMENT (lists); |
d4641098 KR |
263 | |
264 | if (SCM_NULLP (lists)) | |
265 | return SCM_EOL; | |
266 | ||
267 | loc = &ret; | |
268 | for (;;) | |
269 | { | |
1e6808ea | 270 | SCM arg = SCM_CAR (lists); |
d4641098 KR |
271 | *loc = arg; |
272 | ||
1e6808ea | 273 | lists = SCM_CDR (lists); |
d4641098 KR |
274 | if (SCM_NULLP (lists)) |
275 | return ret; | |
276 | ||
277 | if (!SCM_NULL_OR_NIL_P (arg)) | |
278 | { | |
279 | SCM_VALIDATE_CONS (SCM_ARG1, arg); | |
280 | loc = SCM_CDRLOC (scm_last_pair (arg)); | |
281 | } | |
af45e3b0 | 282 | } |
0f2d19dd | 283 | } |
1bbd0b84 | 284 | #undef FUNC_NAME |
0f2d19dd JB |
285 | |
286 | ||
3b3b36dd | 287 | SCM_DEFINE (scm_last_pair, "last-pair", 1, 0, 0, |
e1385ffc | 288 | (SCM lst), |
71a89639 | 289 | "Return the last pair in @var{lst}, signalling an error if\n" |
b380b885 | 290 | "@var{lst} is circular.") |
1bbd0b84 | 291 | #define FUNC_NAME s_scm_last_pair |
df13742c | 292 | { |
e1385ffc GB |
293 | SCM tortoise = lst; |
294 | SCM hare = lst; | |
0f2d19dd | 295 | |
c96d76b8 NJ |
296 | if (SCM_NULL_OR_NIL_P (lst)) |
297 | return lst; | |
df13742c | 298 | |
e1385ffc GB |
299 | SCM_VALIDATE_CONS (SCM_ARG1, lst); |
300 | do { | |
301 | SCM ahead = SCM_CDR(hare); | |
1685446c | 302 | if (!SCM_CONSP (ahead)) return hare; |
e1385ffc GB |
303 | hare = ahead; |
304 | ahead = SCM_CDR(hare); | |
1685446c | 305 | if (!SCM_CONSP (ahead)) return hare; |
e1385ffc GB |
306 | hare = ahead; |
307 | tortoise = SCM_CDR(tortoise); | |
df13742c | 308 | } |
fbd485ba | 309 | while (! SCM_EQ_P (hare, tortoise)); |
1afff620 | 310 | SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst)); |
df13742c | 311 | } |
1bbd0b84 | 312 | #undef FUNC_NAME |
df13742c JB |
313 | |
314 | \f | |
315 | /* reversing lists */ | |
0f2d19dd | 316 | |
a1ec6916 | 317 | SCM_DEFINE (scm_reverse, "reverse", 1, 0, 0, |
e1385ffc | 318 | (SCM lst), |
5352393c MG |
319 | "Return a new list that contains the elements of @var{lst} but\n" |
320 | "in reverse order.") | |
e1385ffc GB |
321 | #define FUNC_NAME s_scm_reverse |
322 | { | |
323 | SCM result = SCM_EOL; | |
324 | SCM tortoise = lst; | |
325 | SCM hare = lst; | |
326 | ||
327 | do { | |
c96d76b8 | 328 | if (SCM_NULL_OR_NIL_P(hare)) return result; |
e1385ffc GB |
329 | SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME); |
330 | result = scm_cons (SCM_CAR (hare), result); | |
331 | hare = SCM_CDR (hare); | |
c96d76b8 | 332 | if (SCM_NULL_OR_NIL_P(hare)) return result; |
e1385ffc GB |
333 | SCM_ASSERT(SCM_CONSP(hare), lst, 1, FUNC_NAME); |
334 | result = scm_cons (SCM_CAR (hare), result); | |
335 | hare = SCM_CDR (hare); | |
336 | tortoise = SCM_CDR (tortoise); | |
337 | } | |
fbd485ba | 338 | while (! SCM_EQ_P (hare, tortoise)); |
1afff620 | 339 | SCM_MISC_ERROR ("Circular structure in position 1: ~S", scm_list_1 (lst)); |
e1385ffc GB |
340 | } |
341 | #undef FUNC_NAME | |
342 | ||
343 | SCM_DEFINE (scm_reverse_x, "reverse!", 1, 1, 0, | |
344 | (SCM lst, SCM new_tail), | |
7a095584 NJ |
345 | "A destructive version of @code{reverse} (@pxref{Pairs and Lists,,,r5rs,\n" |
346 | "The Revised^5 Report on Scheme}). The cdr of each cell in @var{lst} is\n" | |
71a89639 MV |
347 | "modified to point to the previous list element. Return the\n" |
348 | "reversed list.\n\n" | |
b380b885 MD |
349 | "Caveat: because the list is modified in place, the tail of the original\n" |
350 | "list now becomes its head, and the head of the original list now becomes\n" | |
351 | "the tail. Therefore, the @var{lst} symbol to which the head of the\n" | |
352 | "original list was bound now points to the tail. To ensure that the head\n" | |
353 | "of the modified list is not lost, it is wise to save the return value of\n" | |
354 | "@code{reverse!}") | |
1bbd0b84 | 355 | #define FUNC_NAME s_scm_reverse_x |
0f2d19dd | 356 | { |
e39c3de4 | 357 | SCM_VALIDATE_LIST (1, lst); |
3946f0de MD |
358 | if (SCM_UNBNDP (new_tail)) |
359 | new_tail = SCM_EOL; | |
360 | else | |
e39c3de4 | 361 | SCM_VALIDATE_LIST (2, new_tail); |
0f2d19dd | 362 | |
c96d76b8 | 363 | while (!SCM_NULL_OR_NIL_P (lst)) |
3946f0de | 364 | { |
e1385ffc GB |
365 | SCM old_tail = SCM_CDR (lst); |
366 | SCM_SETCDR (lst, new_tail); | |
367 | new_tail = lst; | |
368 | lst = old_tail; | |
3946f0de MD |
369 | } |
370 | return new_tail; | |
0f2d19dd | 371 | } |
1bbd0b84 | 372 | #undef FUNC_NAME |
0f2d19dd | 373 | |
0f2d19dd | 374 | \f |
685c0d71 | 375 | |
df13742c | 376 | /* indexing lists by element number */ |
0f2d19dd | 377 | |
3b3b36dd | 378 | SCM_DEFINE (scm_list_ref, "list-ref", 2, 0, 0, |
685c0d71 | 379 | (SCM list, SCM k), |
5352393c | 380 | "Return the @var{k}th element from @var{list}.") |
1bbd0b84 GB |
381 | #define FUNC_NAME s_scm_list_ref |
382 | { | |
685c0d71 | 383 | SCM lst = list; |
c014a02e | 384 | unsigned long int i; |
34d19ef6 | 385 | SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i); |
685c0d71 DH |
386 | while (SCM_CONSP (lst)) { |
387 | if (i == 0) | |
388 | return SCM_CAR (lst); | |
389 | else { | |
390 | --i; | |
391 | lst = SCM_CDR (lst); | |
392 | } | |
393 | }; | |
c96d76b8 | 394 | if (SCM_NULL_OR_NIL_P (lst)) |
685c0d71 DH |
395 | SCM_OUT_OF_RANGE (2, k); |
396 | else | |
397 | SCM_WRONG_TYPE_ARG (1, list); | |
0f2d19dd | 398 | } |
1bbd0b84 | 399 | #undef FUNC_NAME |
0f2d19dd | 400 | |
685c0d71 | 401 | |
3b3b36dd | 402 | SCM_DEFINE (scm_list_set_x, "list-set!", 3, 0, 0, |
685c0d71 DH |
403 | (SCM list, SCM k, SCM val), |
404 | "Set the @var{k}th element of @var{list} to @var{val}.") | |
1bbd0b84 GB |
405 | #define FUNC_NAME s_scm_list_set_x |
406 | { | |
685c0d71 | 407 | SCM lst = list; |
c014a02e | 408 | unsigned long int i; |
34d19ef6 | 409 | SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i); |
685c0d71 DH |
410 | while (SCM_CONSP (lst)) { |
411 | if (i == 0) { | |
412 | SCM_SETCAR (lst, val); | |
413 | return val; | |
414 | } else { | |
415 | --i; | |
416 | lst = SCM_CDR (lst); | |
417 | } | |
418 | }; | |
c96d76b8 | 419 | if (SCM_NULL_OR_NIL_P (lst)) |
685c0d71 DH |
420 | SCM_OUT_OF_RANGE (2, k); |
421 | else | |
422 | SCM_WRONG_TYPE_ARG (1, list); | |
0f2d19dd | 423 | } |
1bbd0b84 | 424 | #undef FUNC_NAME |
0f2d19dd JB |
425 | |
426 | ||
1bbd0b84 GB |
427 | SCM_REGISTER_PROC(s_list_cdr_ref, "list-cdr-ref", 2, 0, 0, scm_list_tail); |
428 | ||
3b3b36dd | 429 | SCM_DEFINE (scm_list_tail, "list-tail", 2, 0, 0, |
1bbd0b84 | 430 | (SCM lst, SCM k), |
8f85c0c6 | 431 | "@deffnx {Scheme Procedure} list-cdr-ref lst k\n" |
b380b885 MD |
432 | "Return the \"tail\" of @var{lst} beginning with its @var{k}th element.\n" |
433 | "The first element of the list is considered to be element 0.\n\n" | |
872e0c72 | 434 | "@code{list-tail} and @code{list-cdr-ref} are identical. It may help to\n" |
b380b885 MD |
435 | "think of @code{list-cdr-ref} as accessing the @var{k}th cdr of the list,\n" |
436 | "or returning the results of cdring @var{k} times down @var{lst}.") | |
1bbd0b84 | 437 | #define FUNC_NAME s_scm_list_tail |
df13742c | 438 | { |
c014a02e | 439 | register long i; |
34d19ef6 | 440 | SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i); |
df13742c | 441 | while (i-- > 0) { |
34d19ef6 | 442 | SCM_VALIDATE_CONS (1, lst); |
df13742c JB |
443 | lst = SCM_CDR(lst); |
444 | } | |
445 | return lst; | |
446 | } | |
1bbd0b84 | 447 | #undef FUNC_NAME |
df13742c | 448 | |
0f2d19dd | 449 | |
3b3b36dd | 450 | SCM_DEFINE (scm_list_cdr_set_x, "list-cdr-set!", 3, 0, 0, |
685c0d71 DH |
451 | (SCM list, SCM k, SCM val), |
452 | "Set the @var{k}th cdr of @var{list} to @var{val}.") | |
1bbd0b84 GB |
453 | #define FUNC_NAME s_scm_list_cdr_set_x |
454 | { | |
685c0d71 | 455 | SCM lst = list; |
c014a02e | 456 | unsigned long int i; |
34d19ef6 | 457 | SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i); |
685c0d71 DH |
458 | while (SCM_CONSP (lst)) { |
459 | if (i == 0) { | |
460 | SCM_SETCDR (lst, val); | |
461 | return val; | |
462 | } else { | |
463 | --i; | |
464 | lst = SCM_CDR (lst); | |
465 | } | |
466 | }; | |
c96d76b8 | 467 | if (SCM_NULL_OR_NIL_P (lst)) |
685c0d71 DH |
468 | SCM_OUT_OF_RANGE (2, k); |
469 | else | |
470 | SCM_WRONG_TYPE_ARG (1, list); | |
0f2d19dd | 471 | } |
1bbd0b84 | 472 | #undef FUNC_NAME |
0f2d19dd JB |
473 | |
474 | ||
475 | \f | |
df13742c | 476 | /* copying lists, perhaps partially */ |
0f2d19dd | 477 | |
3b3b36dd | 478 | SCM_DEFINE (scm_list_head, "list-head", 2, 0, 0, |
1bbd0b84 | 479 | (SCM lst, SCM k), |
b380b885 MD |
480 | "Copy the first @var{k} elements from @var{lst} into a new list, and\n" |
481 | "return it.") | |
1bbd0b84 | 482 | #define FUNC_NAME s_scm_list_head |
0f2d19dd JB |
483 | { |
484 | SCM answer; | |
485 | SCM * pos; | |
c014a02e | 486 | register long i; |
0f2d19dd | 487 | |
34d19ef6 | 488 | SCM_VALIDATE_INUM_MIN_COPY (2, k,0, i); |
0f2d19dd JB |
489 | answer = SCM_EOL; |
490 | pos = &answer; | |
0f2d19dd JB |
491 | while (i-- > 0) |
492 | { | |
34d19ef6 | 493 | SCM_VALIDATE_CONS (1, lst); |
0f2d19dd | 494 | *pos = scm_cons (SCM_CAR (lst), SCM_EOL); |
25d8012c | 495 | pos = SCM_CDRLOC (*pos); |
0f2d19dd JB |
496 | lst = SCM_CDR(lst); |
497 | } | |
498 | return answer; | |
499 | } | |
1bbd0b84 | 500 | #undef FUNC_NAME |
0f2d19dd JB |
501 | |
502 | ||
a1ec6916 | 503 | SCM_DEFINE (scm_list_copy, "list-copy", 1, 0, 0, |
1bbd0b84 | 504 | (SCM lst), |
b380b885 | 505 | "Return a (newly-created) copy of @var{lst}.") |
1bbd0b84 | 506 | #define FUNC_NAME s_scm_list_copy |
df13742c JB |
507 | { |
508 | SCM newlst; | |
509 | SCM * fill_here; | |
510 | SCM from_here; | |
511 | ||
5d6bb349 KN |
512 | SCM_VALIDATE_LIST (1, lst); |
513 | ||
df13742c JB |
514 | newlst = SCM_EOL; |
515 | fill_here = &newlst; | |
516 | from_here = lst; | |
517 | ||
0c95b57d | 518 | while (SCM_CONSP (from_here)) |
df13742c JB |
519 | { |
520 | SCM c; | |
521 | c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here)); | |
522 | *fill_here = c; | |
25d8012c | 523 | fill_here = SCM_CDRLOC (c); |
df13742c JB |
524 | from_here = SCM_CDR (from_here); |
525 | } | |
526 | return newlst; | |
527 | } | |
1bbd0b84 | 528 | #undef FUNC_NAME |
df13742c | 529 | |
0f2d19dd | 530 | \f |
df13742c JB |
531 | /* membership tests (memq, memv, etc.) */ |
532 | ||
79a3dafe DH |
533 | /* The function scm_c_memq returns the first sublist of list whose car is |
534 | * 'eq?' obj, where the sublists of list are the non-empty lists returned by | |
535 | * (list-tail list k) for k less than the length of list. If obj does not | |
1e6808ea | 536 | * occur in list, then #f (not the empty list) is returned. |
79a3dafe DH |
537 | * List must be a proper list, otherwise scm_c_memq may crash or loop |
538 | * endlessly. | |
539 | */ | |
540 | SCM | |
541 | scm_c_memq (SCM obj, SCM list) | |
542 | { | |
c96d76b8 | 543 | for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR (list)) |
79a3dafe DH |
544 | { |
545 | if (SCM_EQ_P (SCM_CAR (list), obj)) | |
546 | return list; | |
547 | } | |
548 | return SCM_BOOL_F; | |
549 | } | |
550 | ||
551 | ||
3b3b36dd | 552 | SCM_DEFINE (scm_memq, "memq", 2, 0, 0, |
1bbd0b84 | 553 | (SCM x, SCM lst), |
5352393c MG |
554 | "Return the first sublist of @var{lst} whose car is @code{eq?}\n" |
555 | "to @var{x} where the sublists of @var{lst} are the non-empty\n" | |
556 | "lists returned by @code{(list-tail @var{lst} @var{k})} for\n" | |
557 | "@var{k} less than the length of @var{lst}. If @var{x} does not\n" | |
558 | "occur in @var{lst}, then @code{#f} (not the empty list) is\n" | |
559 | "returned.") | |
1bbd0b84 | 560 | #define FUNC_NAME s_scm_memq |
0f2d19dd | 561 | { |
daa6ba18 | 562 | SCM_VALIDATE_LIST (2, lst); |
79a3dafe | 563 | return scm_c_memq (x, lst); |
0f2d19dd | 564 | } |
1bbd0b84 | 565 | #undef FUNC_NAME |
0f2d19dd JB |
566 | |
567 | ||
3b3b36dd | 568 | SCM_DEFINE (scm_memv, "memv", 2, 0, 0, |
1bbd0b84 | 569 | (SCM x, SCM lst), |
5352393c MG |
570 | "Return the first sublist of @var{lst} whose car is @code{eqv?}\n" |
571 | "to @var{x} where the sublists of @var{lst} are the non-empty\n" | |
572 | "lists returned by @code{(list-tail @var{lst} @var{k})} for\n" | |
573 | "@var{k} less than the length of @var{lst}. If @var{x} does not\n" | |
574 | "occur in @var{lst}, then @code{#f} (not the empty list) is\n" | |
575 | "returned.") | |
1bbd0b84 | 576 | #define FUNC_NAME s_scm_memv |
0f2d19dd | 577 | { |
daa6ba18 | 578 | SCM_VALIDATE_LIST (2, lst); |
c96d76b8 | 579 | for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst)) |
daa6ba18 DH |
580 | { |
581 | if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (lst), x))) | |
582 | return lst; | |
583 | } | |
584 | return SCM_BOOL_F; | |
0f2d19dd | 585 | } |
1bbd0b84 | 586 | #undef FUNC_NAME |
0f2d19dd JB |
587 | |
588 | ||
3b3b36dd | 589 | SCM_DEFINE (scm_member, "member", 2, 0, 0, |
1bbd0b84 | 590 | (SCM x, SCM lst), |
5352393c MG |
591 | "Return the first sublist of @var{lst} whose car is\n" |
592 | "@code{equal?} to @var{x} where the sublists of @var{lst} are\n" | |
593 | "the non-empty lists returned by @code{(list-tail @var{lst}\n" | |
594 | "@var{k})} for @var{k} less than the length of @var{lst}. If\n" | |
595 | "@var{x} does not occur in @var{lst}, then @code{#f} (not the\n" | |
596 | "empty list) is returned.") | |
1bbd0b84 | 597 | #define FUNC_NAME s_scm_member |
0f2d19dd | 598 | { |
daa6ba18 | 599 | SCM_VALIDATE_LIST (2, lst); |
c96d76b8 | 600 | for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst)) |
daa6ba18 DH |
601 | { |
602 | if (! SCM_FALSEP (scm_equal_p (SCM_CAR (lst), x))) | |
603 | return lst; | |
604 | } | |
605 | return SCM_BOOL_F; | |
0f2d19dd | 606 | } |
1bbd0b84 | 607 | #undef FUNC_NAME |
0f2d19dd | 608 | |
0f2d19dd | 609 | \f |
df13742c | 610 | /* deleting elements from a list (delq, etc.) */ |
0f2d19dd | 611 | |
3b3b36dd | 612 | SCM_DEFINE (scm_delq_x, "delq!", 2, 0, 0, |
1bbd0b84 | 613 | (SCM item, SCM lst), |
8f85c0c6 NJ |
614 | "@deffnx {Scheme Procedure} delv! item lst\n" |
615 | "@deffnx {Scheme Procedure} delete! item lst\n" | |
b380b885 | 616 | "These procedures are destructive versions of @code{delq}, @code{delv}\n" |
bfefbf18 | 617 | "and @code{delete}: they modify the existing @var{lst}\n" |
b380b885 MD |
618 | "rather than creating a new list. Caveat evaluator: Like other\n" |
619 | "destructive list functions, these functions cannot modify the binding of\n" | |
620 | "@var{lst}, and so cannot be used to delete the first element of\n" | |
621 | "@var{lst} destructively.") | |
1bbd0b84 | 622 | #define FUNC_NAME s_scm_delq_x |
0f2d19dd | 623 | { |
164271a1 JB |
624 | SCM walk; |
625 | SCM *prev; | |
0f2d19dd | 626 | |
164271a1 | 627 | for (prev = &lst, walk = lst; |
0c95b57d | 628 | SCM_CONSP (walk); |
164271a1 | 629 | walk = SCM_CDR (walk)) |
0f2d19dd | 630 | { |
fbd485ba | 631 | if (SCM_EQ_P (SCM_CAR (walk), item)) |
164271a1 JB |
632 | *prev = SCM_CDR (walk); |
633 | else | |
634 | prev = SCM_CDRLOC (walk); | |
0f2d19dd | 635 | } |
164271a1 JB |
636 | |
637 | return lst; | |
0f2d19dd | 638 | } |
1bbd0b84 | 639 | #undef FUNC_NAME |
0f2d19dd JB |
640 | |
641 | ||
3b3b36dd | 642 | SCM_DEFINE (scm_delv_x, "delv!", 2, 0, 0, |
5352393c MG |
643 | (SCM item, SCM lst), |
644 | "Destructively remove all elements from @var{lst} that are\n" | |
645 | "@code{eqv?} to @var{item}.") | |
1bbd0b84 | 646 | #define FUNC_NAME s_scm_delv_x |
0f2d19dd | 647 | { |
164271a1 JB |
648 | SCM walk; |
649 | SCM *prev; | |
0f2d19dd | 650 | |
164271a1 | 651 | for (prev = &lst, walk = lst; |
0c95b57d | 652 | SCM_CONSP (walk); |
164271a1 | 653 | walk = SCM_CDR (walk)) |
0f2d19dd | 654 | { |
fbd485ba | 655 | if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item))) |
164271a1 JB |
656 | *prev = SCM_CDR (walk); |
657 | else | |
658 | prev = SCM_CDRLOC (walk); | |
0f2d19dd | 659 | } |
164271a1 JB |
660 | |
661 | return lst; | |
0f2d19dd | 662 | } |
1bbd0b84 | 663 | #undef FUNC_NAME |
0f2d19dd JB |
664 | |
665 | ||
666 | ||
3b3b36dd | 667 | SCM_DEFINE (scm_delete_x, "delete!", 2, 0, 0, |
5352393c MG |
668 | (SCM item, SCM lst), |
669 | "Destructively remove all elements from @var{lst} that are\n" | |
670 | "@code{equal?} to @var{item}.") | |
1bbd0b84 | 671 | #define FUNC_NAME s_scm_delete_x |
0f2d19dd | 672 | { |
164271a1 JB |
673 | SCM walk; |
674 | SCM *prev; | |
0f2d19dd | 675 | |
164271a1 | 676 | for (prev = &lst, walk = lst; |
0c95b57d | 677 | SCM_CONSP (walk); |
164271a1 | 678 | walk = SCM_CDR (walk)) |
0f2d19dd | 679 | { |
fbd485ba | 680 | if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item))) |
164271a1 JB |
681 | *prev = SCM_CDR (walk); |
682 | else | |
683 | prev = SCM_CDRLOC (walk); | |
0f2d19dd | 684 | } |
164271a1 JB |
685 | |
686 | return lst; | |
0f2d19dd | 687 | } |
1bbd0b84 | 688 | #undef FUNC_NAME |
0f2d19dd JB |
689 | |
690 | ||
691 | \f | |
692 | ||
0f2d19dd | 693 | |
a1ec6916 | 694 | SCM_DEFINE (scm_delq, "delq", 2, 0, 0, |
1bbd0b84 | 695 | (SCM item, SCM lst), |
5352393c MG |
696 | "Return a newly-created copy of @var{lst} with elements\n" |
697 | "@code{eq?} to @var{item} removed. This procedure mirrors\n" | |
698 | "@code{memq}: @code{delq} compares elements of @var{lst} against\n" | |
699 | "@var{item} with @code{eq?}.") | |
1bbd0b84 | 700 | #define FUNC_NAME s_scm_delq |
0f2d19dd | 701 | { |
1bbd0b84 | 702 | SCM copy = scm_list_copy (lst); |
0f2d19dd JB |
703 | return scm_delq_x (item, copy); |
704 | } | |
1bbd0b84 | 705 | #undef FUNC_NAME |
0f2d19dd | 706 | |
a1ec6916 | 707 | SCM_DEFINE (scm_delv, "delv", 2, 0, 0, |
1bbd0b84 | 708 | (SCM item, SCM lst), |
5352393c MG |
709 | "Return a newly-created copy of @var{lst} with elements\n" |
710 | "@code{eqv?} to @var{item} removed. This procedure mirrors\n" | |
711 | "@code{memv}: @code{delv} compares elements of @var{lst} against\n" | |
712 | "@var{item} with @code{eqv?}.") | |
1bbd0b84 | 713 | #define FUNC_NAME s_scm_delv |
0f2d19dd | 714 | { |
1bbd0b84 | 715 | SCM copy = scm_list_copy (lst); |
0f2d19dd JB |
716 | return scm_delv_x (item, copy); |
717 | } | |
1bbd0b84 | 718 | #undef FUNC_NAME |
0f2d19dd | 719 | |
a1ec6916 | 720 | SCM_DEFINE (scm_delete, "delete", 2, 0, 0, |
1bbd0b84 | 721 | (SCM item, SCM lst), |
5352393c MG |
722 | "Return a newly-created copy of @var{lst} with elements\n" |
723 | "@code{equal?} to @var{item} removed. This procedure mirrors\n" | |
724 | "@code{member}: @code{delete} compares elements of @var{lst}\n" | |
725 | "against @var{item} with @code{equal?}.") | |
1bbd0b84 | 726 | #define FUNC_NAME s_scm_delete |
0f2d19dd | 727 | { |
1bbd0b84 | 728 | SCM copy = scm_list_copy (lst); |
0f2d19dd JB |
729 | return scm_delete_x (item, copy); |
730 | } | |
1bbd0b84 | 731 | #undef FUNC_NAME |
0f2d19dd JB |
732 | |
733 | ||
3b3b36dd | 734 | SCM_DEFINE (scm_delq1_x, "delq1!", 2, 0, 0, |
1bbd0b84 | 735 | (SCM item, SCM lst), |
5352393c MG |
736 | "Like @code{delq!}, but only deletes the first occurrence of\n" |
737 | "@var{item} from @var{lst}. Tests for equality using\n" | |
738 | "@code{eq?}. See also @code{delv1!} and @code{delete1!}.") | |
1bbd0b84 | 739 | #define FUNC_NAME s_scm_delq1_x |
82dc9f57 MD |
740 | { |
741 | SCM walk; | |
742 | SCM *prev; | |
743 | ||
744 | for (prev = &lst, walk = lst; | |
0c95b57d | 745 | SCM_CONSP (walk); |
82dc9f57 MD |
746 | walk = SCM_CDR (walk)) |
747 | { | |
fbd485ba | 748 | if (SCM_EQ_P (SCM_CAR (walk), item)) |
82dc9f57 MD |
749 | { |
750 | *prev = SCM_CDR (walk); | |
751 | break; | |
752 | } | |
753 | else | |
754 | prev = SCM_CDRLOC (walk); | |
755 | } | |
756 | ||
757 | return lst; | |
758 | } | |
1bbd0b84 | 759 | #undef FUNC_NAME |
82dc9f57 MD |
760 | |
761 | ||
3b3b36dd | 762 | SCM_DEFINE (scm_delv1_x, "delv1!", 2, 0, 0, |
8507b88c | 763 | (SCM item, SCM lst), |
5352393c MG |
764 | "Like @code{delv!}, but only deletes the first occurrence of\n" |
765 | "@var{item} from @var{lst}. Tests for equality using\n" | |
766 | "@code{eqv?}. See also @code{delq1!} and @code{delete1!}.") | |
1bbd0b84 | 767 | #define FUNC_NAME s_scm_delv1_x |
82dc9f57 MD |
768 | { |
769 | SCM walk; | |
770 | SCM *prev; | |
771 | ||
772 | for (prev = &lst, walk = lst; | |
0c95b57d | 773 | SCM_CONSP (walk); |
82dc9f57 MD |
774 | walk = SCM_CDR (walk)) |
775 | { | |
fbd485ba | 776 | if (! SCM_FALSEP (scm_eqv_p (SCM_CAR (walk), item))) |
82dc9f57 MD |
777 | { |
778 | *prev = SCM_CDR (walk); | |
779 | break; | |
780 | } | |
781 | else | |
782 | prev = SCM_CDRLOC (walk); | |
783 | } | |
784 | ||
785 | return lst; | |
786 | } | |
1bbd0b84 | 787 | #undef FUNC_NAME |
82dc9f57 MD |
788 | |
789 | ||
3b3b36dd | 790 | SCM_DEFINE (scm_delete1_x, "delete1!", 2, 0, 0, |
8507b88c | 791 | (SCM item, SCM lst), |
5352393c MG |
792 | "Like @code{delete!}, but only deletes the first occurrence of\n" |
793 | "@var{item} from @var{lst}. Tests for equality using\n" | |
794 | "@code{equal?}. See also @code{delq1!} and @code{delv1!}.") | |
1bbd0b84 | 795 | #define FUNC_NAME s_scm_delete1_x |
82dc9f57 MD |
796 | { |
797 | SCM walk; | |
798 | SCM *prev; | |
799 | ||
800 | for (prev = &lst, walk = lst; | |
0c95b57d | 801 | SCM_CONSP (walk); |
82dc9f57 MD |
802 | walk = SCM_CDR (walk)) |
803 | { | |
fbd485ba | 804 | if (! SCM_FALSEP (scm_equal_p (SCM_CAR (walk), item))) |
82dc9f57 MD |
805 | { |
806 | *prev = SCM_CDR (walk); | |
807 | break; | |
808 | } | |
809 | else | |
810 | prev = SCM_CDRLOC (walk); | |
811 | } | |
812 | ||
813 | return lst; | |
814 | } | |
1bbd0b84 | 815 | #undef FUNC_NAME |
82dc9f57 | 816 | |
c614a00b MD |
817 | SCM_DEFINE (scm_filter, "filter", 2, 0, 0, |
818 | (SCM pred, SCM list), | |
819 | "Return all the elements of 2nd arg @var{list} that satisfy predicate @var{pred}.\n" | |
820 | "The list is not disordered -- elements that appear in the result list occur\n" | |
821 | "in the same order as they occur in the argument list. The returned list may\n" | |
822 | "share a common tail with the argument list. The dynamic order in which the\n" | |
823 | "various applications of pred are made is not specified.\n\n" | |
824 | "@lisp\n" | |
825 | "(filter even? '(0 7 8 8 43 -4)) => (0 8 8 -4)\n" | |
826 | "@end lisp") | |
827 | #define FUNC_NAME s_scm_filter | |
828 | { | |
829 | scm_t_trampoline_1 call = scm_trampoline_1 (pred); | |
830 | SCM walk; | |
831 | SCM *prev; | |
832 | SCM res = SCM_EOL; | |
833 | SCM_ASSERT (call, pred, 1, FUNC_NAME); | |
834 | SCM_VALIDATE_LIST (2, list); | |
835 | ||
836 | for (prev = &res, walk = list; | |
837 | SCM_CONSP (walk); | |
838 | walk = SCM_CDR (walk)) | |
839 | { | |
840 | if (!SCM_FALSEP (call (pred, SCM_CAR (walk)))) | |
841 | { | |
842 | *prev = scm_cons (SCM_CAR (walk), SCM_EOL); | |
843 | prev = SCM_CDRLOC (*prev); | |
844 | } | |
845 | } | |
846 | ||
847 | return res; | |
848 | } | |
849 | #undef FUNC_NAME | |
850 | ||
851 | SCM_DEFINE (scm_filter_x, "filter!", 2, 0, 0, | |
852 | (SCM pred, SCM list), | |
853 | "Linear-update variant of @code{filter}.") | |
854 | #define FUNC_NAME s_scm_filter_x | |
855 | { | |
856 | scm_t_trampoline_1 call = scm_trampoline_1 (pred); | |
857 | SCM walk; | |
858 | SCM *prev; | |
859 | SCM_ASSERT (call, pred, 1, FUNC_NAME); | |
860 | SCM_VALIDATE_LIST (2, list); | |
861 | ||
862 | for (prev = &list, walk = list; | |
863 | SCM_CONSP (walk); | |
864 | walk = SCM_CDR (walk)) | |
865 | { | |
866 | if (!SCM_FALSEP (call (pred, SCM_CAR (walk)))) | |
867 | prev = SCM_CDRLOC (walk); | |
868 | else | |
869 | *prev = SCM_CDR (walk); | |
870 | } | |
871 | ||
872 | return list; | |
873 | } | |
874 | #undef FUNC_NAME | |
82dc9f57 | 875 | |
0f2d19dd | 876 | \f |
0f2d19dd JB |
877 | void |
878 | scm_init_list () | |
0f2d19dd | 879 | { |
a0599745 | 880 | #include "libguile/list.x" |
0f2d19dd | 881 | } |
89e00824 ML |
882 | |
883 | /* | |
884 | Local Variables: | |
885 | c-file-style: "gnu" | |
886 | End: | |
887 | */ |