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