fix typos in the manual bits generated from source comments.
[bpt/guile.git] / libguile / srfi-1.c
1 /* srfi-1.c --- SRFI-1 procedures for Guile
2 *
3 * Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2005, 2006,
4 * 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
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.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
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
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 */
21
22 \f
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
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>
36
37
38 /* The intent of this file was to gradually replace those Scheme
39 * procedures in srfi-1.scm that extend core primitive procedures,
40 * so that using srfi-1 wouldn't have performance penalties.
41 *
42 * However, we now prefer to write these procedures in Scheme, let the compiler
43 * optimize them, and have the VM execute them efficiently.
44 */
45
46
47 static long
48 srfi1_ilength (SCM sx)
49 {
50 long i = 0;
51 SCM tortoise = sx;
52 SCM hare = sx;
53
54 do {
55 if (SCM_NULL_OR_NIL_P(hare)) return i;
56 if (!scm_is_pair (hare)) return -2;
57 hare = SCM_CDR(hare);
58 i++;
59 if (SCM_NULL_OR_NIL_P(hare)) return i;
60 if (!scm_is_pair (hare)) return -2;
61 hare = SCM_CDR(hare);
62 i++;
63 /* For every two steps the hare takes, the tortoise takes one. */
64 tortoise = SCM_CDR(tortoise);
65 }
66 while (! scm_is_eq (hare, tortoise));
67
68 /* If the tortoise ever catches the hare, then the list must contain
69 a cycle. */
70 return -1;
71 }
72
73 static SCM
74 equal_trampoline (SCM proc, SCM arg1, SCM arg2)
75 {
76 return scm_equal_p (arg1, arg2);
77 }
78
79 /* list_copy_part() copies the first COUNT cells of LST, puts the result at
80 *dst, and returns the SCM_CDRLOC of the last cell in that new list.
81
82 This function is designed to be careful about LST possibly having changed
83 in between the caller deciding what to copy, and the copy actually being
84 done here. The COUNT ensures we terminate if LST has become circular,
85 SCM_VALIDATE_CONS guards against a cdr in the list changed to some
86 non-pair object. */
87
88 #include <stdio.h>
89 static SCM *
90 list_copy_part (SCM lst, int count, SCM *dst)
91 #define FUNC_NAME "list_copy_part"
92 {
93 SCM c;
94 for ( ; count > 0; count--)
95 {
96 SCM_VALIDATE_CONS (SCM_ARGn, lst);
97 c = scm_cons (SCM_CAR (lst), SCM_EOL);
98 *dst = c;
99 dst = SCM_CDRLOC (c);
100 lst = SCM_CDR (lst);
101 }
102 return dst;
103 }
104 #undef FUNC_NAME
105
106
107 SCM_DEFINE (scm_srfi1_append_reverse, "append-reverse", 2, 0, 0,
108 (SCM revhead, SCM tail),
109 "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
110 "result. This is equivalent to @code{(append (reverse\n"
111 "@var{rev-head}) @var{tail})}, but its implementation is more\n"
112 "efficient.\n"
113 "\n"
114 "@example\n"
115 "(append-reverse '(1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
116 "@end example")
117 #define FUNC_NAME s_scm_srfi1_append_reverse
118 {
119 while (scm_is_pair (revhead))
120 {
121 /* copy first element of revhead onto front of tail */
122 tail = scm_cons (SCM_CAR (revhead), tail);
123 revhead = SCM_CDR (revhead);
124 }
125 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
126 "list");
127 return tail;
128 }
129 #undef FUNC_NAME
130
131
132 SCM_DEFINE (scm_srfi1_append_reverse_x, "append-reverse!", 2, 0, 0,
133 (SCM revhead, SCM tail),
134 "Reverse @var{rev-head}, append @var{tail} to it, and return the\n"
135 "result. This is equivalent to @code{(append! (reverse!\n"
136 "@var{rev-head}) @var{tail})}, but its implementation is more\n"
137 "efficient.\n"
138 "\n"
139 "@example\n"
140 "(append-reverse! (list 1 2 3) '(4 5 6)) @result{} (3 2 1 4 5 6)\n"
141 "@end example\n"
142 "\n"
143 "@var{rev-head} may be modified in order to produce the result.")
144 #define FUNC_NAME s_scm_srfi1_append_reverse_x
145 {
146 SCM newtail;
147
148 while (scm_is_pair (revhead))
149 {
150 /* take the first cons cell from revhead */
151 newtail = revhead;
152 revhead = SCM_CDR (revhead);
153
154 /* make it the new start of tail, appending the previous */
155 SCM_SETCDR (newtail, tail);
156 tail = newtail;
157 }
158 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (revhead), revhead, SCM_ARG1, FUNC_NAME,
159 "list");
160 return tail;
161 }
162 #undef FUNC_NAME
163
164 SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
165 (SCM lstlst),
166 "Construct a list by appending all lists in @var{lstlst}.\n"
167 "\n"
168 "@code{concatenate} is the same as @code{(apply append\n"
169 "@var{lstlst})}. It exists because some Scheme implementations\n"
170 "have a limit on the number of arguments a function takes, which\n"
171 "the @code{apply} might exceed. In Guile there is no such\n"
172 "limit.")
173 #define FUNC_NAME s_scm_srfi1_concatenate
174 {
175 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
176 return scm_append (lstlst);
177 }
178 #undef FUNC_NAME
179
180
181 SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
182 (SCM lstlst),
183 "Construct a list by appending all lists in @var{lstlst}. Those\n"
184 "lists may be modified to produce the result.\n"
185 "\n"
186 "@code{concatenate!} is the same as @code{(apply append!\n"
187 "@var{lstlst})}. It exists because some Scheme implementations\n"
188 "have a limit on the number of arguments a function takes, which\n"
189 "the @code{apply} might exceed. In Guile there is no such\n"
190 "limit.")
191 #define FUNC_NAME s_scm_srfi1_concatenate_x
192 {
193 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
194 return scm_append_x (lstlst);
195 }
196 #undef FUNC_NAME
197
198
199 SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
200 (SCM pred, SCM list1, SCM rest),
201 "Return a count of the number of times @var{pred} returns true\n"
202 "when called on elements from the given lists.\n"
203 "\n"
204 "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
205 "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
206 "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
207 "with the first element of each list, the second with the second\n"
208 "element from each, and so on.\n"
209 "\n"
210 "Counting stops when the end of the shortest list is reached.\n"
211 "At least one list must be non-circular.")
212 #define FUNC_NAME s_scm_srfi1_count
213 {
214 long count;
215 SCM lst;
216 int argnum;
217 SCM_VALIDATE_REST_ARGUMENT (rest);
218
219 count = 0;
220
221 if (scm_is_null (rest))
222 {
223 /* one list */
224 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
225
226 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
227 count += scm_is_true (scm_call_1 (pred, SCM_CAR (list1)));
228
229 /* check below that list1 is a proper list, and done */
230 end_list1:
231 lst = list1;
232 argnum = 2;
233 }
234 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
235 {
236 /* two lists */
237 SCM list2;
238
239 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
240
241 list2 = SCM_CAR (rest);
242 for (;;)
243 {
244 if (! scm_is_pair (list1))
245 goto end_list1;
246 if (! scm_is_pair (list2))
247 {
248 lst = list2;
249 argnum = 3;
250 break;
251 }
252 count += scm_is_true (scm_call_2
253 (pred, SCM_CAR (list1), SCM_CAR (list2)));
254 list1 = SCM_CDR (list1);
255 list2 = SCM_CDR (list2);
256 }
257 }
258 else
259 {
260 /* three or more lists */
261 SCM vec, args, a;
262 size_t len, i;
263
264 /* vec is the list arguments */
265 vec = scm_vector (scm_cons (list1, rest));
266 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
267
268 /* args is the argument list to pass to pred, same length as vec,
269 re-used for each call */
270 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
271
272 for (;;)
273 {
274 /* first elem of each list in vec into args, and step those
275 vec entries onto their next element */
276 for (i = 0, a = args, argnum = 2;
277 i < len;
278 i++, a = SCM_CDR (a), argnum++)
279 {
280 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
281 if (! scm_is_pair (lst))
282 goto check_lst_and_done;
283 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
284 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
285 }
286
287 count += scm_is_true (scm_apply (pred, args, SCM_EOL));
288 }
289 }
290
291 check_lst_and_done:
292 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
293 return scm_from_long (count);
294 }
295 #undef FUNC_NAME
296
297
298 SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
299 (SCM x, SCM lst, SCM pred),
300 "Return a list containing the elements of @var{lst} but with\n"
301 "those equal to @var{x} deleted. The returned elements will be\n"
302 "in the same order as they were in @var{lst}.\n"
303 "\n"
304 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
305 "given. An equality call is made just once for each element,\n"
306 "but the order in which the calls are made on the elements is\n"
307 "unspecified.\n"
308 "\n"
309 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
310 "given @var{x} is first. This means for instance elements\n"
311 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
312 "\n"
313 "@var{lst} is not modified, but the returned list might share a\n"
314 "common tail with @var{lst}.")
315 #define FUNC_NAME s_scm_srfi1_delete
316 {
317 SCM ret, *p, keeplst;
318 int count;
319
320 if (SCM_UNBNDP (pred))
321 return scm_delete (x, lst);
322
323 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
324
325 /* ret is the return list being constructed. p is where to append to it,
326 initially &ret then SCM_CDRLOC of the last pair. lst progresses as
327 elements are considered.
328
329 Elements to be retained are not immediately copied, instead keeplst is
330 the last pair in lst which is to be retained but not yet copied, count
331 is how many from there are wanted. When there's no more deletions, *p
332 can be set to keeplst to share the remainder of the original lst. (The
333 entire original lst if there's no deletions at all.) */
334
335 keeplst = lst;
336 count = 0;
337 p = &ret;
338
339 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
340 {
341 if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (lst))))
342 {
343 /* delete this element, so copy those at keeplst */
344 p = list_copy_part (keeplst, count, p);
345 keeplst = SCM_CDR (lst);
346 count = 0;
347 }
348 else
349 {
350 /* keep this element */
351 count++;
352 }
353 }
354
355 /* final retained elements */
356 *p = keeplst;
357
358 /* demand that lst was a proper list */
359 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
360
361 return ret;
362 }
363 #undef FUNC_NAME
364
365
366 SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
367 (SCM x, SCM lst, SCM pred),
368 "Return a list containing the elements of @var{lst} but with\n"
369 "those equal to @var{x} deleted. The returned elements will be\n"
370 "in the same order as they were in @var{lst}.\n"
371 "\n"
372 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
373 "given. An equality call is made just once for each element,\n"
374 "but the order in which the calls are made on the elements is\n"
375 "unspecified.\n"
376 "\n"
377 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
378 "given @var{x} is first. This means for instance elements\n"
379 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
380 "\n"
381 "@var{lst} may be modified to construct the returned list.")
382 #define FUNC_NAME s_scm_srfi1_delete_x
383 {
384 SCM walk;
385 SCM *prev;
386
387 if (SCM_UNBNDP (pred))
388 return scm_delete_x (x, lst);
389
390 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG3, FUNC_NAME);
391
392 for (prev = &lst, walk = lst;
393 scm_is_pair (walk);
394 walk = SCM_CDR (walk))
395 {
396 if (scm_is_true (scm_call_2 (pred, x, SCM_CAR (walk))))
397 *prev = SCM_CDR (walk);
398 else
399 prev = SCM_CDRLOC (walk);
400 }
401
402 /* demand the input was a proper list */
403 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
404 return lst;
405 }
406 #undef FUNC_NAME
407
408
409 SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
410 (SCM lst, SCM pred),
411 "Return a list containing the elements of @var{lst} but without\n"
412 "duplicates.\n"
413 "\n"
414 "When elements are equal, only the first in @var{lst} is\n"
415 "retained. Equal elements can be anywhere in @var{lst}, they\n"
416 "don't have to be adjacent. The returned list will have the\n"
417 "retained elements in the same order as they were in @var{lst}.\n"
418 "\n"
419 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
420 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
421 "being before @var{y} in @var{lst}. A call is made at most once\n"
422 "for each combination, but the sequence of the calls across the\n"
423 "elements is unspecified.\n"
424 "\n"
425 "@var{lst} is not modified, but the return might share a common\n"
426 "tail with @var{lst}.\n"
427 "\n"
428 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
429 "it must check each element against all those preceding it. For\n"
430 "long lists it is more efficient to sort and then compare only\n"
431 "adjacent elements.")
432 #define FUNC_NAME s_scm_srfi1_delete_duplicates
433 {
434 scm_t_trampoline_2 equal_p;
435 SCM ret, *p, keeplst, item, l;
436 int count, i;
437
438 /* ret is the new list constructed. p is where to append, initially &ret
439 then SCM_CDRLOC of the last pair. lst is advanced as each element is
440 considered.
441
442 Elements retained are not immediately appended to ret, instead keeplst
443 is the last pair in lst which is to be kept but is not yet copied.
444 Initially this is the first pair of lst, since the first element is
445 always retained.
446
447 *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
448 the elements retained, making the equality search loop easy.
449
450 If an item must be deleted, elements from keeplst (inclusive) to lst
451 (exclusive) must be copied and appended to ret. When there's no more
452 deletions, *p is left set to keeplst, so ret shares structure with the
453 original lst. (ret will be the entire original lst if there are no
454 deletions.) */
455
456 /* skip to end if an empty list (or something invalid) */
457 ret = SCM_EOL;
458
459 if (SCM_UNBNDP (pred))
460 equal_p = equal_trampoline;
461 else
462 {
463 SCM_VALIDATE_PROC (SCM_ARG2, pred);
464 equal_p = scm_call_2;
465 }
466
467 keeplst = lst;
468 count = 0;
469 p = &ret;
470
471 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
472 {
473 item = SCM_CAR (lst);
474
475 /* look for item in "ret" list */
476 for (l = ret; scm_is_pair (l); l = SCM_CDR (l))
477 {
478 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
479 {
480 /* "item" is a duplicate, so copy keeplst onto ret */
481 duplicate:
482 p = list_copy_part (keeplst, count, p);
483
484 keeplst = SCM_CDR (lst); /* elem after the one deleted */
485 count = 0;
486 goto next_elem;
487 }
488 }
489
490 /* look for item in "keeplst" list
491 be careful traversing, in case nasty code changed the cdrs */
492 for (i = 0, l = keeplst;
493 i < count && scm_is_pair (l);
494 i++, l = SCM_CDR (l))
495 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
496 goto duplicate;
497
498 /* keep this element */
499 count++;
500
501 next_elem:
502 ;
503 }
504 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
505
506 /* share tail of keeplst items */
507 *p = keeplst;
508
509 return ret;
510 }
511 #undef FUNC_NAME
512
513
514 SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
515 (SCM lst, SCM pred),
516 "Return a list containing the elements of @var{lst} but without\n"
517 "duplicates.\n"
518 "\n"
519 "When elements are equal, only the first in @var{lst} is\n"
520 "retained. Equal elements can be anywhere in @var{lst}, they\n"
521 "don't have to be adjacent. The returned list will have the\n"
522 "retained elements in the same order as they were in @var{lst}.\n"
523 "\n"
524 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
525 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
526 "being before @var{y} in @var{lst}. A call is made at most once\n"
527 "for each combination, but the sequence of the calls across the\n"
528 "elements is unspecified.\n"
529 "\n"
530 "@var{lst} may be modified to construct the returned list.\n"
531 "\n"
532 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
533 "it must check each element against all those preceding it. For\n"
534 "long lists it is more efficient to sort and then compare only\n"
535 "adjacent elements.")
536 #define FUNC_NAME s_scm_srfi1_delete_duplicates_x
537 {
538 scm_t_trampoline_2 equal_p;
539 SCM ret, endret, item, l;
540
541 /* ret is the return list, constructed from the pairs in lst. endret is
542 the last pair of ret, initially the first pair. lst is advanced as
543 elements are considered. */
544
545 /* skip to end if an empty list (or something invalid) */
546 ret = lst;
547 if (scm_is_pair (lst))
548 {
549 if (SCM_UNBNDP (pred))
550 equal_p = equal_trampoline;
551 else
552 {
553 SCM_VALIDATE_PROC (SCM_ARG2, pred);
554 equal_p = scm_call_2;
555 }
556
557 endret = ret;
558
559 /* loop over lst elements starting from second */
560 for (;;)
561 {
562 lst = SCM_CDR (lst);
563 if (! scm_is_pair (lst))
564 break;
565 item = SCM_CAR (lst);
566
567 /* is item equal to any element from ret to endret (inclusive)? */
568 l = ret;
569 for (;;)
570 {
571 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
572 break; /* equal, forget this element */
573
574 if (scm_is_eq (l, endret))
575 {
576 /* not equal to any, so append this pair */
577 SCM_SETCDR (endret, lst);
578 endret = lst;
579 break;
580 }
581 l = SCM_CDR (l);
582 }
583 }
584
585 /* terminate, in case last element was deleted */
586 SCM_SETCDR (endret, SCM_EOL);
587 }
588
589 /* demand that lst was a proper list */
590 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
591
592 return ret;
593 }
594 #undef FUNC_NAME
595
596
597 SCM_DEFINE (scm_srfi1_drop_right, "drop-right", 2, 0, 0,
598 (SCM lst, SCM n),
599 "Return a new list containing all except the last @var{n}\n"
600 "elements of @var{lst}.")
601 #define FUNC_NAME s_scm_srfi1_drop_right
602 {
603 SCM tail = scm_list_tail (lst, n);
604 SCM ret = SCM_EOL;
605 SCM *rend = &ret;
606 while (scm_is_pair (tail))
607 {
608 *rend = scm_cons (SCM_CAR (lst), SCM_EOL);
609 rend = SCM_CDRLOC (*rend);
610
611 lst = SCM_CDR (lst);
612 tail = SCM_CDR (tail);
613 }
614 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
615 return ret;
616 }
617 #undef FUNC_NAME
618
619 SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
620 (SCM pred, SCM lst),
621 "Return the first element of @var{lst} which satisfies the\n"
622 "predicate @var{pred}, or return @code{#f} if no such element is\n"
623 "found.")
624 #define FUNC_NAME s_scm_srfi1_find
625 {
626 SCM_VALIDATE_PROC (SCM_ARG1, pred);
627
628 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
629 {
630 SCM elem = SCM_CAR (lst);
631 if (scm_is_true (scm_call_1 (pred, elem)))
632 return elem;
633 }
634 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
635
636 return SCM_BOOL_F;
637 }
638 #undef FUNC_NAME
639
640
641 SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
642 (SCM pred, SCM lst),
643 "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
644 "predicate @var{pred}, or return @code{#f} if no such element is\n"
645 "found.")
646 #define FUNC_NAME s_scm_srfi1_find_tail
647 {
648 SCM_VALIDATE_PROC (SCM_ARG1, pred);
649
650 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
651 if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
652 return lst;
653 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
654
655 return SCM_BOOL_F;
656 }
657 #undef FUNC_NAME
658
659 SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
660 (SCM lst),
661 "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
662 "circular.")
663 #define FUNC_NAME s_scm_srfi1_length_plus
664 {
665 long len = scm_ilength (lst);
666 return (len >= 0 ? SCM_I_MAKINUM (len) : SCM_BOOL_F);
667 }
668 #undef FUNC_NAME
669
670
671 /* This routine differs from the core list-copy in allowing improper lists.
672 Maybe the core could allow them similarly. */
673
674 SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
675 (SCM lst),
676 "Return a copy of the given list @var{lst}.\n"
677 "\n"
678 "@var{lst} can be a proper or improper list. And if @var{lst}\n"
679 "is not a pair then it's treated as the final tail of an\n"
680 "improper list and simply returned.")
681 #define FUNC_NAME s_scm_srfi1_list_copy
682 {
683 SCM newlst;
684 SCM * fill_here;
685 SCM from_here;
686
687 newlst = lst;
688 fill_here = &newlst;
689 from_here = lst;
690
691 while (scm_is_pair (from_here))
692 {
693 SCM c;
694 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
695 *fill_here = c;
696 fill_here = SCM_CDRLOC (c);
697 from_here = SCM_CDR (from_here);
698 }
699 return newlst;
700 }
701 #undef FUNC_NAME
702
703 SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
704 (SCM equal, SCM lst, SCM rest),
705 "Return @var{lst} with any elements in the lists in @var{rest}\n"
706 "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
707 "just that list is returned.\n"
708 "\n"
709 "The given @var{equal} procedure is used for comparing elements,\n"
710 "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
711 "is from @var{lst} and the second from one of the subsequent\n"
712 "lists. But exactly which calls are made and in what order is\n"
713 "unspecified.\n"
714 "\n"
715 "@example\n"
716 "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
717 "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
718 "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
719 "@end example\n"
720 "\n"
721 "@code{lset-difference!} may modify @var{lst} to form its\n"
722 "result.")
723 #define FUNC_NAME s_scm_srfi1_lset_difference_x
724 {
725 SCM ret, *pos, elem, r, b;
726 int argnum;
727
728 SCM_VALIDATE_PROC (SCM_ARG1, equal);
729 SCM_VALIDATE_REST_ARGUMENT (rest);
730
731 ret = SCM_EOL;
732 pos = &ret;
733 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
734 {
735 elem = SCM_CAR (lst);
736
737 for (r = rest, argnum = SCM_ARG3;
738 scm_is_pair (r);
739 r = SCM_CDR (r), argnum++)
740 {
741 for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
742 if (scm_is_true (scm_call_2 (equal, elem, SCM_CAR (b))))
743 goto next_elem; /* equal to elem, so drop that elem */
744
745 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
746 }
747
748 /* elem not equal to anything in later lists, so keep it */
749 *pos = lst;
750 pos = SCM_CDRLOC (lst);
751
752 next_elem:
753 ;
754 }
755 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
756
757 *pos = SCM_EOL;
758 return ret;
759 }
760 #undef FUNC_NAME
761
762
763 /* Typechecking for multi-argument MAP and FOR-EACH.
764
765 Verify that each element of the vector ARGV, except for the first,
766 is a list and return minimum length. Attribute errors to WHO,
767 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
768 static inline int
769 check_map_args (SCM argv,
770 long len,
771 SCM gf,
772 SCM proc,
773 SCM args,
774 const char *who)
775 {
776 long i;
777 SCM elt;
778
779 for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
780 {
781 long elt_len;
782 elt = SCM_SIMPLE_VECTOR_REF (argv, i);
783
784 if (!(scm_is_null (elt) || scm_is_pair (elt)))
785 goto check_map_error;
786
787 elt_len = srfi1_ilength (elt);
788 if (elt_len < -1)
789 goto check_map_error;
790
791 if (len < 0 || (elt_len >= 0 && elt_len < len))
792 len = elt_len;
793 }
794
795 if (len < 0)
796 {
797 /* i == 0 */
798 elt = SCM_EOL;
799 check_map_error:
800 if (gf)
801 scm_apply_generic (gf, scm_cons (proc, args));
802 else
803 scm_wrong_type_arg (who, i + 2, elt);
804 }
805
806 scm_remember_upto_here_1 (argv);
807 return len;
808 }
809
810
811 SCM_GPROC (s_srfi1_map, "map", 2, 0, 1, scm_srfi1_map, g_srfi1_map);
812
813 /* Note: Currently, scm_srfi1_map applies PROC to the argument list(s)
814 sequentially, starting with the first element(s). This is used in
815 the Scheme procedure `map-in-order', which guarantees sequential
816 behaviour, is implemented using scm_map. If the behaviour changes,
817 we need to update `map-in-order'.
818 */
819
820 SCM
821 scm_srfi1_map (SCM proc, SCM arg1, SCM args)
822 #define FUNC_NAME s_srfi1_map
823 {
824 long i, len;
825 SCM res = SCM_EOL;
826 SCM *pres = &res;
827
828 len = srfi1_ilength (arg1);
829 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
830 g_srfi1_map,
831 scm_cons2 (proc, arg1, args), SCM_ARG2, s_srfi1_map);
832 SCM_VALIDATE_REST_ARGUMENT (args);
833 if (scm_is_null (args))
834 {
835 SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_srfi1_map,
836 proc, arg1, SCM_ARG1, s_srfi1_map);
837 SCM_GASSERT2 (len >= 0, g_srfi1_map, proc, arg1, SCM_ARG2, s_srfi1_map);
838 while (SCM_NIMP (arg1))
839 {
840 *pres = scm_list_1 (scm_call_1 (proc, SCM_CAR (arg1)));
841 pres = SCM_CDRLOC (*pres);
842 arg1 = SCM_CDR (arg1);
843 }
844 return res;
845 }
846 if (scm_is_null (SCM_CDR (args)))
847 {
848 SCM arg2 = SCM_CAR (args);
849 int len2 = srfi1_ilength (arg2);
850 SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_srfi1_map,
851 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_map);
852 if (len < 0 || (len2 >= 0 && len2 < len))
853 len = len2;
854 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
855 && len >= 0 && len2 >= -1,
856 g_srfi1_map,
857 scm_cons2 (proc, arg1, args),
858 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
859 s_srfi1_map);
860 while (len > 0)
861 {
862 *pres = scm_list_1 (scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
863 pres = SCM_CDRLOC (*pres);
864 arg1 = SCM_CDR (arg1);
865 arg2 = SCM_CDR (arg2);
866 --len;
867 }
868 return res;
869 }
870 args = scm_vector (arg1 = scm_cons (arg1, args));
871 len = check_map_args (args, len, g_srfi1_map, proc, arg1, s_srfi1_map);
872 while (len > 0)
873 {
874 arg1 = SCM_EOL;
875 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
876 {
877 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
878 arg1 = scm_cons (SCM_CAR (elt), arg1);
879 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
880 }
881 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
882 pres = SCM_CDRLOC (*pres);
883 --len;
884 }
885 return res;
886 }
887 #undef FUNC_NAME
888
889 SCM_REGISTER_PROC (s_srfi1_map_in_order, "map-in-order", 2, 0, 1, scm_srfi1_map);
890
891 SCM_GPROC (s_srfi1_for_each, "for-each", 2, 0, 1, scm_srfi1_for_each, g_srfi1_for_each);
892
893 SCM
894 scm_srfi1_for_each (SCM proc, SCM arg1, SCM args)
895 #define FUNC_NAME s_srfi1_for_each
896 {
897 long i, len;
898 len = srfi1_ilength (arg1);
899 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
900 g_srfi1_for_each, scm_cons2 (proc, arg1, args),
901 SCM_ARG2, s_srfi1_for_each);
902 SCM_VALIDATE_REST_ARGUMENT (args);
903 if (scm_is_null (args))
904 {
905 SCM_GASSERT2 (scm_is_true (scm_procedure_p (proc)), g_srfi1_for_each,
906 proc, arg1, SCM_ARG1, s_srfi1_for_each);
907 SCM_GASSERT2 (len >= 0, g_srfi1_for_each, proc, arg1,
908 SCM_ARG2, s_srfi1_map);
909 while (SCM_NIMP (arg1))
910 {
911 scm_call_1 (proc, SCM_CAR (arg1));
912 arg1 = SCM_CDR (arg1);
913 }
914 return SCM_UNSPECIFIED;
915 }
916 if (scm_is_null (SCM_CDR (args)))
917 {
918 SCM arg2 = SCM_CAR (args);
919 int len2 = srfi1_ilength (arg2);
920 SCM_GASSERTn (scm_is_true (scm_procedure_p (proc)), g_srfi1_for_each,
921 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_for_each);
922 if (len < 0 || (len2 >= 0 && len2 < len))
923 len = len2;
924 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
925 && len >= 0 && len2 >= -1,
926 g_srfi1_for_each,
927 scm_cons2 (proc, arg1, args),
928 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
929 s_srfi1_for_each);
930 while (len > 0)
931 {
932 scm_call_2 (proc, SCM_CAR (arg1), SCM_CAR (arg2));
933 arg1 = SCM_CDR (arg1);
934 arg2 = SCM_CDR (arg2);
935 --len;
936 }
937 return SCM_UNSPECIFIED;
938 }
939 args = scm_vector (arg1 = scm_cons (arg1, args));
940 len = check_map_args (args, len, g_srfi1_for_each, proc, arg1,
941 s_srfi1_for_each);
942 while (len > 0)
943 {
944 arg1 = SCM_EOL;
945 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
946 {
947 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
948 arg1 = scm_cons (SCM_CAR (elt), arg1);
949 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
950 }
951 scm_apply (proc, arg1, SCM_EOL);
952 --len;
953 }
954 return SCM_UNSPECIFIED;
955 }
956 #undef FUNC_NAME
957
958
959 SCM_DEFINE (scm_srfi1_member, "member", 2, 1, 0,
960 (SCM x, SCM lst, SCM pred),
961 "Return the first sublist of @var{lst} whose @sc{car} is equal\n"
962 "to @var{x}. If @var{x} does not appear in @var{lst}, return\n"
963 "@code{#f}.\n"
964 "\n"
965 "Equality is determined by @code{equal?}, or by the equality\n"
966 "predicate @var{=} if given. @var{=} is called @code{(= @var{x}\n"
967 "elem)}, ie.@: with the given @var{x} first, so for example to\n"
968 "find the first element greater than 5,\n"
969 "\n"
970 "@example\n"
971 "(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)\n"
972 "@end example\n"
973 "\n"
974 "This version of @code{member} extends the core @code{member} by\n"
975 "accepting an equality predicate.")
976 #define FUNC_NAME s_scm_srfi1_member
977 {
978 scm_t_trampoline_2 equal_p;
979 SCM_VALIDATE_LIST (2, lst);
980 if (SCM_UNBNDP (pred))
981 equal_p = equal_trampoline;
982 else
983 {
984 SCM_VALIDATE_PROC (SCM_ARG3, pred);
985 equal_p = scm_call_2;
986 }
987 for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
988 {
989 if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
990 return lst;
991 }
992 return SCM_BOOL_F;
993 }
994 #undef FUNC_NAME
995
996 SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
997 (SCM key, SCM alist, SCM pred),
998 "Behaves like @code{assq} but uses third argument @var{pred?}\n"
999 "for key comparison. If @var{pred?} is not supplied,\n"
1000 "@code{equal?} is used. (Extended from R5RS.)\n")
1001 #define FUNC_NAME s_scm_srfi1_assoc
1002 {
1003 SCM ls = alist;
1004 scm_t_trampoline_2 equal_p;
1005 if (SCM_UNBNDP (pred))
1006 equal_p = equal_trampoline;
1007 else
1008 {
1009 SCM_VALIDATE_PROC (SCM_ARG3, pred);
1010 equal_p = scm_call_2;
1011 }
1012 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
1013 {
1014 SCM tmp = SCM_CAR (ls);
1015 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1016 "association list");
1017 if (scm_is_true (equal_p (pred, key, SCM_CAR (tmp))))
1018 return tmp;
1019 }
1020 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1021 "association list");
1022 return SCM_BOOL_F;
1023 }
1024 #undef FUNC_NAME
1025
1026 SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
1027 (SCM pred, SCM list),
1028 "Partition the elements of @var{list} with predicate @var{pred}.\n"
1029 "Return two values: the list of elements satisfying @var{pred} and\n"
1030 "the list of elements @emph{not} satisfying @var{pred}. The order\n"
1031 "of the output lists follows the order of @var{list}. @var{list}\n"
1032 "is not mutated. One of the output lists may share memory with @var{list}.\n")
1033 #define FUNC_NAME s_scm_srfi1_partition
1034 {
1035 /* In this implementation, the output lists don't share memory with
1036 list, because it's probably not worth the effort. */
1037 SCM orig_list = list;
1038 SCM kept = scm_cons(SCM_EOL, SCM_EOL);
1039 SCM kept_tail = kept;
1040 SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
1041 SCM dropped_tail = dropped;
1042
1043 SCM_VALIDATE_PROC (SCM_ARG1, pred);
1044
1045 for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
1046 SCM elt, new_tail;
1047
1048 /* Make sure LIST is not a dotted list. */
1049 SCM_ASSERT (scm_is_pair (list), orig_list, SCM_ARG2, FUNC_NAME);
1050
1051 elt = SCM_CAR (list);
1052 new_tail = scm_cons (SCM_CAR (list), SCM_EOL);
1053
1054 if (scm_is_true (scm_call_1 (pred, elt))) {
1055 SCM_SETCDR(kept_tail, new_tail);
1056 kept_tail = new_tail;
1057 }
1058 else {
1059 SCM_SETCDR(dropped_tail, new_tail);
1060 dropped_tail = new_tail;
1061 }
1062 }
1063 /* re-use the initial conses for the values list */
1064 SCM_SETCAR(kept, SCM_CDR(kept));
1065 SCM_SETCDR(kept, dropped);
1066 SCM_SETCAR(dropped, SCM_CDR(dropped));
1067 SCM_SETCDR(dropped, SCM_EOL);
1068 return scm_values(kept);
1069 }
1070 #undef FUNC_NAME
1071
1072
1073 SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
1074 (SCM pred, SCM lst),
1075 "Split @var{lst} into those elements which do and don't satisfy\n"
1076 "the predicate @var{pred}.\n"
1077 "\n"
1078 "The return is two values (@pxref{Multiple Values}), the first\n"
1079 "being a list of all elements from @var{lst} which satisfy\n"
1080 "@var{pred}, the second a list of those which do not.\n"
1081 "\n"
1082 "The elements in the result lists are in the same order as in\n"
1083 "@var{lst} but the order in which the calls @code{(@var{pred}\n"
1084 "elem)} are made on the list elements is unspecified.\n"
1085 "\n"
1086 "@var{lst} may be modified to construct the return lists.")
1087 #define FUNC_NAME s_scm_srfi1_partition_x
1088 {
1089 SCM tlst, flst, *tp, *fp;
1090
1091 SCM_ASSERT (scm_is_true (scm_procedure_p (pred)), pred, SCM_ARG1, FUNC_NAME);
1092
1093 /* tlst and flst are the lists of true and false elements. tp and fp are
1094 where to store to append to them, initially &tlst and &flst, then
1095 SCM_CDRLOC of the last pair in the respective lists. */
1096
1097 tlst = SCM_EOL;
1098 flst = SCM_EOL;
1099 tp = &tlst;
1100 fp = &flst;
1101
1102 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1103 {
1104 if (scm_is_true (scm_call_1 (pred, SCM_CAR (lst))))
1105 {
1106 *tp = lst;
1107 tp = SCM_CDRLOC (lst);
1108 }
1109 else
1110 {
1111 *fp = lst;
1112 fp = SCM_CDRLOC (lst);
1113 }
1114 }
1115
1116 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1117
1118 /* terminate whichever didn't get the last element(s) */
1119 *tp = SCM_EOL;
1120 *fp = SCM_EOL;
1121
1122 return scm_values (scm_list_2 (tlst, flst));
1123 }
1124 #undef FUNC_NAME
1125
1126 SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
1127 (SCM pred, SCM list),
1128 "Return a list containing all elements from @var{lst} which do\n"
1129 "not satisfy the predicate @var{pred}. The elements in the\n"
1130 "result list have the same order as in @var{lst}. The order in\n"
1131 "which @var{pred} is applied to the list elements is not\n"
1132 "specified.")
1133 #define FUNC_NAME s_scm_srfi1_remove
1134 {
1135 SCM walk;
1136 SCM *prev;
1137 SCM res = SCM_EOL;
1138 SCM_VALIDATE_PROC (SCM_ARG1, pred);
1139 SCM_VALIDATE_LIST (2, list);
1140
1141 for (prev = &res, walk = list;
1142 scm_is_pair (walk);
1143 walk = SCM_CDR (walk))
1144 {
1145 if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
1146 {
1147 *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
1148 prev = SCM_CDRLOC (*prev);
1149 }
1150 }
1151
1152 return res;
1153 }
1154 #undef FUNC_NAME
1155
1156
1157 SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
1158 (SCM pred, SCM list),
1159 "Return a list containing all elements from @var{list} which do\n"
1160 "not satisfy the predicate @var{pred}. The elements in the\n"
1161 "result list have the same order as in @var{list}. The order in\n"
1162 "which @var{pred} is applied to the list elements is not\n"
1163 "specified. @var{list} may be modified to build the return\n"
1164 "list.")
1165 #define FUNC_NAME s_scm_srfi1_remove_x
1166 {
1167 SCM walk;
1168 SCM *prev;
1169 SCM_VALIDATE_PROC (SCM_ARG1, pred);
1170 SCM_VALIDATE_LIST (2, list);
1171
1172 for (prev = &list, walk = list;
1173 scm_is_pair (walk);
1174 walk = SCM_CDR (walk))
1175 {
1176 if (scm_is_false (scm_call_1 (pred, SCM_CAR (walk))))
1177 prev = SCM_CDRLOC (walk);
1178 else
1179 *prev = SCM_CDR (walk);
1180 }
1181
1182 return list;
1183 }
1184 #undef FUNC_NAME
1185
1186 SCM_DEFINE (scm_srfi1_take_right, "take-right", 2, 0, 0,
1187 (SCM lst, SCM n),
1188 "Return a list containing the @var{n} last elements of\n"
1189 "@var{lst}.")
1190 #define FUNC_NAME s_scm_srfi1_take_right
1191 {
1192 SCM tail = scm_list_tail (lst, n);
1193 while (scm_is_pair (tail))
1194 {
1195 lst = SCM_CDR (lst);
1196 tail = SCM_CDR (tail);
1197 }
1198 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
1199 return lst;
1200 }
1201 #undef FUNC_NAME
1202
1203 \f
1204 void
1205 scm_register_srfi_1 (void)
1206 {
1207 scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
1208 "scm_init_srfi_1",
1209 (scm_t_extension_init_func)scm_init_srfi_1, NULL);
1210 }
1211
1212 void
1213 scm_init_srfi_1 (void)
1214 {
1215 SCM the_root_module = scm_lookup_closure_module (SCM_BOOL_F);
1216 #ifndef SCM_MAGIC_SNARFER
1217 #include "libguile/srfi-1.x"
1218 #endif
1219 scm_c_extend_primitive_generic
1220 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "map")),
1221 SCM_VARIABLE_REF (scm_c_lookup ("map")));
1222 scm_c_extend_primitive_generic
1223 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "for-each")),
1224 SCM_VARIABLE_REF (scm_c_lookup ("for-each")));
1225 }
1226
1227 /* End of srfi-1.c. */