(lset-difference!): Rewrite in C.
[bpt/guile.git] / srfi / srfi-1.c
1 /* srfi-1.c --- SRFI-1 procedures for Guile
2 *
3 * Copyright (C) 1995, 1996, 1997, 2000, 2001, 2002, 2003 Free Software
4 * 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
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but 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 02110-1301 USA
19 */
20
21 #include <libguile.h>
22 #include <libguile/lang.h>
23
24 #include "srfi-1.h"
25
26 /* The intent of this file is to gradually replace those Scheme
27 * procedures in srfi-1.scm which extends core primitive procedures,
28 * so that using srfi-1 won't have performance penalties.
29 *
30 * Please feel free to contribute any new replacements!
31 */
32
33 static long
34 srfi1_ilength (SCM sx)
35 {
36 long i = 0;
37 SCM tortoise = sx;
38 SCM hare = sx;
39
40 do {
41 if (SCM_NULL_OR_NIL_P(hare)) return i;
42 if (!scm_is_pair (hare)) return -2;
43 hare = SCM_CDR(hare);
44 i++;
45 if (SCM_NULL_OR_NIL_P(hare)) return i;
46 if (!scm_is_pair (hare)) return -2;
47 hare = SCM_CDR(hare);
48 i++;
49 /* For every two steps the hare takes, the tortoise takes one. */
50 tortoise = SCM_CDR(tortoise);
51 }
52 while (! scm_is_eq (hare, tortoise));
53
54 /* If the tortoise ever catches the hare, then the list must contain
55 a cycle. */
56 return -1;
57 }
58
59 static SCM
60 equal_trampoline (SCM proc, SCM arg1, SCM arg2)
61 {
62 return scm_equal_p (arg1, arg2);
63 }
64
65
66 SCM_DEFINE (scm_srfi1_alist_copy, "alist-copy", 1, 0, 0,
67 (SCM alist),
68 "Return a copy of @var{alist}, copying both the pairs comprising\n"
69 "the list and those making the associations.")
70 #define FUNC_NAME s_scm_srfi1_alist_copy
71 {
72 SCM ret, *p, elem, c;
73
74 /* ret is the list to return. p is where to append to it, initially &ret
75 then SCM_CDRLOC of the last pair. */
76 ret = SCM_EOL;
77 p = &ret;
78
79 for ( ; scm_is_pair (alist); alist = SCM_CDR (alist))
80 {
81 elem = SCM_CAR (alist);
82
83 /* each element of alist must be a pair */
84 SCM_ASSERT_TYPE (scm_is_pair (elem), alist, SCM_ARG1, FUNC_NAME,
85 "association list");
86
87 c = scm_cons (scm_cons (SCM_CAR (elem), SCM_CDR (elem)), SCM_EOL);
88 *p = c;
89 p = SCM_CDRLOC (c);
90 }
91
92 /* alist must be a proper list */
93 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (alist), alist, SCM_ARG1, FUNC_NAME,
94 "association list");
95 return ret;
96 }
97 #undef FUNC_NAME
98
99
100 SCM_DEFINE (scm_srfi1_break, "break", 2, 0, 0,
101 (SCM pred, SCM lst),
102 "Return two values, the longest initial prefix of @var{lst}\n"
103 "whose elements all fail the predicate @var{pred}, and the\n"
104 "remainder of @var{lst}.\n"
105 "\n"
106 "Note that the name @code{break} conflicts with the @code{break}\n"
107 "binding established by @code{while}. Applications wanting to\n"
108 "use @code{break} from within a @code{while} loop will need to\n"
109 "make a new define under a different name.")
110 #define FUNC_NAME s_scm_srfi1_break
111 {
112 scm_t_trampoline_1 pred_tramp;
113 SCM ret, *p;
114
115 pred_tramp = scm_trampoline_1 (pred);
116 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
117
118 ret = SCM_EOL;
119 p = &ret;
120 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
121 {
122 SCM elem = SCM_CAR (lst);
123 if (scm_is_true (pred_tramp (pred, elem)))
124 goto done;
125
126 /* want this elem, tack it onto the end of ret */
127 *p = scm_cons (elem, SCM_EOL);
128 p = SCM_CDRLOC (*p);
129 }
130 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
131
132 done:
133 return scm_values (scm_list_2 (ret, lst));
134 }
135 #undef FUNC_NAME
136
137
138 SCM_DEFINE (scm_srfi1_break_x, "break!", 2, 0, 0,
139 (SCM pred, SCM lst),
140 "Return two values, the longest initial prefix of @var{lst}\n"
141 "whose elements all fail the predicate @var{pred}, and the\n"
142 "remainder of @var{lst}. @var{lst} may be modified to form the\n"
143 "return.")
144 #define FUNC_NAME s_scm_srfi1_break_x
145 {
146 SCM upto, *p;
147 scm_t_trampoline_1 pred_tramp;
148
149 pred_tramp = scm_trampoline_1 (pred);
150 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
151
152 p = &lst;
153 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
154 {
155 if (scm_is_true (pred_tramp (pred, SCM_CAR (upto))))
156 goto done;
157
158 /* want this element */
159 p = SCM_CDRLOC (upto);
160 }
161 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
162
163 done:
164 *p = SCM_EOL;
165 return scm_values (scm_list_2 (lst, upto));
166 }
167 #undef FUNC_NAME
168
169
170 SCM_DEFINE (scm_srfi1_car_plus_cdr, "car+cdr", 1, 0, 0,
171 (SCM pair),
172 "Return two values, the @sc{car} and the @sc{cdr} of @var{pair}.")
173 #define FUNC_NAME s_scm_srfi1_car_plus_cdr
174 {
175 SCM_VALIDATE_CONS (SCM_ARG1, pair);
176 return scm_values (scm_list_2 (SCM_CAR (pair), SCM_CDR (pair)));
177 }
178 #undef FUNC_NAME
179
180
181 SCM_DEFINE (scm_srfi1_concatenate, "concatenate", 1, 0, 0,
182 (SCM lstlst),
183 "Construct a list by appending all lists in @var{lstlst}.\n"
184 "\n"
185 "@code{concatenate} is the same as @code{(apply append\n"
186 "@var{lstlst})}. It exists because some Scheme implementations\n"
187 "have a limit on the number of arguments a function takes, which\n"
188 "the @code{apply} might exceed. In Guile there is no such\n"
189 "limit.")
190 #define FUNC_NAME s_scm_srfi1_concatenate
191 {
192 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
193 return scm_append (lstlst);
194 }
195 #undef FUNC_NAME
196
197
198 SCM_DEFINE (scm_srfi1_concatenate_x, "concatenate!", 1, 0, 0,
199 (SCM lstlst),
200 "Construct a list by appending all lists in @var{lstlst}. Those\n"
201 "lists may be modified to produce the result.\n"
202 "\n"
203 "@code{concatenate!} is the same as @code{(apply append!\n"
204 "@var{lstlst})}. It exists because some Scheme implementations\n"
205 "have a limit on the number of arguments a function takes, which\n"
206 "the @code{apply} might exceed. In Guile there is no such\n"
207 "limit.")
208 #define FUNC_NAME s_scm_srfi1_concatenate
209 {
210 SCM_VALIDATE_LIST (SCM_ARG1, lstlst);
211 return scm_append_x (lstlst);
212 }
213 #undef FUNC_NAME
214
215
216 SCM_DEFINE (scm_srfi1_count, "count", 2, 0, 1,
217 (SCM pred, SCM list1, SCM rest),
218 "Return a count of the number of times @var{pred} returns true\n"
219 "when called on elements from the given lists.\n"
220 "\n"
221 "@var{pred} is called with @var{N} parameters @code{(@var{pred}\n"
222 "@var{elem1} @dots{} @var{elemN})}, each element being from the\n"
223 "corresponding @var{list1} @dots{} @var{lstN}. The first call is\n"
224 "with the first element of each list, the second with the second\n"
225 "element from each, and so on.\n"
226 "\n"
227 "Counting stops when the end of the shortest list is reached.\n"
228 "At least one list must be non-circular.")
229 #define FUNC_NAME s_scm_srfi1_count
230 {
231 long count;
232 SCM lst;
233 int argnum;
234 SCM_VALIDATE_REST_ARGUMENT (rest);
235
236 count = 0;
237
238 if (scm_is_null (rest))
239 {
240 /* one list */
241 scm_t_trampoline_1 pred_tramp;
242 pred_tramp = scm_trampoline_1 (pred);
243 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
244
245 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
246 count += scm_is_true (pred_tramp (pred, SCM_CAR (list1)));
247
248 /* check below that list1 is a proper list, and done */
249 end_list1:
250 lst = list1;
251 argnum = 2;
252 }
253 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
254 {
255 /* two lists */
256 scm_t_trampoline_2 pred_tramp;
257 SCM list2;
258
259 pred_tramp = scm_trampoline_2 (pred);
260 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
261
262 list2 = SCM_CAR (rest);
263 for (;;)
264 {
265 if (! scm_is_pair (list1))
266 goto end_list1;
267 if (! scm_is_pair (list2))
268 {
269 lst = list2;
270 argnum = 3;
271 break;
272 }
273 count += scm_is_true (pred_tramp
274 (pred, SCM_CAR (list1), SCM_CAR (list2)));
275 list1 = SCM_CDR (list1);
276 list2 = SCM_CDR (list2);
277 }
278 }
279 else
280 {
281 /* three or more lists */
282 SCM vec, args, a;
283 size_t len, i;
284
285 /* vec is the list arguments */
286 vec = scm_vector (scm_cons (list1, rest));
287 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
288
289 /* args is the argument list to pass to pred, same length as vec,
290 re-used for each call */
291 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
292
293 for (;;)
294 {
295 /* first elem of each list in vec into args, and step those
296 vec entries onto their next element */
297 for (i = 0, a = args, argnum = 2;
298 i < len;
299 i++, a = SCM_CDR (a), argnum++)
300 {
301 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
302 if (! scm_is_pair (lst))
303 goto check_lst_and_done;
304 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
305 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
306 }
307
308 count += scm_is_true (scm_apply (pred, args, SCM_EOL));
309 }
310 }
311
312 check_lst_and_done:
313 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
314 return scm_from_long (count);
315 }
316 #undef FUNC_NAME
317
318
319 SCM_DEFINE (scm_srfi1_delete, "delete", 2, 1, 0,
320 (SCM x, SCM lst, SCM pred),
321 "Return a list containing the elements of @var{lst} but with\n"
322 "those equal to @var{x} deleted. The returned elements will be\n"
323 "in the same order as they were in @var{lst}.\n"
324 "\n"
325 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
326 "given. An equality call is made just once for each element,\n"
327 "but the order in which the calls are made on the elements is\n"
328 "unspecified.\n"
329 "\n"
330 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
331 "given @var{x} is first. This means for instance elements\n"
332 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
333 "\n"
334 "@var{lst} is not modified, but the returned list might share a\n"
335 "common tail with @var{lst}.")
336 #define FUNC_NAME s_scm_srfi1_delete
337 {
338 scm_t_trampoline_2 equal_p;
339 SCM ret, *p, keeplst;
340
341 if (SCM_UNBNDP (pred))
342 return scm_delete (x, lst);
343
344 equal_p = scm_trampoline_2 (pred);
345 SCM_ASSERT (equal_p, pred, SCM_ARG3, FUNC_NAME);
346
347 /* ret is the return list being constructed. p is where to append to it,
348 initially &ret then SCM_CDRLOC of the last pair. lst progresses as
349 elements are considered.
350
351 Elements to be retained are not immediately copied, instead keeplst is
352 the last pair in lst which is to be retained but not yet copied. When
353 there's no more deletions, *p can be set to keeplst to share the
354 remainder of the original lst. (The entire original lst if there's no
355 deletions at all.) */
356
357 keeplst = lst;
358 ret = SCM_EOL;
359 p = &ret;
360
361 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
362 {
363 if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
364 {
365 /* delete this element, so copy from keeplst (inclusive) to lst
366 (exclusive) onto ret */
367 while (! scm_is_eq (keeplst, lst))
368 {
369 SCM c = scm_cons (SCM_CAR (keeplst), SCM_EOL);
370 *p = c;
371 p = SCM_CDRLOC (c);
372 keeplst = SCM_CDR (keeplst);
373 }
374
375 keeplst = SCM_CDR (lst);
376 }
377 }
378
379 /* final retained elements */
380 *p = keeplst;
381
382 /* demand that lst was a proper list */
383 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
384
385 return ret;
386 }
387 #undef FUNC_NAME
388
389
390 SCM_DEFINE (scm_srfi1_delete_x, "delete!", 2, 1, 0,
391 (SCM x, SCM lst, SCM pred),
392 "Return a list containing the elements of @var{lst} but with\n"
393 "those equal to @var{x} deleted. The returned elements will be\n"
394 "in the same order as they were in @var{lst}.\n"
395 "\n"
396 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
397 "given. An equality call is made just once for each element,\n"
398 "but the order in which the calls are made on the elements is\n"
399 "unspecified.\n"
400 "\n"
401 "The equality calls are always @code{(pred x elem)}, ie.@: the\n"
402 "given @var{x} is first. This means for instance elements\n"
403 "greater than 5 can be deleted with @code{(delete 5 lst <)}.\n"
404 "\n"
405 "@var{lst} may be modified to construct the returned list.")
406 #define FUNC_NAME s_scm_srfi1_delete_x
407 {
408 scm_t_trampoline_2 equal_p;
409 SCM walk;
410 SCM *prev;
411
412 if (SCM_UNBNDP (pred))
413 return scm_delete_x (x, lst);
414
415 equal_p = scm_trampoline_2 (pred);
416 SCM_ASSERT (equal_p, pred, SCM_ARG3, FUNC_NAME);
417
418 for (prev = &lst, walk = lst;
419 scm_is_pair (walk);
420 walk = SCM_CDR (walk))
421 {
422 if (scm_is_true (equal_p (pred, x, SCM_CAR (walk))))
423 *prev = SCM_CDR (walk);
424 else
425 prev = SCM_CDRLOC (walk);
426 }
427
428 /* demand the input was a proper list */
429 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (walk), walk, SCM_ARG2, FUNC_NAME,"list");
430 return lst;
431 }
432 #undef FUNC_NAME
433
434
435 SCM_DEFINE (scm_srfi1_delete_duplicates, "delete-duplicates", 1, 1, 0,
436 (SCM lst, SCM pred),
437 "Return a list containing the elements of @var{lst} but without\n"
438 "duplicates.\n"
439 "\n"
440 "When elements are equal, only the first in @var{lst} is\n"
441 "retained. Equal elements can be anywhere in @var{lst}, they\n"
442 "don't have to be adjacent. The returned list will have the\n"
443 "retained elements in the same order as they were in @var{lst}.\n"
444 "\n"
445 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
446 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
447 "being before @var{y} in @var{lst}. A call is made at most once\n"
448 "for each combination, but the sequence of the calls across the\n"
449 "elements is unspecified.\n"
450 "\n"
451 "@var{lst} is not modified, but the return might share a common\n"
452 "tail with @var{lst}.\n"
453 "\n"
454 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
455 "it must check each element against all those preceding it. For\n"
456 "long lists it is more efficient to sort and then compare only\n"
457 "adjacent elements.")
458 #define FUNC_NAME s_scm_srfi1_delete_duplicates
459 {
460 scm_t_trampoline_2 equal_p;
461 SCM ret, *p, keeplst, item, l;
462
463 /* ret is the new list constructed. p is where to append, initially &ret
464 then SCM_CDRLOC of the last pair. lst is advanced as each element is
465 considered.
466
467 Elements retained are not immediately appended to ret, instead keeplst
468 is the last pair in lst which is to be kept but is not yet copied.
469 Initially this is the first pair of lst, since the first element is
470 always retained.
471
472 *p is kept set to keeplst, so ret (inclusive) to lst (exclusive) is all
473 the elements retained, making the equality search loop easy.
474
475 If an item must be deleted, elements from keeplst (inclusive) to lst
476 (exclusive) must be copied and appended to ret. When there's no more
477 deletions, *p is left set to keeplst, so ret shares structure with the
478 original lst. (ret will be the entire original lst if there are no
479 deletions.) */
480
481 /* skip to end if an empty list (or something invalid) */
482 ret = lst;
483 if (scm_is_pair (lst))
484 {
485 if (SCM_UNBNDP (pred))
486 equal_p = equal_trampoline;
487 else
488 {
489 equal_p = scm_trampoline_2 (pred);
490 SCM_ASSERT (equal_p, pred, SCM_ARG2, FUNC_NAME);
491 }
492
493 keeplst = lst;
494 p = &ret;
495
496 /* loop over lst elements starting from second */
497 for (;;)
498 {
499 lst = SCM_CDR (lst);
500 if (! scm_is_pair (lst))
501 break;
502 item = SCM_CAR (lst);
503
504 /* loop searching ret upto lst */
505 for (l = ret; ! scm_is_eq (l, lst); l = SCM_CDR (l))
506 {
507 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
508 {
509 /* duplicate, don't want this element, so copy keeplst
510 (inclusive) to lst (exclusive) onto ret */
511 while (! scm_is_eq (keeplst, lst))
512 {
513 SCM c = scm_cons (SCM_CAR (keeplst), SCM_EOL);
514 *p = c;
515 p = SCM_CDRLOC (c);
516 keeplst = SCM_CDR (keeplst);
517 }
518
519 keeplst = SCM_CDR (lst); /* elem after the one deleted */
520 *p = keeplst;
521 break;
522 }
523 }
524 }
525 }
526
527 /* demand that lst was a proper list */
528 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
529
530 return ret;
531 }
532 #undef FUNC_NAME
533
534
535 SCM_DEFINE (scm_srfi1_delete_duplicates_x, "delete-duplicates!", 1, 1, 0,
536 (SCM lst, SCM pred),
537 "Return a list containing the elements of @var{lst} but without\n"
538 "duplicates.\n"
539 "\n"
540 "When elements are equal, only the first in @var{lst} is\n"
541 "retained. Equal elements can be anywhere in @var{lst}, they\n"
542 "don't have to be adjacent. The returned list will have the\n"
543 "retained elements in the same order as they were in @var{lst}.\n"
544 "\n"
545 "Equality is determined by @var{pred}, or @code{equal?} if not\n"
546 "given. Calls @code{(pred x y)} are made with element @var{x}\n"
547 "being before @var{y} in @var{lst}. A call is made at most once\n"
548 "for each combination, but the sequence of the calls across the\n"
549 "elements is unspecified.\n"
550 "\n"
551 "@var{lst} may be modified to construct the returned list.\n"
552 "\n"
553 "In the worst case, this is an @math{O(N^2)} algorithm because\n"
554 "it must check each element against all those preceding it. For\n"
555 "long lists it is more efficient to sort and then compare only\n"
556 "adjacent elements.")
557 #define FUNC_NAME s_scm_srfi1_delete_duplicates_x
558 {
559 scm_t_trampoline_2 equal_p;
560 SCM ret, endret, item, l;
561
562 /* ret is the return list, constructed from the pairs in lst. endret is
563 the last pair of ret, initially the first pair. lst is advanced as
564 elements are considered. */
565
566 /* skip to end if an empty list (or something invalid) */
567 ret = lst;
568 if (scm_is_pair (lst))
569 {
570 if (SCM_UNBNDP (pred))
571 equal_p = equal_trampoline;
572 else
573 {
574 equal_p = scm_trampoline_2 (pred);
575 SCM_ASSERT (equal_p, pred, SCM_ARG2, FUNC_NAME);
576 }
577
578 endret = ret;
579
580 /* loop over lst elements starting from second */
581 for (;;)
582 {
583 lst = SCM_CDR (lst);
584 if (! scm_is_pair (lst))
585 break;
586 item = SCM_CAR (lst);
587
588 /* is item equal to any element from ret to endret (inclusive)? */
589 l = ret;
590 for (;;)
591 {
592 if (scm_is_true (equal_p (pred, SCM_CAR (l), item)))
593 break; /* equal, forget this element */
594
595 if (scm_is_eq (l, endret))
596 {
597 /* not equal to any, so append this pair */
598 SCM_SETCDR (endret, lst);
599 endret = lst;
600 break;
601 }
602 l = SCM_CDR (l);
603 }
604 }
605
606 /* terminate, in case last element was deleted */
607 SCM_SETCDR (endret, SCM_EOL);
608 }
609
610 /* demand that lst was a proper list */
611 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG1, FUNC_NAME, "list");
612
613 return ret;
614 }
615 #undef FUNC_NAME
616
617
618 SCM_DEFINE (scm_srfi1_drop_right, "drop-right", 2, 0, 0,
619 (SCM lst, SCM n),
620 "Return a new list containing all except the last @var{n}\n"
621 "elements of @var{lst}.")
622 #define FUNC_NAME s_scm_srfi1_drop_right
623 {
624 SCM tail = scm_list_tail (lst, n);
625 SCM ret = SCM_EOL;
626 SCM *rend = &ret;
627 while (scm_is_pair (tail))
628 {
629 *rend = scm_cons (SCM_CAR (lst), SCM_EOL);
630 rend = SCM_CDRLOC (*rend);
631
632 lst = SCM_CDR (lst);
633 tail = SCM_CDR (tail);
634 }
635 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
636 return ret;
637 }
638 #undef FUNC_NAME
639
640
641 SCM_DEFINE (scm_srfi1_drop_right_x, "drop-right!", 2, 0, 0,
642 (SCM lst, SCM n),
643 "Return the a list containing the @var{n} last elements of\n"
644 "@var{lst}. @var{lst} may be modified to build the return.")
645 #define FUNC_NAME s_scm_srfi1_drop_right_x
646 {
647 SCM tail, *p;
648
649 if (scm_is_eq (n, SCM_INUM0))
650 return lst;
651
652 tail = scm_list_tail (lst, n);
653 p = &lst;
654
655 /* p and tail work along the list, p being the cdrloc of the cell n steps
656 behind tail */
657 for ( ; scm_is_pair (tail); tail = SCM_CDR (tail))
658 p = SCM_CDRLOC (*p);
659
660 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
661
662 *p = SCM_EOL;
663 return lst;
664 }
665 #undef FUNC_NAME
666
667
668 SCM_DEFINE (scm_srfi1_drop_while, "drop-while", 2, 0, 0,
669 (SCM pred, SCM lst),
670 "Drop the longest initial prefix of @var{lst} whose elements all\n"
671 "satisfy the predicate @var{pred}.")
672 #define FUNC_NAME s_scm_srfi1_drop_while
673 {
674 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
675 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
676
677 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
678 if (scm_is_false (pred_tramp (pred, SCM_CAR (lst))))
679 goto done;
680
681 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
682 done:
683 return lst;
684 }
685 #undef FUNC_NAME
686
687
688 SCM_DEFINE (scm_srfi1_eighth, "eighth", 1, 0, 0,
689 (SCM lst),
690 "Return the eighth element of @var{lst}.")
691 #define FUNC_NAME s_scm_srfi1_eighth
692 {
693 return scm_list_ref (lst, SCM_I_MAKINUM (7));
694 }
695 #undef FUNC_NAME
696
697
698 SCM_DEFINE (scm_srfi1_fifth, "fifth", 1, 0, 0,
699 (SCM lst),
700 "Return the fifth element of @var{lst}.")
701 #define FUNC_NAME s_scm_srfi1_fifth
702 {
703 return scm_list_ref (lst, SCM_I_MAKINUM (4));
704 }
705 #undef FUNC_NAME
706
707
708 SCM_DEFINE (scm_srfi1_filter_map, "filter-map", 2, 0, 1,
709 (SCM proc, SCM list1, SCM rest),
710 "Apply @var{proc} to to the elements of @var{list1} @dots{} and\n"
711 "return a list of the results as per SRFI-1 @code{map}, except\n"
712 "that any @code{#f} results are omitted from the list returned.")
713 #define FUNC_NAME s_scm_srfi1_filter_map
714 {
715 SCM ret, *loc, elem, newcell, lst;
716 int argnum;
717
718 SCM_VALIDATE_REST_ARGUMENT (rest);
719
720 ret = SCM_EOL;
721 loc = &ret;
722
723 if (scm_is_null (rest))
724 {
725 /* one list */
726 scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
727 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
728
729 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
730 {
731 elem = proc_tramp (proc, SCM_CAR (list1));
732 if (scm_is_true (elem))
733 {
734 newcell = scm_cons (elem, SCM_EOL);
735 *loc = newcell;
736 loc = SCM_CDRLOC (newcell);
737 }
738 }
739
740 /* check below that list1 is a proper list, and done */
741 end_list1:
742 lst = list1;
743 argnum = 2;
744 }
745 else if (scm_is_null (SCM_CDR (rest)))
746 {
747 /* two lists */
748 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
749 SCM list2 = SCM_CAR (rest);
750 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
751
752 for (;;)
753 {
754 if (! scm_is_pair (list1))
755 goto end_list1;
756 if (! scm_is_pair (list2))
757 {
758 lst = list2;
759 argnum = 3;
760 goto check_lst_and_done;
761 }
762 elem = proc_tramp (proc, SCM_CAR (list1), SCM_CAR (list2));
763 if (scm_is_true (elem))
764 {
765 newcell = scm_cons (elem, SCM_EOL);
766 *loc = newcell;
767 loc = SCM_CDRLOC (newcell);
768 }
769 list1 = SCM_CDR (list1);
770 list2 = SCM_CDR (list2);
771 }
772 }
773 else
774 {
775 /* three or more lists */
776 SCM vec, args, a;
777 size_t len, i;
778
779 /* vec is the list arguments */
780 vec = scm_vector (scm_cons (list1, rest));
781 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
782
783 /* args is the argument list to pass to proc, same length as vec,
784 re-used for each call */
785 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
786
787 for (;;)
788 {
789 /* first elem of each list in vec into args, and step those
790 vec entries onto their next element */
791 for (i = 0, a = args, argnum = 2;
792 i < len;
793 i++, a = SCM_CDR (a), argnum++)
794 {
795 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
796 if (! scm_is_pair (lst))
797 goto check_lst_and_done;
798 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for proc */
799 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
800 }
801
802 elem = scm_apply (proc, args, SCM_EOL);
803 if (scm_is_true (elem))
804 {
805 newcell = scm_cons (elem, SCM_EOL);
806 *loc = newcell;
807 loc = SCM_CDRLOC (newcell);
808 }
809 }
810 }
811
812 check_lst_and_done:
813 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
814 return ret;
815 }
816 #undef FUNC_NAME
817
818
819 SCM_DEFINE (scm_srfi1_find, "find", 2, 0, 0,
820 (SCM pred, SCM lst),
821 "Return the first element of @var{lst} which satisfies the\n"
822 "predicate @var{pred}, or return @code{#f} if no such element is\n"
823 "found.")
824 #define FUNC_NAME s_scm_srfi1_find
825 {
826 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
827 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
828
829 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
830 {
831 SCM elem = SCM_CAR (lst);
832 if (scm_is_true (pred_tramp (pred, elem)))
833 return elem;
834 }
835 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
836
837 return SCM_BOOL_F;
838 }
839 #undef FUNC_NAME
840
841
842 SCM_DEFINE (scm_srfi1_find_tail, "find-tail", 2, 0, 0,
843 (SCM pred, SCM lst),
844 "Return the first pair of @var{lst} whose @sc{car} satisfies the\n"
845 "predicate @var{pred}, or return @code{#f} if no such element is\n"
846 "found.")
847 #define FUNC_NAME s_scm_srfi1_find_tail
848 {
849 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
850 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
851
852 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
853 if (scm_is_true (pred_tramp (pred, SCM_CAR (lst))))
854 return lst;
855 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
856
857 return SCM_BOOL_F;
858 }
859 #undef FUNC_NAME
860
861
862 SCM_DEFINE (scm_srfi1_fold, "fold", 3, 0, 1,
863 (SCM proc, SCM init, SCM list1, SCM rest),
864 "Apply @var{proc} to the elements of @var{lst1} @dots{}\n"
865 "@var{lstN} to build a result, and return that result.\n"
866 "\n"
867 "Each @var{proc} call is @code{(@var{proc} @var{elem1} @dots{}\n"
868 "@var{elemN} @var{previous})}, where @var{elem1} is from\n"
869 "@var{lst1}, through @var{elemN} from @var{lstN}.\n"
870 "@var{previous} is the return from the previous call to\n"
871 "@var{proc}, or the given @var{init} for the first call. If any\n"
872 "list is empty, just @var{init} is returned.\n"
873 "\n"
874 "@code{fold} works through the list elements from first to last.\n"
875 "The following shows a list reversal and the calls it makes,\n"
876 "\n"
877 "@example\n"
878 "(fold cons '() '(1 2 3))\n"
879 "\n"
880 "(cons 1 '())\n"
881 "(cons 2 '(1))\n"
882 "(cons 3 '(2 1)\n"
883 "@result{} (3 2 1)\n"
884 "@end example\n"
885 "\n"
886 "If @var{lst1} through @var{lstN} have different lengths,\n"
887 "@code{fold} stops when the end of the shortest is reached.\n"
888 "Ie.@: elements past the length of the shortest are ignored in\n"
889 "the other @var{lst}s. At least one @var{lst} must be\n"
890 "non-circular.\n"
891 "\n"
892 "The way @code{fold} builds a result from iterating is quite\n"
893 "general, it can do more than other iterations like say\n"
894 "@code{map} or @code{filter}. The following for example removes\n"
895 "adjacent duplicate elements from a list,\n"
896 "\n"
897 "@example\n"
898 "(define (delete-adjacent-duplicates lst)\n"
899 " (fold-right (lambda (elem ret)\n"
900 " (if (equal? elem (first ret))\n"
901 " ret\n"
902 " (cons elem ret)))\n"
903 " (list (last lst))\n"
904 " lst))\n"
905 "(delete-adjacent-duplicates '(1 2 3 3 4 4 4 5))\n"
906 "@result{} (1 2 3 4 5)\n"
907 "@end example\n"
908 "\n"
909 "Clearly the same sort of thing can be done with a\n"
910 "@code{for-each} and a variable in which to build the result,\n"
911 "but a self-contained @var{proc} can be re-used in multiple\n"
912 "contexts, where a @code{for-each} would have to be written out\n"
913 "each time.")
914 #define FUNC_NAME s_scm_srfi1_fold
915 {
916 SCM lst;
917 int argnum;
918 SCM_VALIDATE_REST_ARGUMENT (rest);
919
920 if (scm_is_null (rest))
921 {
922 /* one list */
923 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
924 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
925
926 for ( ; scm_is_pair (list1); list1 = SCM_CDR (list1))
927 init = proc_tramp (proc, SCM_CAR (list1), init);
928
929 /* check below that list1 is a proper list, and done */
930 lst = list1;
931 argnum = 2;
932 }
933 else
934 {
935 /* two or more lists */
936 SCM vec, args, a;
937 size_t len, i;
938
939 /* vec is the list arguments */
940 vec = scm_vector (scm_cons (list1, rest));
941 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
942
943 /* args is the argument list to pass to proc, same length as vec,
944 re-used for each call */
945 args = scm_make_list (SCM_I_MAKINUM (len+1), SCM_UNDEFINED);
946
947 for (;;)
948 {
949 /* first elem of each list in vec into args, and step those
950 vec entries onto their next element */
951 for (i = 0, a = args, argnum = 2;
952 i < len;
953 i++, a = SCM_CDR (a), argnum++)
954 {
955 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
956 if (! scm_is_pair (lst))
957 goto check_lst_and_done;
958 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for proc */
959 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
960 }
961 SCM_SETCAR (a, init);
962
963 init = scm_apply (proc, args, SCM_EOL);
964 }
965 }
966
967 check_lst_and_done:
968 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
969 return init;
970 }
971 #undef FUNC_NAME
972
973
974 SCM_DEFINE (scm_srfi1_last, "last", 1, 0, 0,
975 (SCM lst),
976 "Like @code{cons}, but with interchanged arguments. Useful\n"
977 "mostly when passed to higher-order procedures.")
978 #define FUNC_NAME s_scm_srfi1_last
979 {
980 SCM pair = scm_last_pair (lst);
981 /* scm_last_pair returns SCM_EOL for an empty list */
982 SCM_VALIDATE_CONS (SCM_ARG1, pair);
983 return SCM_CAR (pair);
984 }
985 #undef FUNC_NAME
986
987
988 SCM_DEFINE (scm_srfi1_length_plus, "length+", 1, 0, 0,
989 (SCM lst),
990 "Return the length of @var{lst}, or @code{#f} if @var{lst} is\n"
991 "circular.")
992 #define FUNC_NAME s_scm_srfi1_length_plus
993 {
994 long len = scm_ilength (lst);
995 return (len >= 0 ? SCM_I_MAKINUM (len) : SCM_BOOL_F);
996 }
997 #undef FUNC_NAME
998
999
1000 SCM_DEFINE (scm_srfi1_list_index, "list-index", 2, 0, 1,
1001 (SCM pred, SCM list1, SCM rest),
1002 "Return the index of the first set of elements, one from each of\n"
1003 "@var{lst1}@dots{}@var{lstN}, which satisfies @var{pred}.\n"
1004 "\n"
1005 "@var{pred} is called as @code{(@var{pred} elem1 @dots{}\n"
1006 "elemN)}. Searching stops when the end of the shortest\n"
1007 "@var{lst} is reached. The return index starts from 0 for the\n"
1008 "first set of elements. If no set of elements pass then the\n"
1009 "return is @code{#f}.\n"
1010 "\n"
1011 "@example\n"
1012 "(list-index odd? '(2 4 6 9)) @result{} 3\n"
1013 "(list-index = '(1 2 3) '(3 1 2)) @result{} #f\n"
1014 "@end example")
1015 #define FUNC_NAME s_scm_srfi1_list_index
1016 {
1017 long n = 0;
1018 SCM lst;
1019 int argnum;
1020 SCM_VALIDATE_REST_ARGUMENT (rest);
1021
1022 if (scm_is_null (rest))
1023 {
1024 /* one list */
1025 scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (pred);
1026 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1027
1028 for ( ; scm_is_pair (list1); n++, list1 = SCM_CDR (list1))
1029 if (scm_is_true (pred_tramp (pred, SCM_CAR (list1))))
1030 return SCM_I_MAKINUM (n);
1031
1032 /* not found, check below that list1 is a proper list */
1033 end_list1:
1034 lst = list1;
1035 argnum = 2;
1036 }
1037 else if (scm_is_pair (rest) && scm_is_null (SCM_CDR (rest)))
1038 {
1039 /* two lists */
1040 SCM list2 = SCM_CAR (rest);
1041 scm_t_trampoline_2 pred_tramp = scm_trampoline_2 (pred);
1042 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1043
1044 for ( ; ; n++)
1045 {
1046 if (! scm_is_pair (list1))
1047 goto end_list1;
1048 if (! scm_is_pair (list2))
1049 {
1050 lst = list2;
1051 argnum = 3;
1052 break;
1053 }
1054 if (scm_is_true (pred_tramp (pred,
1055 SCM_CAR (list1), SCM_CAR (list2))))
1056 return SCM_I_MAKINUM (n);
1057
1058 list1 = SCM_CDR (list1);
1059 list2 = SCM_CDR (list2);
1060 }
1061 }
1062 else
1063 {
1064 /* three or more lists */
1065 SCM vec, args, a;
1066 size_t len, i;
1067
1068 /* vec is the list arguments */
1069 vec = scm_vector (scm_cons (list1, rest));
1070 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
1071
1072 /* args is the argument list to pass to pred, same length as vec,
1073 re-used for each call */
1074 args = scm_make_list (SCM_I_MAKINUM (len), SCM_UNDEFINED);
1075
1076 for ( ; ; n++)
1077 {
1078 /* first elem of each list in vec into args, and step those
1079 vec entries onto their next element */
1080 for (i = 0, a = args, argnum = 2;
1081 i < len;
1082 i++, a = SCM_CDR (a), argnum++)
1083 {
1084 lst = SCM_SIMPLE_VECTOR_REF (vec, i); /* list argument */
1085 if (! scm_is_pair (lst))
1086 goto not_found_check_lst;
1087 SCM_SETCAR (a, SCM_CAR (lst)); /* arg for pred */
1088 SCM_SIMPLE_VECTOR_SET (vec, i, SCM_CDR (lst)); /* rest of lst */
1089 }
1090
1091 if (scm_is_true (scm_apply (pred, args, SCM_EOL)))
1092 return SCM_I_MAKINUM (n);
1093 }
1094 }
1095
1096 not_found_check_lst:
1097 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, argnum, FUNC_NAME, "list");
1098 return SCM_BOOL_F;
1099 }
1100 #undef FUNC_NAME
1101
1102
1103 /* This routine differs from the core list-copy in allowing improper lists.
1104 Maybe the core could allow them similarly. */
1105
1106 SCM_DEFINE (scm_srfi1_list_copy, "list-copy", 1, 0, 0,
1107 (SCM lst),
1108 "Return a copy of the given list @var{lst}.\n"
1109 "\n"
1110 "@var{lst} can be a proper or improper list. And if @var{lst}\n"
1111 "is not a pair then it's treated as the final tail of an\n"
1112 "improper list and simply returned.")
1113 #define FUNC_NAME s_scm_srfi1_list_copy
1114 {
1115 SCM newlst;
1116 SCM * fill_here;
1117 SCM from_here;
1118
1119 newlst = lst;
1120 fill_here = &newlst;
1121 from_here = lst;
1122
1123 while (scm_is_pair (from_here))
1124 {
1125 SCM c;
1126 c = scm_cons (SCM_CAR (from_here), SCM_CDR (from_here));
1127 *fill_here = c;
1128 fill_here = SCM_CDRLOC (c);
1129 from_here = SCM_CDR (from_here);
1130 }
1131 return newlst;
1132 }
1133 #undef FUNC_NAME
1134
1135
1136 SCM_DEFINE (scm_srfi1_list_tabulate, "list-tabulate", 2, 0, 0,
1137 (SCM n, SCM proc),
1138 "Return an @var{n}-element list, where each list element is\n"
1139 "produced by applying the procedure @var{init-proc} to the\n"
1140 "corresponding list index. The order in which @var{init-proc}\n"
1141 "is applied to the indices is not specified.")
1142 #define FUNC_NAME s_scm_srfi1_list_tabulate
1143 {
1144 long i, nn;
1145 scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
1146 SCM ret = SCM_EOL;
1147
1148 nn = scm_to_signed_integer (n, 0, LONG_MAX);
1149 SCM_ASSERT (proc_tramp, proc, SCM_ARG2, FUNC_NAME);
1150
1151 for (i = nn-1; i >= 0; i--)
1152 ret = scm_cons (proc_tramp (proc, scm_from_long (i)), ret);
1153
1154 return ret;
1155 }
1156 #undef FUNC_NAME
1157
1158
1159 SCM_DEFINE (scm_srfi1_lset_adjoin, "lset-adjoin", 2, 0, 1,
1160 (SCM equal, SCM lst, SCM rest),
1161 "Add to @var{list} any of the given @var{elem}s not already in\n"
1162 "the list. @var{elem}s are @code{cons}ed onto the start of\n"
1163 "@var{list} (so the return shares a common tail with\n"
1164 "@var{list}), but the order they're added is unspecified.\n"
1165 "\n"
1166 "The given @var{=} procedure is used for comparing elements,\n"
1167 "called as @code{(@var{=} listelem elem)}, ie.@: the second\n"
1168 "argument is one of the given @var{elem} parameters.\n"
1169 "\n"
1170 "@example\n"
1171 "(lset-adjoin eqv? '(1 2 3) 4 1 5) @result{} (5 4 1 2 3)\n"
1172 "@end example")
1173 #define FUNC_NAME s_scm_srfi1_lset_adjoin
1174 {
1175 scm_t_trampoline_2 equal_tramp;
1176 SCM l, elem;
1177
1178 equal_tramp = scm_trampoline_2 (equal);
1179 SCM_ASSERT (equal_tramp, equal, SCM_ARG1, FUNC_NAME);
1180 SCM_VALIDATE_REST_ARGUMENT (rest);
1181
1182 /* It's not clear if duplicates among the `rest' elements are meant to be
1183 cast out. The spec says `=' is called as (= list-elem rest-elem),
1184 suggesting perhaps not, but the reference implementation shows the
1185 "list" at each stage as including those "rest" elements already added.
1186 The latter corresponds to what's described for lset-union, so that's
1187 what's done here. */
1188
1189 for ( ; scm_is_pair (rest); rest = SCM_CDR (rest))
1190 {
1191 elem = SCM_CAR (rest);
1192
1193 for (l = lst; scm_is_pair (l); l = SCM_CDR (l))
1194 if (scm_is_true (equal_tramp (equal, SCM_CAR (l), elem)))
1195 goto next_elem; /* elem already in lst, don't add */
1196
1197 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(l), lst, SCM_ARG2, FUNC_NAME, "list");
1198
1199 /* elem is not equal to anything already in lst, add it */
1200 lst = scm_cons (elem, lst);
1201
1202 next_elem:
1203 ;
1204 }
1205
1206 return lst;
1207 }
1208 #undef FUNC_NAME
1209
1210
1211 SCM_DEFINE (scm_srfi1_lset_difference_x, "lset-difference!", 2, 0, 1,
1212 (SCM equal, SCM lst, SCM rest),
1213 "Return @var{lst} with any elements in the lists in @var{rest}\n"
1214 "removed (ie.@: subtracted). For only one @var{lst} argument,\n"
1215 "just that list is returned.\n"
1216 "\n"
1217 "The given @var{equal} procedure is used for comparing elements,\n"
1218 "called as @code{(@var{equal} elem1 elemN)}. The first argument\n"
1219 "is from @var{lst} and the second from one of the subsequent\n"
1220 "lists. But exactly which calls are made and in what order is\n"
1221 "unspecified.\n"
1222 "\n"
1223 "@example\n"
1224 "(lset-difference! eqv? (list 'x 'y)) @result{} (x y)\n"
1225 "(lset-difference! eqv? (list 1 2 3) '(3 1)) @result{} (2)\n"
1226 "(lset-difference! eqv? (list 1 2 3) '(3) '(2)) @result{} (1)\n"
1227 "@end example\n"
1228 "\n"
1229 "@code{lset-difference!} may modify @var{lst} to form its\n"
1230 "result.")
1231 #define FUNC_NAME s_scm_srfi1_lset_difference_x
1232 {
1233 scm_t_trampoline_2 equal_tramp = scm_trampoline_2 (equal);
1234 SCM ret, *pos, elem, r, b;
1235 int argnum;
1236
1237 SCM_ASSERT (equal_tramp, equal, SCM_ARG1, FUNC_NAME);
1238 SCM_VALIDATE_REST_ARGUMENT (rest);
1239
1240 ret = SCM_EOL;
1241 pos = &ret;
1242 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1243 {
1244 elem = SCM_CAR (lst);
1245
1246 for (r = rest, argnum = SCM_ARG3;
1247 scm_is_pair (r);
1248 r = SCM_CDR (r), argnum++)
1249 {
1250 for (b = SCM_CAR (r); scm_is_pair (b); b = SCM_CDR (b))
1251 if (scm_is_true (equal_tramp (equal, elem, SCM_CAR (b))))
1252 goto next_elem; /* equal to elem, so drop that elem */
1253
1254 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (b), b, argnum, FUNC_NAME,"list");
1255 }
1256
1257 /* elem not equal to anything in later lists, so keep it */
1258 *pos = lst;
1259 pos = SCM_CDRLOC (lst);
1260
1261 next_elem:
1262 ;
1263 }
1264 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1265
1266 *pos = SCM_EOL;
1267 return ret;
1268 }
1269 #undef FUNC_NAME
1270
1271
1272 /* Typechecking for multi-argument MAP and FOR-EACH.
1273
1274 Verify that each element of the vector ARGV, except for the first,
1275 is a list and return minimum length. Attribute errors to WHO,
1276 and claim that the i'th element of ARGV is WHO's i+2'th argument. */
1277 static inline int
1278 check_map_args (SCM argv,
1279 long len,
1280 SCM gf,
1281 SCM proc,
1282 SCM args,
1283 const char *who)
1284 {
1285 long i;
1286 SCM elt;
1287
1288 for (i = SCM_SIMPLE_VECTOR_LENGTH (argv) - 1; i >= 1; i--)
1289 {
1290 long elt_len;
1291 elt = SCM_SIMPLE_VECTOR_REF (argv, i);
1292
1293 if (!(scm_is_null (elt) || scm_is_pair (elt)))
1294 goto check_map_error;
1295
1296 elt_len = srfi1_ilength (elt);
1297 if (elt_len < -1)
1298 goto check_map_error;
1299
1300 if (len < 0 || (elt_len >= 0 && elt_len < len))
1301 len = elt_len;
1302 }
1303
1304 if (len < 0)
1305 {
1306 /* i == 0 */
1307 elt = SCM_EOL;
1308 check_map_error:
1309 if (gf)
1310 scm_apply_generic (gf, scm_cons (proc, args));
1311 else
1312 scm_wrong_type_arg (who, i + 2, elt);
1313 }
1314
1315 scm_remember_upto_here_1 (argv);
1316 return len;
1317 }
1318
1319
1320 SCM_GPROC (s_srfi1_map, "map", 2, 0, 1, scm_srfi1_map, g_srfi1_map);
1321
1322 /* Note: Currently, scm_srfi1_map applies PROC to the argument list(s)
1323 sequentially, starting with the first element(s). This is used in
1324 the Scheme procedure `map-in-order', which guarantees sequential
1325 behaviour, is implemented using scm_map. If the behaviour changes,
1326 we need to update `map-in-order'.
1327 */
1328
1329 SCM
1330 scm_srfi1_map (SCM proc, SCM arg1, SCM args)
1331 #define FUNC_NAME s_srfi1_map
1332 {
1333 long i, len;
1334 SCM res = SCM_EOL;
1335 SCM *pres = &res;
1336
1337 len = srfi1_ilength (arg1);
1338 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
1339 g_srfi1_map,
1340 scm_cons2 (proc, arg1, args), SCM_ARG2, s_srfi1_map);
1341 SCM_VALIDATE_REST_ARGUMENT (args);
1342 if (scm_is_null (args))
1343 {
1344 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
1345 SCM_GASSERT2 (call, g_srfi1_map, proc, arg1, SCM_ARG1, s_srfi1_map);
1346 SCM_GASSERT2 (len >= 0, g_srfi1_map, proc, arg1, SCM_ARG2, s_srfi1_map);
1347 while (SCM_NIMP (arg1))
1348 {
1349 *pres = scm_list_1 (call (proc, SCM_CAR (arg1)));
1350 pres = SCM_CDRLOC (*pres);
1351 arg1 = SCM_CDR (arg1);
1352 }
1353 return res;
1354 }
1355 if (scm_is_null (SCM_CDR (args)))
1356 {
1357 SCM arg2 = SCM_CAR (args);
1358 int len2 = srfi1_ilength (arg2);
1359 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
1360 SCM_GASSERTn (call, g_srfi1_map,
1361 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_map);
1362 if (len < 0 || (len2 >= 0 && len2 < len))
1363 len = len2;
1364 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
1365 && len >= 0 && len2 >= -1,
1366 g_srfi1_map,
1367 scm_cons2 (proc, arg1, args),
1368 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
1369 s_srfi1_map);
1370 while (len > 0)
1371 {
1372 *pres = scm_list_1 (call (proc, SCM_CAR (arg1), SCM_CAR (arg2)));
1373 pres = SCM_CDRLOC (*pres);
1374 arg1 = SCM_CDR (arg1);
1375 arg2 = SCM_CDR (arg2);
1376 --len;
1377 }
1378 return res;
1379 }
1380 args = scm_vector (arg1 = scm_cons (arg1, args));
1381 len = check_map_args (args, len, g_srfi1_map, proc, arg1, s_srfi1_map);
1382 while (len > 0)
1383 {
1384 arg1 = SCM_EOL;
1385 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
1386 {
1387 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
1388 arg1 = scm_cons (SCM_CAR (elt), arg1);
1389 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
1390 }
1391 *pres = scm_list_1 (scm_apply (proc, arg1, SCM_EOL));
1392 pres = SCM_CDRLOC (*pres);
1393 --len;
1394 }
1395 return res;
1396 }
1397 #undef FUNC_NAME
1398
1399 SCM_REGISTER_PROC (s_srfi1_map_in_order, "map-in-order", 2, 0, 1, scm_srfi1_map);
1400
1401 SCM_GPROC (s_srfi1_for_each, "for-each", 2, 0, 1, scm_srfi1_for_each, g_srfi1_for_each);
1402
1403 SCM
1404 scm_srfi1_for_each (SCM proc, SCM arg1, SCM args)
1405 #define FUNC_NAME s_srfi1_for_each
1406 {
1407 long i, len;
1408 len = srfi1_ilength (arg1);
1409 SCM_GASSERTn ((scm_is_null (arg1) || scm_is_pair (arg1)) && len >= -1,
1410 g_srfi1_for_each, scm_cons2 (proc, arg1, args),
1411 SCM_ARG2, s_srfi1_for_each);
1412 SCM_VALIDATE_REST_ARGUMENT (args);
1413 if (scm_is_null (args))
1414 {
1415 scm_t_trampoline_1 call = scm_trampoline_1 (proc);
1416 SCM_GASSERT2 (call, g_srfi1_for_each, proc, arg1,
1417 SCM_ARG1, s_srfi1_for_each);
1418 SCM_GASSERT2 (len >= 0, g_srfi1_for_each, proc, arg1,
1419 SCM_ARG2, s_srfi1_map);
1420 while (SCM_NIMP (arg1))
1421 {
1422 call (proc, SCM_CAR (arg1));
1423 arg1 = SCM_CDR (arg1);
1424 }
1425 return SCM_UNSPECIFIED;
1426 }
1427 if (scm_is_null (SCM_CDR (args)))
1428 {
1429 SCM arg2 = SCM_CAR (args);
1430 int len2 = srfi1_ilength (arg2);
1431 scm_t_trampoline_2 call = scm_trampoline_2 (proc);
1432 SCM_GASSERTn (call, g_srfi1_for_each,
1433 scm_cons2 (proc, arg1, args), SCM_ARG1, s_srfi1_for_each);
1434 if (len < 0 || (len2 >= 0 && len2 < len))
1435 len = len2;
1436 SCM_GASSERTn ((scm_is_null (arg2) || scm_is_pair (arg2))
1437 && len >= 0 && len2 >= -1,
1438 g_srfi1_for_each,
1439 scm_cons2 (proc, arg1, args),
1440 len2 >= 0 ? SCM_ARG2 : SCM_ARG3,
1441 s_srfi1_for_each);
1442 while (len > 0)
1443 {
1444 call (proc, SCM_CAR (arg1), SCM_CAR (arg2));
1445 arg1 = SCM_CDR (arg1);
1446 arg2 = SCM_CDR (arg2);
1447 --len;
1448 }
1449 return SCM_UNSPECIFIED;
1450 }
1451 args = scm_vector (arg1 = scm_cons (arg1, args));
1452 len = check_map_args (args, len, g_srfi1_for_each, proc, arg1,
1453 s_srfi1_for_each);
1454 while (len > 0)
1455 {
1456 arg1 = SCM_EOL;
1457 for (i = SCM_SIMPLE_VECTOR_LENGTH (args) - 1; i >= 0; i--)
1458 {
1459 SCM elt = SCM_SIMPLE_VECTOR_REF (args, i);
1460 arg1 = scm_cons (SCM_CAR (elt), arg1);
1461 SCM_SIMPLE_VECTOR_SET (args, i, SCM_CDR (elt));
1462 }
1463 scm_apply (proc, arg1, SCM_EOL);
1464 --len;
1465 }
1466 return SCM_UNSPECIFIED;
1467 }
1468 #undef FUNC_NAME
1469
1470
1471 SCM_DEFINE (scm_srfi1_member, "member", 2, 1, 0,
1472 (SCM x, SCM lst, SCM pred),
1473 "Return the first sublist of @var{lst} whose @sc{car} is equal\n"
1474 "to @var{x}. If @var{x} does not appear in @var{lst}, return\n"
1475 "@code{#f}.\n"
1476 "\n"
1477 "Equality is determined by @code{equal?}, or by the equality\n"
1478 "predicate @var{=} if given. @var{=} is called @code{(= @var{x}\n"
1479 "elem)}, ie.@: with the given @var{x} first, so for example to\n"
1480 "find the first element greater than 5,\n"
1481 "\n"
1482 "@example\n"
1483 "(member 5 '(3 5 1 7 2 9) <) @result{} (7 2 9)\n"
1484 "@end example\n"
1485 "\n"
1486 "This version of @code{member} extends the core @code{member} by\n"
1487 "accepting an equality predicate.")
1488 #define FUNC_NAME s_scm_srfi1_member
1489 {
1490 scm_t_trampoline_2 equal_p;
1491 SCM_VALIDATE_LIST (2, lst);
1492 if (SCM_UNBNDP (pred))
1493 equal_p = equal_trampoline;
1494 else
1495 {
1496 equal_p = scm_trampoline_2 (pred);
1497 SCM_ASSERT (equal_p, pred, 3, FUNC_NAME);
1498 }
1499 for (; !SCM_NULL_OR_NIL_P (lst); lst = SCM_CDR (lst))
1500 {
1501 if (scm_is_true (equal_p (pred, x, SCM_CAR (lst))))
1502 return lst;
1503 }
1504 return SCM_BOOL_F;
1505 }
1506 #undef FUNC_NAME
1507
1508 SCM_DEFINE (scm_srfi1_assoc, "assoc", 2, 1, 0,
1509 (SCM key, SCM alist, SCM pred),
1510 "Behaves like @code{assq} but uses third argument @var{pred?}\n"
1511 "for key comparison. If @var{pred?} is not supplied,\n"
1512 "@code{equal?} is used. (Extended from R5RS.)\n")
1513 #define FUNC_NAME s_scm_srfi1_assoc
1514 {
1515 SCM ls = alist;
1516 scm_t_trampoline_2 equal_p;
1517 if (SCM_UNBNDP (pred))
1518 equal_p = equal_trampoline;
1519 else
1520 {
1521 equal_p = scm_trampoline_2 (pred);
1522 SCM_ASSERT (equal_p, pred, 3, FUNC_NAME);
1523 }
1524 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
1525 {
1526 SCM tmp = SCM_CAR (ls);
1527 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1528 "association list");
1529 if (scm_is_true (equal_p (pred, SCM_CAR (tmp), key)))
1530 return tmp;
1531 }
1532 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1533 "association list");
1534 return SCM_BOOL_F;
1535 }
1536 #undef FUNC_NAME
1537
1538
1539 SCM_DEFINE (scm_srfi1_ninth, "ninth", 1, 0, 0,
1540 (SCM lst),
1541 "Return the ninth element of @var{lst}.")
1542 #define FUNC_NAME s_scm_srfi1_ninth
1543 {
1544 return scm_list_ref (lst, scm_from_int (8));
1545 }
1546 #undef FUNC_NAME
1547
1548
1549 SCM_DEFINE (scm_srfi1_not_pair_p, "not-pair?", 1, 0, 0,
1550 (SCM obj),
1551 "Return @code{#t} is @var{obj} is not a pair, @code{#f}\n"
1552 "otherwise.\n"
1553 "\n"
1554 "This is shorthand notation @code{(not (pair? @var{obj}))} and\n"
1555 "is supposed to be used for end-of-list checking in contexts\n"
1556 "where dotted lists are allowed.")
1557 #define FUNC_NAME s_scm_srfi1_not_pair_p
1558 {
1559 return scm_from_bool (! scm_is_pair (obj));
1560 }
1561 #undef FUNC_NAME
1562
1563
1564 SCM_DEFINE (scm_srfi1_partition, "partition", 2, 0, 0,
1565 (SCM pred, SCM list),
1566 "Partition the elements of @var{list} with predicate @var{pred}.\n"
1567 "Return two values: the list of elements satifying @var{pred} and\n"
1568 "the list of elements @emph{not} satisfying @var{pred}. The order\n"
1569 "of the output lists follows the order of @var{list}. @var{list}\n"
1570 "is not mutated. One of the output lists may share memory with @var{list}.\n")
1571 #define FUNC_NAME s_scm_srfi1_partition
1572 {
1573 /* In this implementation, the output lists don't share memory with
1574 list, because it's probably not worth the effort. */
1575 scm_t_trampoline_1 call = scm_trampoline_1(pred);
1576 SCM kept = scm_cons(SCM_EOL, SCM_EOL);
1577 SCM kept_tail = kept;
1578 SCM dropped = scm_cons(SCM_EOL, SCM_EOL);
1579 SCM dropped_tail = dropped;
1580
1581 SCM_ASSERT(call, pred, 2, FUNC_NAME);
1582
1583 for (; !SCM_NULL_OR_NIL_P (list); list = SCM_CDR(list)) {
1584 SCM elt = SCM_CAR(list);
1585 SCM new_tail = scm_cons(SCM_CAR(list), SCM_EOL);
1586 if (scm_is_true (call (pred, elt))) {
1587 SCM_SETCDR(kept_tail, new_tail);
1588 kept_tail = new_tail;
1589 }
1590 else {
1591 SCM_SETCDR(dropped_tail, new_tail);
1592 dropped_tail = new_tail;
1593 }
1594 }
1595 /* re-use the initial conses for the values list */
1596 SCM_SETCAR(kept, SCM_CDR(kept));
1597 SCM_SETCDR(kept, dropped);
1598 SCM_SETCAR(dropped, SCM_CDR(dropped));
1599 SCM_SETCDR(dropped, SCM_EOL);
1600 return scm_values(kept);
1601 }
1602 #undef FUNC_NAME
1603
1604
1605 SCM_DEFINE (scm_srfi1_partition_x, "partition!", 2, 0, 0,
1606 (SCM pred, SCM lst),
1607 "Split @var{lst} into those elements which do and don't satisfy\n"
1608 "the predicate @var{pred}.\n"
1609 "\n"
1610 "The return is two values (@pxref{Multiple Values}), the first\n"
1611 "being a list of all elements from @var{lst} which satisfy\n"
1612 "@var{pred}, the second a list of those which do not.\n"
1613 "\n"
1614 "The elements in the result lists are in the same order as in\n"
1615 "@var{lst} but the order in which the calls @code{(@var{pred}\n"
1616 "elem)} are made on the list elements is unspecified.\n"
1617 "\n"
1618 "@var{lst} may be modified to construct the return lists.")
1619 #define FUNC_NAME s_scm_srfi1_partition_x
1620 {
1621 SCM tlst, flst, *tp, *fp;
1622 scm_t_trampoline_1 pred_tramp;
1623
1624 pred_tramp = scm_trampoline_1 (pred);
1625 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1626
1627 /* tlst and flst are the lists of true and false elements. tp and fp are
1628 where to store to append to them, initially &tlst and &flst, then
1629 SCM_CDRLOC of the last pair in the respective lists. */
1630
1631 tlst = SCM_EOL;
1632 flst = SCM_EOL;
1633 tp = &tlst;
1634 fp = &flst;
1635
1636 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1637 {
1638 if (scm_is_true (pred_tramp (pred, SCM_CAR (lst))))
1639 {
1640 *tp = lst;
1641 tp = SCM_CDRLOC (lst);
1642 }
1643 else
1644 {
1645 *fp = lst;
1646 fp = SCM_CDRLOC (lst);
1647 }
1648 }
1649
1650 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1651
1652 /* terminate whichever didn't get the last element(s) */
1653 *tp = SCM_EOL;
1654 *fp = SCM_EOL;
1655
1656 return scm_values (scm_list_2 (tlst, flst));
1657 }
1658 #undef FUNC_NAME
1659
1660
1661 SCM_DEFINE (scm_srfi1_reduce, "reduce", 3, 0, 0,
1662 (SCM proc, SCM def, SCM lst),
1663 "@code{reduce} is a variant of @code{fold}, where the first call\n"
1664 "to @var{proc} is on two elements from @var{lst}, rather than\n"
1665 "one element and a given initial value.\n"
1666 "\n"
1667 "If @var{lst} is empty, @code{reduce} returns @var{def} (this is\n"
1668 "the only use for @var{def}). If @var{lst} has just one element\n"
1669 "then that's the return value. Otherwise @var{proc} is called\n"
1670 "on the elements of @var{lst}.\n"
1671 "\n"
1672 "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
1673 "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
1674 "second and subsequent elements of @var{lst}), and\n"
1675 "@var{previous} is the return from the previous call to\n"
1676 "@var{proc}. The first element of @var{lst} is the\n"
1677 "@var{previous} for the first call to @var{proc}.\n"
1678 "\n"
1679 "For example, the following adds a list of numbers, the calls\n"
1680 "made to @code{+} are shown. (Of course @code{+} accepts\n"
1681 "multiple arguments and can add a list directly, with\n"
1682 "@code{apply}.)\n"
1683 "\n"
1684 "@example\n"
1685 "(reduce + 0 '(5 6 7)) @result{} 18\n"
1686 "\n"
1687 "(+ 6 5) @result{} 11\n"
1688 "(+ 7 11) @result{} 18\n"
1689 "@end example\n"
1690 "\n"
1691 "@code{reduce} can be used instead of @code{fold} where the\n"
1692 "@var{init} value is an ``identity'', meaning a value which\n"
1693 "under @var{proc} doesn't change the result, in this case 0 is\n"
1694 "an identity since @code{(+ 5 0)} is just 5. @code{reduce}\n"
1695 "avoids that unnecessary call.")
1696 #define FUNC_NAME s_scm_srfi1_reduce
1697 {
1698 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
1699 SCM ret;
1700
1701 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
1702
1703 ret = def; /* if lst is empty */
1704 if (scm_is_pair (lst))
1705 {
1706 ret = SCM_CAR (lst); /* if lst has one element */
1707
1708 for (lst = SCM_CDR (lst); scm_is_pair (lst); lst = SCM_CDR (lst))
1709 ret = proc_tramp (proc, SCM_CAR (lst), ret);
1710 }
1711
1712 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG3, FUNC_NAME, "list");
1713 return ret;
1714 }
1715 #undef FUNC_NAME
1716
1717
1718 SCM_DEFINE (scm_srfi1_reduce_right, "reduce-right", 3, 0, 0,
1719 (SCM proc, SCM def, SCM lst),
1720 "@code{reduce-right} is a variant of @code{fold-right}, where\n"
1721 "the first call to @var{proc} is on two elements from @var{lst},\n"
1722 "rather than one element and a given initial value.\n"
1723 "\n"
1724 "If @var{lst} is empty, @code{reduce-right} returns @var{def}\n"
1725 "(this is the only use for @var{def}). If @var{lst} has just\n"
1726 "one element then that's the return value. Otherwise @var{proc}\n"
1727 "is called on the elements of @var{lst}.\n"
1728 "\n"
1729 "Each @var{proc} call is @code{(@var{proc} @var{elem}\n"
1730 "@var{previous})}, where @var{elem} is from @var{lst} (the\n"
1731 "second last and then working back to the first element of\n"
1732 "@var{lst}), and @var{previous} is the return from the previous\n"
1733 "call to @var{proc}. The last element of @var{lst} is the\n"
1734 "@var{previous} for the first call to @var{proc}.\n"
1735 "\n"
1736 "For example, the following adds a list of numbers, the calls\n"
1737 "made to @code{+} are shown. (Of course @code{+} accepts\n"
1738 "multiple arguments and can add a list directly, with\n"
1739 "@code{apply}.)\n"
1740 "\n"
1741 "@example\n"
1742 "(reduce-right + 0 '(5 6 7)) @result{} 18\n"
1743 "\n"
1744 "(+ 6 7) @result{} 13\n"
1745 "(+ 5 13) @result{} 18\n"
1746 "@end example\n"
1747 "\n"
1748 "@code{reduce-right} can be used instead of @code{fold-right}\n"
1749 "where the @var{init} value is an ``identity'', meaning a value\n"
1750 "which under @var{proc} doesn't change the result, in this case\n"
1751 "0 is an identity since @code{(+ 7 0)} is just 5.\n"
1752 "@code{reduce-right} avoids that unnecessary call.\n"
1753 "\n"
1754 "@code{reduce} should be preferred over @code{reduce-right} if\n"
1755 "the order of processing doesn't matter, or can be arranged\n"
1756 "either way, since @code{reduce} is a little more efficient.")
1757 #define FUNC_NAME s_scm_srfi1_reduce_right
1758 {
1759 /* To work backwards across a list requires either repeatedly traversing
1760 to get each previous element, or using some memory for a reversed or
1761 random-access form. Repeated traversal might not be too terrible, but
1762 is of course quadratic complexity and hence to be avoided in case LST
1763 is long. A vector is preferred over a reversed list since it's more
1764 compact and is less work for the gc to collect. */
1765
1766 scm_t_trampoline_2 proc_tramp = scm_trampoline_2 (proc);
1767 SCM ret, vec;
1768 long len, i;
1769
1770 SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
1771
1772 if (SCM_NULL_OR_NIL_P (lst))
1773 return def;
1774
1775 vec = scm_vector (lst);
1776 len = SCM_SIMPLE_VECTOR_LENGTH (vec);
1777
1778 ret = SCM_SIMPLE_VECTOR_REF (vec, len-1);
1779 for (i = len-2; i >= 0; i--)
1780 ret = proc_tramp (proc, SCM_SIMPLE_VECTOR_REF (vec, i), ret);
1781
1782 return ret;
1783 }
1784 #undef FUNC_NAME
1785
1786
1787 SCM_DEFINE (scm_srfi1_remove, "remove", 2, 0, 0,
1788 (SCM pred, SCM list),
1789 "Return a list containing all elements from @var{lst} which do\n"
1790 "not satisfy the predicate @var{pred}. The elements in the\n"
1791 "result list have the same order as in @var{lst}. The order in\n"
1792 "which @var{pred} is applied to the list elements is not\n"
1793 "specified.")
1794 #define FUNC_NAME s_scm_srfi1_remove
1795 {
1796 scm_t_trampoline_1 call = scm_trampoline_1 (pred);
1797 SCM walk;
1798 SCM *prev;
1799 SCM res = SCM_EOL;
1800 SCM_ASSERT (call, pred, 1, FUNC_NAME);
1801 SCM_VALIDATE_LIST (2, list);
1802
1803 for (prev = &res, walk = list;
1804 scm_is_pair (walk);
1805 walk = SCM_CDR (walk))
1806 {
1807 if (scm_is_false (call (pred, SCM_CAR (walk))))
1808 {
1809 *prev = scm_cons (SCM_CAR (walk), SCM_EOL);
1810 prev = SCM_CDRLOC (*prev);
1811 }
1812 }
1813
1814 return res;
1815 }
1816 #undef FUNC_NAME
1817
1818
1819 SCM_DEFINE (scm_srfi1_remove_x, "remove!", 2, 0, 0,
1820 (SCM pred, SCM list),
1821 "Return a list containing all elements from @var{list} which do\n"
1822 "not satisfy the predicate @var{pred}. The elements in the\n"
1823 "result list have the same order as in @var{list}. The order in\n"
1824 "which @var{pred} is applied to the list elements is not\n"
1825 "specified. @var{list} may be modified to build the return\n"
1826 "list.")
1827 #define FUNC_NAME s_scm_srfi1_remove_x
1828 {
1829 scm_t_trampoline_1 call = scm_trampoline_1 (pred);
1830 SCM walk;
1831 SCM *prev;
1832 SCM_ASSERT (call, pred, 1, FUNC_NAME);
1833 SCM_VALIDATE_LIST (2, list);
1834
1835 for (prev = &list, walk = list;
1836 scm_is_pair (walk);
1837 walk = SCM_CDR (walk))
1838 {
1839 if (scm_is_false (call (pred, SCM_CAR (walk))))
1840 prev = SCM_CDRLOC (walk);
1841 else
1842 *prev = SCM_CDR (walk);
1843 }
1844
1845 return list;
1846 }
1847 #undef FUNC_NAME
1848
1849
1850 SCM_DEFINE (scm_srfi1_seventh, "seventh", 1, 0, 0,
1851 (SCM lst),
1852 "Return the seventh element of @var{lst}.")
1853 #define FUNC_NAME s_scm_srfi1_seventh
1854 {
1855 return scm_list_ref (lst, scm_from_int (6));
1856 }
1857 #undef FUNC_NAME
1858
1859
1860 SCM_DEFINE (scm_srfi1_sixth, "sixth", 1, 0, 0,
1861 (SCM lst),
1862 "Return the sixth element of @var{lst}.")
1863 #define FUNC_NAME s_scm_srfi1_sixth
1864 {
1865 return scm_list_ref (lst, scm_from_int (5));
1866 }
1867 #undef FUNC_NAME
1868
1869
1870 SCM_DEFINE (scm_srfi1_span, "span", 2, 0, 0,
1871 (SCM pred, SCM lst),
1872 "Return two values, the longest initial prefix of @var{lst}\n"
1873 "whose elements all satisfy the predicate @var{pred}, and the\n"
1874 "remainder of @var{lst}.")
1875 #define FUNC_NAME s_scm_srfi1_span
1876 {
1877 scm_t_trampoline_1 pred_tramp;
1878 SCM ret, *p;
1879
1880 pred_tramp = scm_trampoline_1 (pred);
1881 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1882
1883 ret = SCM_EOL;
1884 p = &ret;
1885 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
1886 {
1887 SCM elem = SCM_CAR (lst);
1888 if (scm_is_false (pred_tramp (pred, elem)))
1889 goto done;
1890
1891 /* want this elem, tack it onto the end of ret */
1892 *p = scm_cons (elem, SCM_EOL);
1893 p = SCM_CDRLOC (*p);
1894 }
1895 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
1896
1897 done:
1898 return scm_values (scm_list_2 (ret, lst));
1899 }
1900 #undef FUNC_NAME
1901
1902
1903 SCM_DEFINE (scm_srfi1_span_x, "span!", 2, 0, 0,
1904 (SCM pred, SCM lst),
1905 "Return two values, the longest initial prefix of @var{lst}\n"
1906 "whose elements all satisfy the predicate @var{pred}, and the\n"
1907 "remainder of @var{lst}. @var{lst} may be modified to form the\n"
1908 "return.")
1909 #define FUNC_NAME s_scm_srfi1_span_x
1910 {
1911 SCM upto, *p;
1912 scm_t_trampoline_1 pred_tramp;
1913
1914 pred_tramp = scm_trampoline_1 (pred);
1915 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
1916
1917 p = &lst;
1918 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
1919 {
1920 if (scm_is_false (pred_tramp (pred, SCM_CAR (upto))))
1921 goto done;
1922
1923 /* want this element */
1924 p = SCM_CDRLOC (upto);
1925 }
1926 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
1927
1928 done:
1929 *p = SCM_EOL;
1930 return scm_values (scm_list_2 (lst, upto));
1931 }
1932 #undef FUNC_NAME
1933
1934
1935 SCM_DEFINE (scm_srfi1_split_at, "split-at", 2, 0, 0,
1936 (SCM lst, SCM n),
1937 "Return two values (multiple values), being a list of the\n"
1938 "elements before index @var{n} in @var{lst}, and a list of those\n"
1939 "after.")
1940 #define FUNC_NAME s_scm_srfi1_split_at
1941 {
1942 size_t nn;
1943 /* pre is a list of elements before the i split point, loc is the CDRLOC
1944 of the last cell, ie. where to store to append to it */
1945 SCM pre = SCM_EOL;
1946 SCM *loc = &pre;
1947
1948 for (nn = scm_to_size_t (n); nn != 0; nn--)
1949 {
1950 SCM_VALIDATE_CONS (SCM_ARG1, lst);
1951
1952 *loc = scm_cons (SCM_CAR (lst), SCM_EOL);
1953 loc = SCM_CDRLOC (*loc);
1954 lst = SCM_CDR(lst);
1955 }
1956 return scm_values (scm_list_2 (pre, lst));
1957 }
1958 #undef FUNC_NAME
1959
1960
1961 SCM_DEFINE (scm_srfi1_split_at_x, "split-at!", 2, 0, 0,
1962 (SCM lst, SCM n),
1963 "Return two values (multiple values), being a list of the\n"
1964 "elements before index @var{n} in @var{lst}, and a list of those\n"
1965 "after. @var{lst} is modified to form those values.")
1966 #define FUNC_NAME s_scm_srfi1_split_at
1967 {
1968 size_t nn;
1969 SCM upto = lst;
1970 SCM *loc = &lst;
1971
1972 for (nn = scm_to_size_t (n); nn != 0; nn--)
1973 {
1974 SCM_VALIDATE_CONS (SCM_ARG1, upto);
1975
1976 loc = SCM_CDRLOC (upto);
1977 upto = SCM_CDR (upto);
1978 }
1979
1980 *loc = SCM_EOL;
1981 return scm_values (scm_list_2 (lst, upto));
1982 }
1983 #undef FUNC_NAME
1984
1985
1986 SCM_DEFINE (scm_srfi1_take_x, "take!", 2, 0, 0,
1987 (SCM lst, SCM n),
1988 "Return a list containing the first @var{n} elements of\n"
1989 "@var{lst}.")
1990 #define FUNC_NAME s_scm_srfi1_take_x
1991 {
1992 long nn;
1993 SCM pos;
1994
1995 nn = scm_to_signed_integer (n, 0, LONG_MAX);
1996 if (nn == 0)
1997 return SCM_EOL;
1998
1999 pos = scm_list_tail (lst, scm_from_long (nn - 1));
2000
2001 /* Must have at least one cell left, mustn't have reached the end of an
2002 n-1 element list. SCM_VALIDATE_CONS here gives the same error as
2003 scm_list_tail does on say an n-2 element list, though perhaps a range
2004 error would make more sense (for both). */
2005 SCM_VALIDATE_CONS (SCM_ARG1, pos);
2006
2007 SCM_SETCDR (pos, SCM_EOL);
2008 return lst;
2009 }
2010 #undef FUNC_NAME
2011
2012
2013 SCM_DEFINE (scm_srfi1_take_right, "take-right", 2, 0, 0,
2014 (SCM lst, SCM n),
2015 "Return the a list containing the @var{n} last elements of\n"
2016 "@var{lst}.")
2017 #define FUNC_NAME s_scm_srfi1_take_right
2018 {
2019 SCM tail = scm_list_tail (lst, n);
2020 while (scm_is_pair (tail))
2021 {
2022 lst = SCM_CDR (lst);
2023 tail = SCM_CDR (tail);
2024 }
2025 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P(tail), tail, SCM_ARG1, FUNC_NAME, "list");
2026 return lst;
2027 }
2028 #undef FUNC_NAME
2029
2030
2031 SCM_DEFINE (scm_srfi1_take_while, "take-while", 2, 0, 0,
2032 (SCM pred, SCM lst),
2033 "Return a new list which is the longest initial prefix of\n"
2034 "@var{lst} whose elements all satisfy the predicate @var{pred}.")
2035 #define FUNC_NAME s_scm_srfi1_take_while
2036 {
2037 scm_t_trampoline_1 pred_tramp;
2038 SCM ret, *p;
2039
2040 pred_tramp = scm_trampoline_1 (pred);
2041 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
2042
2043 ret = SCM_EOL;
2044 p = &ret;
2045 for ( ; scm_is_pair (lst); lst = SCM_CDR (lst))
2046 {
2047 SCM elem = SCM_CAR (lst);
2048 if (scm_is_false (pred_tramp (pred, elem)))
2049 goto done;
2050
2051 /* want this elem, tack it onto the end of ret */
2052 *p = scm_cons (elem, SCM_EOL);
2053 p = SCM_CDRLOC (*p);
2054 }
2055 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (lst), lst, SCM_ARG2, FUNC_NAME, "list");
2056
2057 done:
2058 return ret;
2059 }
2060 #undef FUNC_NAME
2061
2062
2063 SCM_DEFINE (scm_srfi1_take_while_x, "take-while!", 2, 0, 0,
2064 (SCM pred, SCM lst),
2065 "Return the longest initial prefix of @var{lst} whose elements\n"
2066 "all satisfy the predicate @var{pred}. @var{lst} may be\n"
2067 "modified to form the return.")
2068 #define FUNC_NAME s_scm_srfi1_take_while_x
2069 {
2070 SCM upto, *p;
2071 scm_t_trampoline_1 pred_tramp;
2072
2073 pred_tramp = scm_trampoline_1 (pred);
2074 SCM_ASSERT (pred_tramp, pred, SCM_ARG1, FUNC_NAME);
2075
2076 p = &lst;
2077 for (upto = lst; scm_is_pair (upto); upto = SCM_CDR (upto))
2078 {
2079 if (scm_is_false (pred_tramp (pred, SCM_CAR (upto))))
2080 goto done;
2081
2082 /* want this element */
2083 p = SCM_CDRLOC (upto);
2084 }
2085 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (upto), lst, SCM_ARG2, FUNC_NAME, "list");
2086
2087 done:
2088 *p = SCM_EOL;
2089 return lst;
2090 }
2091 #undef FUNC_NAME
2092
2093
2094 SCM_DEFINE (scm_srfi1_tenth, "tenth", 1, 0, 0,
2095 (SCM lst),
2096 "Return the tenth element of @var{lst}.")
2097 #define FUNC_NAME s_scm_srfi1_tenth
2098 {
2099 return scm_list_ref (lst, scm_from_int (9));
2100 }
2101 #undef FUNC_NAME
2102
2103
2104 SCM_DEFINE (scm_srfi1_xcons, "xcons", 2, 0, 0,
2105 (SCM d, SCM a),
2106 "Like @code{cons}, but with interchanged arguments. Useful\n"
2107 "mostly when passed to higher-order procedures.")
2108 #define FUNC_NAME s_scm_srfi1_xcons
2109 {
2110 return scm_cons (a, d);
2111 }
2112 #undef FUNC_NAME
2113
2114
2115 void
2116 scm_init_srfi_1 (void)
2117 {
2118 SCM the_root_module = scm_lookup_closure_module (SCM_BOOL_F);
2119 #ifndef SCM_MAGIC_SNARFER
2120 #include "srfi/srfi-1.x"
2121 #endif
2122 scm_c_extend_primitive_generic
2123 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "map")),
2124 SCM_VARIABLE_REF (scm_c_lookup ("map")));
2125 scm_c_extend_primitive_generic
2126 (SCM_VARIABLE_REF (scm_c_module_lookup (the_root_module, "for-each")),
2127 SCM_VARIABLE_REF (scm_c_lookup ("for-each")));
2128 }
2129
2130 /* End of srfi-1.c. */