Adapt GDB integration to newest patches
[bpt/guile.git] / libguile / srfi-1.c
CommitLineData
ee6aac97
MD
1/* srfi-1.c --- SRFI-1 procedures for Guile
2 *
856d318a 3 * Copyright (C) 1995-1997, 2000-2003, 2005, 2006, 2008-2011, 2013
a5186f50 4 * 2014 Free Software Foundation, Inc.
cf9d3c47 5 *
73be1d9e 6 * This library is free software; you can redistribute it and/or
53befeb7
NJ
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
ee6aac97 10 *
53befeb7
NJ
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
ee6aac97 15 *
73be1d9e
MV
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
53befeb7
NJ
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
73be1d9e 20 */
ee6aac97 21
37710f7e 22\f
dbb605f5 23#ifdef HAVE_CONFIG_H
a030cb4b
LC
24# include <config.h>
25#endif
26
37710f7e
AW
27#include "libguile/_scm.h"
28#include "libguile/eq.h"
29
30#include "libguile/validate.h"
31#include "libguile/list.h"
32#include "libguile/eval.h"
33#include "libguile/srfi-1.h"
34
35#include <stdarg.h>
ee6aac97 36
ee6aac97 37
0b7f2eb8
LC
38/* The intent of this file was to gradually replace those Scheme
39 * procedures in srfi-1.scm that extend core primitive procedures,
37710f7e 40 * so that using srfi-1 wouldn't have performance penalties.
ee6aac97 41 *
0b7f2eb8
LC
42 * However, we now prefer to write these procedures in Scheme, let the compiler
43 * optimize them, and have the VM execute them efficiently.
ee6aac97
MD
44 */
45
0b7f2eb8 46
d0a634de
KR
47static SCM
48equal_trampoline (SCM proc, SCM arg1, SCM arg2)
49{
50 return scm_equal_p (arg1, arg2);
51}
52
cf9d3c47
KR
53/* list_copy_part() copies the first COUNT cells of LST, puts the result at
54 *dst, and returns the SCM_CDRLOC of the last cell in that new list.
55
56 This function is designed to be careful about LST possibly having changed
57 in between the caller deciding what to copy, and the copy actually being
58 done here. The COUNT ensures we terminate if LST has become circular,
59 SCM_VALIDATE_CONS guards against a cdr in the list changed to some
60 non-pair object. */
61
62#include <stdio.h>
63static SCM *
64list_copy_part (SCM lst, int count, SCM *dst)
65#define FUNC_NAME "list_copy_part"
66{
67 SCM c;
68 for ( ; count > 0; count--)
69 {
70 SCM_VALIDATE_CONS (SCM_ARGn, lst);
71 c = scm_cons (SCM_CAR (lst), SCM_EOL);
72 *dst = c;
73 dst = SCM_CDRLOC (c);
74 lst = SCM_CDR (lst);
75 }
76 return dst;
77}
78#undef FUNC_NAME
79
d0a634de 80
9a993171
KR
81SCM_DEFINE (scm_srfi1_append_reverse, "append-reverse", 2, 0, 0,
82 (SCM revhead, SCM tail),
83 "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
84 "result. This is equivalent to @code{(append (reverse\n"
85 "@var{rev-head}) @var{tail})}, but its implementation is more\n"
86 "efficient.\n"
87 "\n"
88 "@example\n"
89 "(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
90 "@end example")
91#define FUNC_NAME s_scm_srfi1_append_reverse
92{
93 while (scm_is_pair (revhead))
94 {
95 /* copy first element of revhead onto front of tail */
96 tail = scm_cons (SCM_CAR (revhead), tail);
97 revhead = SCM_CDR (revhead);
98 }
99 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
100 "list");
101 return tail;
102}
103#undef FUNC_NAME
104
105
106SCM_DEFINE (scm_srfi1_append_reverse_x, "append-reverse!", 2, 0, 0,
107 (SCM revhead, SCM tail),
108 "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
109 "result. This is equivalent to @code{(append! (reverse!\n"
110 "@var{rev-head}) @var{tail})}, but its implementation is more\n"
111 "efficient.\n"
112 "\n"
113 "@example\n"
114 "(append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
115 "@end example\n"
116 "\n"
117 "@var{rev-head} may be modified in order to produce the result.")
118#define FUNC_NAME s_scm_srfi1_append_reverse_x
119{
120 SCM newtail;
121
122 while (scm_is_pair (revhead))
123 {
124 /* take the first cons cell from revhead */
125 newtail = revhead;
126 revhead = SCM_CDR (revhead);
127
128 /* make it the new start of tail, appending the previous */
129 SCM_SETCDR (newtail, tail);
130 tail = newtail;
131 }
132 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
133 "list");
134 return tail;
135}
136#undef FUNC_NAME
137
c66c6d53
KR
138SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
139 (SCM lstlst),
140 "Construct a list by appending all lists in @var{lstlst}.\n"
141 "\n"
142 "@code{concatenate} is the same as @code{(apply append\n"
143 "@var{lstlst})}. It exists because some Scheme implementations\n"
144 "have a limit on the number of arguments a function takes, which\n"
145 "the @code{apply} might exceed. In Guile there is no such\n"
146 "limit.")
147#define FUNC_NAME s_scm_srfi1_concatenate
148{
149 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
150 return scm_append (lstlst);
151}
152#undef FUNC_NAME
153
47f2726f 154
c66c6d53
KR
155SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
156 (SCM lstlst),
157 "Construct a list by appending all lists in @var{lstlst}. Those\n"
158 "lists may be modified to produce the result.\n"
159 "\n"
160 "@code{concatenate!} is the same as @code{(apply append!\n"
161 "@var{lstlst})}. It exists because some Scheme implementations\n"
162 "have a limit on the number of arguments a function takes, which\n"
163 "the @code{apply} might exceed. In Guile there is no such\n"
164 "limit.")
2d5ef309 165#define FUNC_NAME s_scm_srfi1_concatenate_x
c66c6d53
KR
166{
167 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
168 return scm_append_x (lstlst);
169}
170#undef FUNC_NAME
47f2726f
KR
171
172
110348ae 173SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
edea856c 174 (SCM pred, SCM list1, SCM rest),
110348ae
KR
175 "Return a count of the number of times @var{pred} returns true\n"
176 "when called on elements from the given lists.\n"
177 "\n"
178 "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
179 "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
edea856c 180 "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
110348ae
KR
181 "with the first element of each list, the second with the second\n"
182 "element from each, and so on.\n"
183 "\n"
184 "Counting stops when the end of the shortest list is reached.\n"
185 "At least one list must be non-circular.")
186#define FUNC_NAME s_scm_srfi1_count
187{
188 long count;
5fc743b4
KR
189 SCM lst;
190 int argnum;
110348ae
KR
191 SCM_VALIDATE_REST_ARGUMENT (rest);
192
193 count = 0;
194
896df2d5 195 if (scm_is_null (rest))
110348ae
KR
196 {
197 /* one list */
a3e92377 198 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
110348ae 199
896df2d5 200 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
a3e92377 201 count += scm_is_true (scm_call_1 (pred, SCM_CAR (list1)));
110348ae 202
5fc743b4
KR
203 /* check below that list1 is a proper list, and done */
204 end_list1:
205 lst = list1;
206 argnum = 2;
110348ae 207 }
896df2d5 208 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
110348ae
KR
209 {
210 /* two lists */
edea856c 211 SCM list2;
110348ae 212
a3e92377 213 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
110348ae 214
edea856c 215 list2 = SCM_CAR (rest);
110348ae
KR
216 for (;;)
217 {
896df2d5 218 if (! scm_is_pair (list1))
5fc743b4 219 goto end_list1;
896df2d5 220 if (! scm_is_pair (list2))
110348ae 221 {
5fc743b4
KR
222 lst = list2;
223 argnum = 3;
110348ae
KR
224 break;
225 }
a3e92377 226 count += scm_is_true (scm_call_2
edea856c
SJ
227 (pred, SCM_CAR (list1), SCM_CAR (list2)));
228 list1 = SCM_CDR (list1);
229 list2 = SCM_CDR (list2);
110348ae
KR
230 }
231 }
232 else
233 {
234 /* three or more lists */
eccd308a
KR
235 SCM vec, args, a;
236 size_t len, i;
110348ae 237
eccd308a
KR
238 /* vec is the list arguments */
239 vec = scm_vector (scm_cons (list1, rest));
240 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
110348ae 241
eccd308a 242 /* args is the argument list to pass to pred, same length as vec,
110348ae 243 re-used for each call */
eccd308a 244 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
110348ae
KR
245
246 for (;;)
247 {
eccd308a
KR
248 /* first elem of each list in vec into args, and step those
249 vec entries onto their next element */
250 for (i = 0, a = args, argnum = 2;
251 i < len;
252 i++, a = SCM_CDR (a), argnum++)
110348ae 253 {
eccd308a 254 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
896df2d5 255 if (! scm_is_pair (lst))
5fc743b4 256 goto check_lst_and_done;
110348ae 257 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
eccd308a 258 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
110348ae
KR
259 }
260
6b4ba76d 261 count += scm_is_true (scm_apply_0 (pred, args));
110348ae
KR
262 }
263 }
5fc743b4
KR
264
265 check_lst_and_done:
266 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
93ccaef0 267 return scm_from_long (count);
110348ae
KR
268}
269#undef FUNC_NAME
270
271
d0a634de
KR
272SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
273 (SCM x, SCM lst, SCM pred),
274 "Return a list containing the elements of @var{lst} but with\n"
275 "those equal to @var{x} deleted. The returned elements will be\n"
276 "in the same order as they were in @var{lst}.\n"
277 "\n"
278 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
279 "given. An equality call is made just once for each element,\n"
280 "but the order in which the calls are made on the elements is\n"
281 "unspecified.\n"
282 "\n"
283 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
284 "given @var{x} is first. This means for instance elements\n"
285 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
286 "\n"
287 "@var{lst} is not modified, but the returned list might share a\n"
288 "common tail with @var{lst}.")
289#define FUNC_NAME s_scm_srfi1_delete
290{
d0a634de 291 SCM ret, *p, keeplst;
cf9d3c47 292 int count;
d0a634de
KR
293
294 if (SCM_UNBNDP (pred))
295 return scm_delete (x, lst);
296
a3e92377 297 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
d0a634de
KR
298
299 /* ret is the return list being constructed. p is where to append to it,
300 initially &ret then SCM_CDRLOC of the last pair. lst progresses as
301 elements are considered.
302
303 Elements to be retained are not immediately copied, instead keeplst is
cf9d3c47
KR
304 the last pair in lst which is to be retained but not yet copied, count
305 is how many from there are wanted. When there's no more deletions, *p
306 can be set to keeplst to share the remainder of the original lst. (The
307 entire original lst if there's no deletions at all.) */
d0a634de
KR
308
309 keeplst = lst;
cf9d3c47 310 count = 0;
d0a634de
KR
311 p = &ret;
312
896df2d5 313 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
d0a634de 314 {
a3e92377 315 if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (lst))))
d0a634de 316 {
cf9d3c47
KR
317 /* delete this element, so copy those at keeplst */
318 p = list_copy_part (keeplst, count, p);
d0a634de 319 keeplst = SCM_CDR (lst);
cf9d3c47
KR
320 count = 0;
321 }
322 else
323 {
324 /* keep this element */
325 count++;
d0a634de
KR
326 }
327 }
328
329 /* final retained elements */
330 *p = keeplst;
331
332 /* demand that lst was a proper list */
333 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
334
335 return ret;
336}
337#undef FUNC_NAME
338
339
340SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
341 (SCM x, SCM lst, SCM pred),
342 "Return a list containing the elements of @var{lst} but with\n"
343 "those equal to @var{x} deleted. The returned elements will be\n"
344 "in the same order as they were in @var{lst}.\n"
345 "\n"
346 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
347 "given. An equality call is made just once for each element,\n"
348 "but the order in which the calls are made on the elements is\n"
349 "unspecified.\n"
350 "\n"
351 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
352 "given @var{x} is first. This means for instance elements\n"
353 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
354 "\n"
355 "@var{lst} may be modified to construct the returned list.")
356#define FUNC_NAME s_scm_srfi1_delete_x
357{
d0a634de
KR
358 SCM walk;
359 SCM *prev;
360
361 if (SCM_UNBNDP (pred))
362 return scm_delete_x (x, lst);
363
a3e92377 364 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
d0a634de
KR
365
366 for (prev = &lst, walk = lst;
896df2d5 367 scm_is_pair (walk);
d0a634de
KR
368 walk = SCM_CDR (walk))
369 {
a3e92377 370 if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (walk))))
d0a634de
KR
371 *prev = SCM_CDR (walk);
372 else
373 prev = SCM_CDRLOC (walk);
374 }
375
376 /* demand the input was a proper list */
377 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
378 return lst;
379}
380#undef FUNC_NAME
381
382
383SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
384 (SCM lst, SCM pred),
385 "Return a list containing the elements of @var{lst} but without\n"
386 "duplicates.\n"
387 "\n"
388 "When elements are equal, only the first in @var{lst} is\n"
389 "retained. Equal elements can be anywhere in @var{lst}, they\n"
390 "don't have to be adjacent. The returned list will have the\n"
391 "retained elements in the same order as they were in @var{lst}.\n"
392 "\n"
393 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
394 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
395 "being before @var{y} in @var{lst}. A call is made at most once\n"
396 "for each combination, but the sequence of the calls across the\n"
397 "elements is unspecified.\n"
398 "\n"
399 "@var{lst} is not modified, but the return might share a common\n"
400 "tail with @var{lst}.\n"
401 "\n"
402 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
403 "it must check each element against all those preceding it. For\n"
404 "long lists it is more efficient to sort and then compare only\n"
405 "adjacent elements.")
406#define FUNC_NAME s_scm_srfi1_delete_duplicates
407{
408 scm_t_trampoline_2 equal_p;
409 SCM ret, *p, keeplst, item, l;
cf9d3c47 410 int count, i;
d0a634de
KR
411
412 /* ret is the new list constructed. p is where to append, initially &ret
413 then SCM_CDRLOC of the last pair. lst is advanced as each element is
414 considered.
415
416 Elements retained are not immediately appended to ret, instead keeplst
417 is the last pair in lst which is to be kept but is not yet copied.
418 Initially this is the first pair of lst, since the first element is
419 always retained.
420
421 *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
422 the elements retained, making the equality search loop easy.
423
424 If an item must be deleted, elements from keeplst (inclusive) to lst
425 (exclusive) must be copied and appended to ret. When there's no more
426 deletions, *p is left set to keeplst, so ret shares structure with the
427 original lst. (ret will be the entire original lst if there are no
428 deletions.) */
429
430 /* skip to end if an empty list (or something invalid) */
cf9d3c47
KR
431 ret = SCM_EOL;
432
433 if (SCM_UNBNDP (pred))
434 equal_p = equal_trampoline;
435 else
d0a634de 436 {
a3e92377
AW
437 SCM_VALIDATE_PROC (SCM_ARG2, pred);
438 equal_p = scm_call_2;
cf9d3c47 439 }
d0a634de 440
cf9d3c47
KR
441 keeplst = lst;
442 count = 0;
443 p = &ret;
d0a634de 444
cf9d3c47
KR
445 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
446 {
447 item = SCM_CAR (lst);
d0a634de 448
cf9d3c47
KR
449 /* look for item in "ret" list */
450 for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
451 {
452 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
d0a634de 453 {
cf9d3c47
KR
454 /* "item" is a duplicate, so copy keeplst onto ret */
455 duplicate:
456 p = list_copy_part (keeplst, count, p);
457
458 keeplst = SCM_CDR (lst); /* elem after the one deleted */
459 count = 0;
460 goto next_elem;
d0a634de
KR
461 }
462 }
d0a634de 463
cf9d3c47
KR
464 /* look for item in "keeplst" list
465 be careful traversing, in case nasty code changed the cdrs */
466 for (i = 0, l = keeplst;
467 i < count && scm_is_pair (l);
468 i++, l = SCM_CDR (l))
469 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
470 goto duplicate;
471
472 /* keep this element */
473 count++;
474
475 next_elem:
476 ;
477 }
d0a634de
KR
478 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
479
cf9d3c47
KR
480 /* share tail of keeplst items */
481 *p = keeplst;
482
d0a634de
KR
483 return ret;
484}
485#undef FUNC_NAME
486
487
488SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
489 (SCM lst, SCM pred),
490 "Return a list containing the elements of @var{lst} but without\n"
491 "duplicates.\n"
492 "\n"
493 "When elements are equal, only the first in @var{lst} is\n"
494 "retained. Equal elements can be anywhere in @var{lst}, they\n"
495 "don't have to be adjacent. The returned list will have the\n"
496 "retained elements in the same order as they were in @var{lst}.\n"
497 "\n"
498 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
499 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
500 "being before @var{y} in @var{lst}. A call is made at most once\n"
501 "for each combination, but the sequence of the calls across the\n"
502 "elements is unspecified.\n"
503 "\n"
504 "@var{lst} may be modified to construct the returned list.\n"
505 "\n"
506 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
507 "it must check each element against all those preceding it. For\n"
508 "long lists it is more efficient to sort and then compare only\n"
509 "adjacent elements.")
510#define FUNC_NAME s_scm_srfi1_delete_duplicates_x
511{
512 scm_t_trampoline_2 equal_p;
513 SCM ret, endret, item, l;
514
515 /* ret is the return list, constructed from the pairs in lst. endret is
516 the last pair of ret, initially the first pair. lst is advanced as
517 elements are considered. */
518
519 /* skip to end if an empty list (or something invalid) */
520 ret = lst;
896df2d5 521 if (scm_is_pair (lst))
d0a634de
KR
522 {
523 if (SCM_UNBNDP (pred))
524 equal_p = equal_trampoline;
525 else
526 {
a3e92377
AW
527 SCM_VALIDATE_PROC (SCM_ARG2, pred);
528 equal_p = scm_call_2;
d0a634de
KR
529 }
530
531 endret = ret;
532
533 /* loop over lst elements starting from second */
534 for (;;)
535 {
536 lst = SCM_CDR (lst);
896df2d5 537 if (! scm_is_pair (lst))
d0a634de
KR
538 break;
539 item = SCM_CAR (lst);
540
541 /* is item equal to any element from ret to endret (inclusive)? */
542 l = ret;
543 for (;;)
544 {
00874d5f 545 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
d0a634de
KR
546 break; /* equal, forget this element */
547
bc36d050 548 if (scm_is_eq (l, endret))
d0a634de
KR
549 {
550 /* not equal to any, so append this pair */
551 SCM_SETCDR (endret, lst);
552 endret = lst;
553 break;
554 }
555 l = SCM_CDR (l);
556 }
557 }
558
559 /* terminate, in case last element was deleted */
560 SCM_SETCDR (endret, SCM_EOL);
561 }
562
563 /* demand that lst was a proper list */
564 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
565
566 return ret;
567}
568#undef FUNC_NAME
569
570
5df2ac97
KR
571SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
572 (SCM pred, SCM lst),
573 "Return the first element of @var{lst} which satisfies the\n"
574 "predicate @var{pred}, or return @code{#f} if no such element is\n"
575 "found.")
576#define FUNC_NAME s_scm_srfi1_find
577{
a3e92377 578 SCM_VALIDATE_PROC (SCM_ARG1, pred);
5df2ac97
KR
579
580 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
581 {
582 SCM elem = SCM_CAR (lst);
a3e92377 583 if (scm_is_true (scm_call_1 (pred, elem)))
5df2ac97
KR
584 return elem;
585 }
586 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
587
588 return SCM_BOOL_F;
589}
590#undef FUNC_NAME
591
592
593SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
594 (SCM pred, SCM lst),
595 "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
596 "predicate @var{pred}, or return @code{#f} if no such element is\n"
597 "found.")
598#define FUNC_NAME s_scm_srfi1_find_tail
599{
a3e92377 600 SCM_VALIDATE_PROC (SCM_ARG1, pred);
5df2ac97
KR
601
602 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
a3e92377 603 if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
5df2ac97
KR
604 return lst;
605 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
606
607 return SCM_BOOL_F;
608}
609#undef FUNC_NAME
610
de51f595
KR
611SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
612 (SCM lst),
613 "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
614 "circular.")
615#define FUNC_NAME s_scm_srfi1_length_plus
616{
a5186f50
MW
617 size_t i = 0;
618 SCM tortoise = lst;
619 SCM hare = lst;
620
621 do
622 {
a5186f50 623 if (!scm_is_pair (hare))
fc8a9004
MW
624 {
625 if (SCM_NULL_OR_NIL_P (hare))
626 return scm_from_size_t (i);
627 else
628 scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
629 "proper or circular list");
630 }
a5186f50
MW
631 hare = SCM_CDR (hare);
632 i++;
a5186f50 633 if (!scm_is_pair (hare))
fc8a9004
MW
634 {
635 if (SCM_NULL_OR_NIL_P (hare))
636 return scm_from_size_t (i);
637 else
638 scm_wrong_type_arg_msg (FUNC_NAME, 1, lst,
639 "proper or circular list");
640 }
a5186f50
MW
641 hare = SCM_CDR (hare);
642 i++;
643 /* For every two steps the hare takes, the tortoise takes one. */
fc8a9004 644 tortoise = SCM_CDR (tortoise);
a5186f50
MW
645 }
646 while (!scm_is_eq (hare, tortoise));
647
648 /* If the tortoise ever catches the hare, then the list must contain
649 a cycle. */
650 return SCM_BOOL_F;
de51f595
KR
651}
652#undef FUNC_NAME
653
654
d61261f0
KR
655/* This routine differs from the core list-copy in allowing improper lists.
656 Maybe the core could allow them similarly. */
657
658SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
659 (SCM lst),
660 "Return a copy of the given list @var{lst}.\n"
661 "\n"
662 "@var{lst} can be a proper or improper list. And if @var{lst}\n"
663 "is not a pair then it's treated as the final tail of an\n"
664 "improper list and simply returned.")
665#define FUNC_NAME s_scm_srfi1_list_copy
666{
667 SCM newlst;
668 SCM * fill_here;
669 SCM from_here;
670
671 newlst = lst;
672 fill_here = &newlst;
673 from_here = lst;
674
896df2d5 675 while (scm_is_pair (from_here))
d61261f0
KR
676 {
677 SCM c;
678 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
679 *fill_here = c;
680 fill_here = SCM_CDRLOC (c);
681 from_here = SCM_CDR (from_here);
682 }
683 return newlst;
684}
685#undef FUNC_NAME
686
9dcee2b7
KR
687SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
688 (SCM equal, SCM lst, SCM rest),
689 "Return @var{lst} with any elements in the lists in @var{rest}\n"
690 "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
691 "just that list is returned.\n"
692 "\n"
693 "The given @var{equal} procedure is used for comparing elements,\n"
694 "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
695 "is from @var{lst} and the second from one of the subsequent\n"
696 "lists. But exactly which calls are made and in what order is\n"
697 "unspecified.\n"
698 "\n"
699 "@example\n"
700 "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
701 "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
702 "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
703 "@end example\n"
704 "\n"
705 "@code{lset-difference!} may modify @var{lst} to form its\n"
706 "result.")
707#define FUNC_NAME s_scm_srfi1_lset_difference_x
708{
9dcee2b7
KR
709 SCM ret, *pos, elem, r, b;
710 int argnum;
711
95e59982 712 SCM_VALIDATE_PROC (SCM_ARG1, equal);
9dcee2b7
KR
713 SCM_VALIDATE_REST_ARGUMENT (rest);
714
715 ret = SCM_EOL;
716 pos = &ret;
717 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
718 {
719 elem = SCM_CAR (lst);
720
721 for (r = rest, argnum = SCM_ARG3;
722 scm_is_pair (r);
723 r = SCM_CDR (r), argnum++)
724 {
725 for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
a3e92377 726 if (scm_is_true (scm_call_2 (equal, elem, SCM_CAR (b))))
9dcee2b7
KR
727 goto next_elem; /* equal to elem, so drop that elem */
728
729 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
730 }
731
732 /* elem not equal to anything in later lists, so keep it */
733 *pos = lst;
734 pos = SCM_CDRLOC (lst);
735
736 next_elem:
737 ;
738 }
739 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
740
741 *pos = SCM_EOL;
742 return ret;
743}
744#undef FUNC_NAME
745
746
7692d26b
MD
747SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
748 (SCM key, SCM alist, SCM pred),
b7e64f8b
BT
749 "Behaves like @code{assq} but uses third argument @var{pred}\n"
750 "for key comparison. If @var{pred} is not supplied,\n"
7692d26b
MD
751 "@code{equal?} is used. (Extended from R5RS.)\n")
752#define FUNC_NAME s_scm_srfi1_assoc
753{
754 SCM ls = alist;
755 scm_t_trampoline_2 equal_p;
756 if (SCM_UNBNDP (pred))
757 equal_p = equal_trampoline;
758 else
759 {
a3e92377
AW
760 SCM_VALIDATE_PROC (SCM_ARG3, pred);
761 equal_p = scm_call_2;
7692d26b 762 }
896df2d5 763 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
7692d26b
MD
764 {
765 SCM tmp = SCM_CAR (ls);
896df2d5 766 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
7692d26b 767 "association list");
9a993171 768 if (scm_is_true (equal_p (pred, key, SCM_CAR (tmp))))
7692d26b
MD
769 return tmp;
770 }
771 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
772 "association list");
773 return SCM_BOOL_F;
774}
775#undef FUNC_NAME
776
65978fb2
KR
777SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
778 (SCM pred, SCM list),
779 "Partition the elements of @var{list} with predicate @var{pred}.\n"
ffb62a43 780 "Return two values: the list of elements satisfying @var{pred} and\n"
65978fb2
KR
781 "the list of elements @emph{not} satisfying @var{pred}. The order\n"
782 "of the output lists follows the order of @var{list}. @var{list}\n"
783 "is not mutated. One of the output lists may share memory with @var{list}.\n")
784#define FUNC_NAME s_scm_srfi1_partition
785{
786 /* In this implementation, the output lists don't share memory with
787 list, because it's probably not worth the effort. */
0fb11ae4 788 SCM orig_list = list;
65978fb2
KR
789 SCM kept = scm_cons(SCM_EOL, SCM_EOL);
790 SCM kept_tail = kept;
791 SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
792 SCM dropped_tail = dropped;
793
a3e92377 794 SCM_VALIDATE_PROC (SCM_ARG1, pred);
65978fb2
KR
795
796 for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
0fb11ae4
LC
797 SCM elt, new_tail;
798
799 /* Make sure LIST is not a dotted list. */
800 SCM_ASSERT (scm_is_pair (list), orig_list, SCM_ARG2, FUNC_NAME);
801
802 elt = SCM_CAR (list);
803 new_tail = scm_cons (SCM_CAR (list), SCM_EOL);
804
a3e92377 805 if (scm_is_true (scm_call_1 (pred, elt))) {
65978fb2
KR
806 SCM_SETCDR(kept_tail, new_tail);
807 kept_tail = new_tail;
808 }
809 else {
810 SCM_SETCDR(dropped_tail, new_tail);
811 dropped_tail = new_tail;
812 }
813 }
814 /* re-use the initial conses for the values list */
815 SCM_SETCAR(kept, SCM_CDR(kept));
816 SCM_SETCDR(kept, dropped);
817 SCM_SETCAR(dropped, SCM_CDR(dropped));
818 SCM_SETCDR(dropped, SCM_EOL);
819 return scm_values(kept);
820}
821#undef FUNC_NAME
822
2b077051
KR
823
824SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
825 (SCM pred, SCM lst),
826 "Split @var{lst} into those elements which do and don't satisfy\n"
827 "the predicate @var{pred}.\n"
828 "\n"
829 "The return is two values (@pxref{Multiple Values}), the first\n"
830 "being a list of all elements from @var{lst} which satisfy\n"
831 "@var{pred}, the second a list of those which do not.\n"
832 "\n"
833 "The elements in the result lists are in the same order as in\n"
834 "@var{lst} but the order in which the calls @code{(@var{pred}\n"
835 "elem)} are made on the list elements is unspecified.\n"
836 "\n"
837 "@var{lst} may be modified to construct the return lists.")
838#define FUNC_NAME s_scm_srfi1_partition_x
839{
840 SCM tlst, flst, *tp, *fp;
2b077051 841
a3e92377 842 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
2b077051
KR
843
844 /* tlst and flst are the lists of true and false elements. tp and fp are
845 where to store to append to them, initially &tlst and &flst, then
846 SCM_CDRLOC of the last pair in the respective lists. */
847
848 tlst = SCM_EOL;
849 flst = SCM_EOL;
850 tp = &tlst;
851 fp = &flst;
852
853 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
854 {
a3e92377 855 if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
2b077051
KR
856 {
857 *tp = lst;
858 tp = SCM_CDRLOC (lst);
859 }
860 else
861 {
862 *fp = lst;
863 fp = SCM_CDRLOC (lst);
864 }
865 }
866
867 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
868
869 /* terminate whichever didn't get the last element(s) */
870 *tp = SCM_EOL;
871 *fp = SCM_EOL;
872
873 return scm_values (scm_list_2 (tlst, flst));
874}
875#undef FUNC_NAME
876
59747b8d
KR
877SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
878 (SCM pred, SCM list),
b7e64f8b 879 "Return a list containing all elements from @var{list} which do\n"
59747b8d 880 "not satisfy the predicate @var{pred}. The elements in the\n"
b7e64f8b 881 "result list have the same order as in @var{list}. The order in\n"
59747b8d
KR
882 "which @var{pred} is applied to the list elements is not\n"
883 "specified.")
884#define FUNC_NAME s_scm_srfi1_remove
885{
59747b8d
KR
886 SCM walk;
887 SCM *prev;
888 SCM res = SCM_EOL;
a3e92377 889 SCM_VALIDATE_PROC (SCM_ARG1, pred);
59747b8d
KR
890 SCM_VALIDATE_LIST (2, list);
891
892 for (prev = &res, walk = list;
893 scm_is_pair (walk);
894 walk = SCM_CDR (walk))
895 {
a3e92377 896 if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
59747b8d
KR
897 {
898 *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
899 prev = SCM_CDRLOC (*prev);
900 }
901 }
902
903 return res;
904}
905#undef FUNC_NAME
906
2b077051
KR
907
908SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
909 (SCM pred, SCM list),
910 "Return a list containing all elements from @var{list} which do\n"
911 "not satisfy the predicate @var{pred}. The elements in the\n"
912 "result list have the same order as in @var{list}. The order in\n"
913 "which @var{pred} is applied to the list elements is not\n"
914 "specified. @var{list} may be modified to build the return\n"
915 "list.")
916#define FUNC_NAME s_scm_srfi1_remove_x
917{
2b077051
KR
918 SCM walk;
919 SCM *prev;
a3e92377 920 SCM_VALIDATE_PROC (SCM_ARG1, pred);
2b077051
KR
921 SCM_VALIDATE_LIST (2, list);
922
923 for (prev = &list, walk = list;
924 scm_is_pair (walk);
925 walk = SCM_CDR (walk))
926 {
a3e92377 927 if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
2b077051
KR
928 prev = SCM_CDRLOC (walk);
929 else
930 *prev = SCM_CDR (walk);
931 }
932
933 return list;
934}
935#undef FUNC_NAME
936
0b7f2eb8 937\f
37710f7e
AW
938void
939scm_register_srfi_1 (void)
940{
941 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
942 "scm_init_srfi_1",
943 (scm_t_extension_init_func)scm_init_srfi_1, NULL);
944}
945
ee6aac97
MD
946void
947scm_init_srfi_1 (void)
948{
949#ifndef SCM_MAGIC_SNARFER
37710f7e 950#include "libguile/srfi-1.x"
ee6aac97
MD
951#endif
952}
953
954/* End of srfi-1.c. */