Commit | Line | Data |
---|---|---|
6be07c52 TTN |
1 | ;;; srfi-1.scm --- List Library |
2 | ||
f595ccfe | 3 | ;; Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc. |
6be07c52 | 4 | ;; |
73be1d9e MV |
5 | ;; This library is free software; you can redistribute it and/or |
6 | ;; modify it under the terms of the GNU Lesser General Public | |
7 | ;; License as published by the Free Software Foundation; either | |
8 | ;; version 2.1 of the License, or (at your option) any later version. | |
9 | ;; | |
10 | ;; This library is distributed in the hope that it will be useful, | |
6be07c52 TTN |
11 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
73be1d9e MV |
13 | ;; Lesser General Public License for more details. |
14 | ;; | |
15 | ;; You should have received a copy of the GNU Lesser General Public | |
16 | ;; License along with this library; if not, write to the Free Software | |
17 | ;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
e9680547 MG |
18 | |
19 | ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de> | |
20 | ;;; Date: 2001-06-06 | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
6be07c52 TTN |
24 | ;; This is an implementation of SRFI-1 (List Library). |
25 | ;; | |
26 | ;; All procedures defined in SRFI-1, which are not already defined in | |
27 | ;; the Guile core library, are exported. The procedures in this | |
28 | ;; implementation work, but they have not been tuned for speed or | |
29 | ;; memory usage. | |
30 | ;; | |
31 | ;; This module is fully documented in the Guile Reference Manual. | |
e9680547 MG |
32 | |
33 | ;;; Code: | |
34 | ||
35 | (define-module (srfi srfi-1) | |
563058ef | 36 | :use-module (ice-9 session) |
f595ccfe MD |
37 | :use-module (ice-9 receive) |
38 | :export ( | |
e9680547 MG |
39 | ;;; Constructors |
40 | ;; cons <= in the core | |
41 | ;; list <= in the core | |
42 | xcons | |
43 | ;; cons* <= in the core | |
44 | ;; make-list <= in the core | |
45 | list-tabulate | |
d61261f0 | 46 | list-copy |
e9680547 | 47 | circular-list |
f595ccfe | 48 | ;; iota ; Extended. |
e9680547 MG |
49 | |
50 | ;;; Predicates | |
51 | proper-list? | |
52 | circular-list? | |
53 | dotted-list? | |
54 | ;; pair? <= in the core | |
55 | ;; null? <= in the core | |
56 | null-list? | |
57 | not-pair? | |
58 | list= | |
59 | ||
60 | ;;; Selectors | |
61 | ;; car <= in the core | |
62 | ;; cdr <= in the core | |
63 | ;; caar <= in the core | |
64 | ;; cadr <= in the core | |
65 | ;; cdar <= in the core | |
2a326497 | 66 | ;; cddr <= in the core |
e9680547 MG |
67 | ;; caaar <= in the core |
68 | ;; caadr <= in the core | |
69 | ;; cadar <= in the core | |
70 | ;; caddr <= in the core | |
71 | ;; cdaar <= in the core | |
72 | ;; cdadr <= in the core | |
73 | ;; cddar <= in the core | |
74 | ;; cdddr <= in the core | |
75 | ;; caaaar <= in the core | |
76 | ;; caaadr <= in the core | |
77 | ;; caadar <= in the core | |
78 | ;; caaddr <= in the core | |
79 | ;; cadaar <= in the core | |
80 | ;; cadadr <= in the core | |
81 | ;; caddar <= in the core | |
82 | ;; cadddr <= in the core | |
83 | ;; cdaaar <= in the core | |
84 | ;; cdaadr <= in the core | |
85 | ;; cdadar <= in the core | |
86 | ;; cdaddr <= in the core | |
87 | ;; cddaar <= in the core | |
88 | ;; cddadr <= in the core | |
89 | ;; cdddar <= in the core | |
90 | ;; cddddr <= in the core | |
91 | ;; list-ref <= in the core | |
92 | first | |
93 | second | |
94 | third | |
95 | fourth | |
96 | fifth | |
97 | sixth | |
98 | seventh | |
99 | eighth | |
100 | ninth | |
101 | tenth | |
102 | car+cdr | |
103 | take | |
104 | drop | |
105 | take-right | |
106 | drop-right | |
107 | take! | |
108 | drop-right! | |
109 | split-at | |
110 | split-at! | |
111 | last | |
112 | ;; last-pair <= in the core | |
113 | ||
114 | ;;; Miscelleneous: length, append, concatenate, reverse, zip & count | |
115 | ;; length <= in the core | |
116 | length+ | |
117 | ;; append <= in the core | |
118 | ;; append! <= in the core | |
119 | concatenate | |
120 | concatenate! | |
121 | ;; reverse <= in the core | |
122 | ;; reverse! <= in the core | |
123 | append-reverse | |
124 | append-reverse! | |
125 | zip | |
126 | unzip1 | |
127 | unzip2 | |
128 | unzip3 | |
129 | unzip4 | |
130 | unzip5 | |
131 | count | |
132 | ||
133 | ;;; Fold, unfold & map | |
134 | fold | |
135 | fold-right | |
136 | pair-fold | |
137 | pair-fold-right | |
138 | reduce | |
139 | reduce-right | |
140 | unfold | |
141 | unfold-right | |
f595ccfe MD |
142 | ;; map ; Extended. |
143 | ;; for-each ; Extended. | |
e9680547 MG |
144 | append-map |
145 | append-map! | |
146 | map! | |
f595ccfe | 147 | ;; map-in-order ; Extended. |
e9680547 MG |
148 | pair-for-each |
149 | filter-map | |
150 | ||
151 | ;;; Filtering & partitioning | |
c614a00b | 152 | ;; filter <= in the core |
e9680547 MG |
153 | partition |
154 | remove | |
c614a00b | 155 | ;; filter! <= in the core |
e9680547 MG |
156 | partition! |
157 | remove! | |
158 | ||
159 | ;;; Searching | |
160 | find | |
161 | find-tail | |
162 | take-while | |
163 | take-while! | |
164 | drop-while | |
165 | span | |
166 | span! | |
167 | break | |
168 | break! | |
169 | any | |
170 | every | |
f595ccfe MD |
171 | ;; list-index ; Extended. |
172 | ;; member ; Extended. | |
e9680547 MG |
173 | ;; memq <= in the core |
174 | ;; memv <= in the core | |
175 | ||
176 | ;;; Deletion | |
f595ccfe MD |
177 | ;; delete ; Extended. |
178 | ;; delete! ; Extended. | |
e9680547 MG |
179 | delete-duplicates |
180 | delete-duplicates! | |
181 | ||
182 | ;;; Association lists | |
f595ccfe | 183 | ;; assoc ; Extended. |
e9680547 MG |
184 | ;; assq <= in the core |
185 | ;; assv <= in the core | |
186 | alist-cons | |
187 | alist-copy | |
188 | alist-delete | |
189 | alist-delete! | |
190 | ||
191 | ;;; Set operations on lists | |
192 | lset<= | |
193 | lset= | |
194 | lset-adjoin | |
195 | lset-union | |
196 | lset-intersection | |
197 | lset-difference | |
198 | lset-xor | |
199 | lset-diff+intersection | |
200 | lset-union! | |
201 | lset-intersection! | |
202 | lset-difference! | |
203 | lset-xor! | |
204 | lset-diff+intersection! | |
205 | ||
206 | ;;; Primitive side-effects | |
207 | ;; set-car! <= in the core | |
208 | ;; set-cdr! <= in the core | |
209 | ) | |
d61261f0 | 210 | :re-export (cons list cons* make-list pair? null? |
b8b0abf0 MD |
211 | car cdr caar cadr cdar cddr |
212 | caaar caadr cadar caddr cdaar cdadr cddar cdddr | |
213 | caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr | |
214 | cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr | |
215 | list-ref last-pair length append append! reverse reverse! | |
216 | filter filter! memq memv assq assv set-car! set-cdr!) | |
d61261f0 | 217 | :replace (iota map for-each map-in-order list-copy list-index member |
f595ccfe MD |
218 | delete delete! assoc) |
219 | ) | |
e9680547 MG |
220 | |
221 | (cond-expand-provide (current-module) '(srfi-1)) | |
222 | ||
ee6aac97 MD |
223 | ;; Load the compiled primitives from the shared library. |
224 | ;; | |
225 | (load-extension "libguile-srfi-srfi-1" "scm_init_srfi_1") | |
226 | ||
227 | ||
e9680547 MG |
228 | ;;; Constructors |
229 | ||
230 | (define (xcons d a) | |
231 | (cons a d)) | |
232 | ||
5753f02f GH |
233 | ;; internal helper, similar to (scsh utilities) check-arg. |
234 | (define (check-arg-type pred arg caller) | |
235 | (if (pred arg) | |
236 | arg | |
237 | (scm-error 'wrong-type-arg caller | |
238 | "Wrong type argument: ~S" (list arg) '()))) | |
239 | ||
240 | ;; the srfi spec doesn't seem to forbid inexact integers. | |
241 | (define (non-negative-integer? x) (and (integer? x) (>= x 0))) | |
242 | ||
e9680547 | 243 | (define (list-tabulate n init-proc) |
5753f02f | 244 | (check-arg-type non-negative-integer? n "list-tabulate") |
e9680547 | 245 | (let lp ((n n) (acc '())) |
018adcae | 246 | (if (<= n 0) |
e9680547 MG |
247 | acc |
248 | (lp (- n 1) (cons (init-proc (- n 1)) acc))))) | |
249 | ||
250 | (define (circular-list elt1 . rest) | |
251 | (let ((start (cons elt1 '()))) | |
252 | (let lp ((r rest) (p start)) | |
253 | (if (null? r) | |
254 | (begin | |
255 | (set-cdr! p start) | |
256 | start) | |
257 | (begin | |
258 | (set-cdr! p (cons (car r) '())) | |
259 | (lp (cdr r) (cdr p))))))) | |
260 | ||
261 | (define (iota count . rest) | |
5753f02f | 262 | (check-arg-type non-negative-integer? count "iota") |
e9680547 MG |
263 | (let ((start (if (pair? rest) (car rest) 0)) |
264 | (step (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 1))) | |
265 | (let lp ((n 0) (acc '())) | |
266 | (if (= n count) | |
267 | (reverse! acc) | |
268 | (lp (+ n 1) (cons (+ start (* n step)) acc)))))) | |
269 | ||
270 | ;;; Predicates | |
271 | ||
272 | (define (proper-list? x) | |
273 | (list? x)) | |
274 | ||
275 | (define (circular-list? x) | |
276 | (if (not-pair? x) | |
277 | #f | |
278 | (let lp ((hare (cdr x)) (tortoise x)) | |
279 | (if (not-pair? hare) | |
280 | #f | |
281 | (let ((hare (cdr hare))) | |
282 | (if (not-pair? hare) | |
283 | #f | |
284 | (if (eq? hare tortoise) | |
285 | #t | |
286 | (lp (cdr hare) (cdr tortoise))))))))) | |
287 | ||
288 | (define (dotted-list? x) | |
289 | (cond | |
290 | ((null? x) #f) | |
291 | ((not-pair? x) #t) | |
292 | (else | |
293 | (let lp ((hare (cdr x)) (tortoise x)) | |
294 | (cond | |
295 | ((null? hare) #f) | |
296 | ((not-pair? hare) #t) | |
297 | (else | |
298 | (let ((hare (cdr hare))) | |
513a3bd7 | 299 | (cond |
e9680547 MG |
300 | ((null? hare) #f) |
301 | ((not-pair? hare) #t) | |
302 | ((eq? hare tortoise) #f) | |
303 | (else | |
304 | (lp (cdr hare) (cdr tortoise))))))))))) | |
305 | ||
306 | (define (null-list? x) | |
307 | (cond | |
513a3bd7 | 308 | ((proper-list? x) |
e9680547 MG |
309 | (null? x)) |
310 | ((circular-list? x) | |
311 | #f) | |
312 | (else | |
313 | (error "not a proper list in null-list?")))) | |
314 | ||
315 | (define (not-pair? x) | |
316 | (not (pair? x))) | |
317 | ||
318 | (define (list= elt= . rest) | |
319 | (define (lists-equal a b) | |
320 | (let lp ((a a) (b b)) | |
321 | (cond ((null? a) | |
322 | (null? b)) | |
323 | ((null? b) | |
324 | #f) | |
325 | (else | |
326 | (and (elt= (car a) (car b)) | |
327 | (lp (cdr a) (cdr b))))))) | |
328 | (or (null? rest) | |
329 | (let ((first (car rest))) | |
330 | (let lp ((lists rest)) | |
331 | (or (null? lists) | |
332 | (and (lists-equal first (car lists)) | |
333 | (lp (cdr lists)))))))) | |
334 | ||
335 | ;;; Selectors | |
336 | ||
337 | (define first car) | |
338 | (define second cadr) | |
339 | (define third caddr) | |
340 | (define fourth cadddr) | |
341 | (define (fifth x) (car (cddddr x))) | |
342 | (define (sixth x) (cadr (cddddr x))) | |
343 | (define (seventh x) (caddr (cddddr x))) | |
344 | (define (eighth x) (cadddr (cddddr x))) | |
345 | (define (ninth x) (car (cddddr (cddddr x)))) | |
346 | (define (tenth x) (cadr (cddddr (cddddr x)))) | |
347 | ||
348 | (define (car+cdr x) (values (car x) (cdr x))) | |
349 | ||
4dd6bd84 KR |
350 | (define take list-head) |
351 | (define drop list-tail) | |
352 | ||
e9680547 MG |
353 | (define (take-right flist i) |
354 | (let lp ((n i) (l flist)) | |
e800aa04 | 355 | (if (<= n 0) |
e9680547 MG |
356 | (let lp0 ((s flist) (l l)) |
357 | (if (null? l) | |
358 | s | |
359 | (lp0 (cdr s) (cdr l)))) | |
360 | (lp (- n 1) (cdr l))))) | |
513a3bd7 | 361 | |
e9680547 MG |
362 | (define (drop-right flist i) |
363 | (let lp ((n i) (l flist)) | |
e800aa04 | 364 | (if (<= n 0) |
e9680547 MG |
365 | (let lp0 ((s flist) (l l) (acc '())) |
366 | (if (null? l) | |
367 | (reverse! acc) | |
368 | (lp0 (cdr s) (cdr l) (cons (car s) acc)))) | |
369 | (lp (- n 1) (cdr l))))) | |
370 | ||
371 | (define (take! x i) | |
e800aa04 | 372 | (if (<= i 0) |
e9680547 MG |
373 | '() |
374 | (let lp ((n (- i 1)) (l x)) | |
e800aa04 | 375 | (if (<= n 0) |
513a3bd7 | 376 | (begin |
e9680547 MG |
377 | (set-cdr! l '()) |
378 | x) | |
379 | (lp (- n 1) (cdr l)))))) | |
380 | ||
381 | (define (drop-right! flist i) | |
e800aa04 | 382 | (if (<= i 0) |
e9680547 MG |
383 | flist |
384 | (let lp ((n (+ i 1)) (l flist)) | |
e800aa04 | 385 | (if (<= n 0) |
e9680547 MG |
386 | (let lp0 ((s flist) (l l)) |
387 | (if (null? l) | |
388 | (begin | |
389 | (set-cdr! s '()) | |
390 | flist) | |
391 | (lp0 (cdr s) (cdr l)))) | |
392 | (if (null? l) | |
393 | '() | |
394 | (lp (- n 1) (cdr l))))))) | |
395 | ||
396 | (define (split-at x i) | |
397 | (let lp ((l x) (n i) (acc '())) | |
e800aa04 | 398 | (if (<= n 0) |
e9680547 MG |
399 | (values (reverse! acc) l) |
400 | (lp (cdr l) (- n 1) (cons (car l) acc))))) | |
401 | ||
402 | (define (split-at! x i) | |
e800aa04 | 403 | (if (<= i 0) |
e9680547 MG |
404 | (values '() x) |
405 | (let lp ((l x) (n (- i 1))) | |
e800aa04 | 406 | (if (<= n 0) |
e9680547 MG |
407 | (let ((tmp (cdr l))) |
408 | (set-cdr! l '()) | |
409 | (values x tmp)) | |
410 | (lp (cdr l) (- n 1)))))) | |
411 | ||
412 | (define (last pair) | |
413 | (car (last-pair pair))) | |
414 | ||
415 | ;;; Miscelleneous: length, append, concatenate, reverse, zip & count | |
416 | ||
e9680547 MG |
417 | (define (append-reverse rev-head tail) |
418 | (let lp ((l rev-head) (acc tail)) | |
419 | (if (null? l) | |
420 | acc | |
421 | (lp (cdr l) (cons (car l) acc))))) | |
422 | ||
423 | (define (append-reverse! rev-head tail) | |
424 | (append-reverse rev-head tail)) ; XXX:optimize | |
425 | ||
426 | (define (zip clist1 . rest) | |
427 | (let lp ((l (cons clist1 rest)) (acc '())) | |
428 | (if (any null? l) | |
429 | (reverse! acc) | |
cef248dd | 430 | (lp (map1 cdr l) (cons (map1 car l) acc))))) |
513a3bd7 | 431 | |
e9680547 MG |
432 | |
433 | (define (unzip1 l) | |
cef248dd | 434 | (map1 first l)) |
e9680547 | 435 | (define (unzip2 l) |
cef248dd | 436 | (values (map1 first l) (map1 second l))) |
e9680547 | 437 | (define (unzip3 l) |
cef248dd | 438 | (values (map1 first l) (map1 second l) (map1 third l))) |
e9680547 | 439 | (define (unzip4 l) |
cef248dd | 440 | (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l))) |
e9680547 | 441 | (define (unzip5 l) |
cef248dd MG |
442 | (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l) |
443 | (map1 fifth l))) | |
e9680547 MG |
444 | |
445 | (define (count pred clist1 . rest) | |
446 | (if (null? rest) | |
447 | (count1 pred clist1) | |
448 | (let lp ((lists (cons clist1 rest))) | |
449 | (cond ((any1 null? lists) | |
450 | 0) | |
451 | (else | |
cef248dd MG |
452 | (if (apply pred (map1 car lists)) |
453 | (+ 1 (lp (map1 cdr lists))) | |
454 | (lp (map1 cdr lists)))))))) | |
e9680547 MG |
455 | |
456 | (define (count1 pred clist) | |
e4cb30df TTN |
457 | (let lp ((result 0) (rest clist)) |
458 | (if (null? rest) | |
459 | result | |
460 | (if (pred (car rest)) | |
461 | (lp (+ 1 result) (cdr rest)) | |
462 | (lp result (cdr rest)))))) | |
e9680547 MG |
463 | |
464 | ;;; Fold, unfold & map | |
465 | ||
466 | (define (fold kons knil list1 . rest) | |
467 | (if (null? rest) | |
468 | (let f ((knil knil) (list1 list1)) | |
469 | (if (null? list1) | |
470 | knil | |
471 | (f (kons (car list1) knil) (cdr list1)))) | |
472 | (let f ((knil knil) (lists (cons list1 rest))) | |
473 | (if (any null? lists) | |
474 | knil | |
cef248dd MG |
475 | (let ((cars (map1 car lists)) |
476 | (cdrs (map1 cdr lists))) | |
563058ef | 477 | (f (apply kons (append! cars (list knil))) cdrs)))))) |
e9680547 MG |
478 | |
479 | (define (fold-right kons knil clist1 . rest) | |
480 | (if (null? rest) | |
481 | (let f ((list1 clist1)) | |
482 | (if (null? list1) | |
483 | knil | |
484 | (kons (car list1) (f (cdr list1))))) | |
485 | (let f ((lists (cons clist1 rest))) | |
486 | (if (any null? lists) | |
487 | knil | |
cef248dd | 488 | (apply kons (append! (map1 car lists) (list (f (map1 cdr lists))))))))) |
e9680547 MG |
489 | |
490 | (define (pair-fold kons knil clist1 . rest) | |
491 | (if (null? rest) | |
492 | (let f ((knil knil) (list1 clist1)) | |
493 | (if (null? list1) | |
494 | knil | |
495 | (let ((tail (cdr list1))) | |
496 | (f (kons list1 knil) tail)))) | |
497 | (let f ((knil knil) (lists (cons clist1 rest))) | |
498 | (if (any null? lists) | |
499 | knil | |
cef248dd | 500 | (let ((tails (map1 cdr lists))) |
563058ef | 501 | (f (apply kons (append! lists (list knil))) tails)))))) |
e9680547 MG |
502 | |
503 | ||
504 | (define (pair-fold-right kons knil clist1 . rest) | |
505 | (if (null? rest) | |
506 | (let f ((list1 clist1)) | |
507 | (if (null? list1) | |
508 | knil | |
509 | (kons list1 (f (cdr list1))))) | |
510 | (let f ((lists (cons clist1 rest))) | |
511 | (if (any null? lists) | |
512 | knil | |
cef248dd | 513 | (apply kons (append! lists (list (f (map1 cdr lists))))))))) |
e9680547 MG |
514 | |
515 | (define (unfold p f g seed . rest) | |
516 | (let ((tail-gen (if (pair? rest) | |
517 | (if (pair? (cdr rest)) | |
518 | (scm-error 'wrong-number-of-args | |
519 | "unfold" "too many arguments" '() '()) | |
520 | (car rest)) | |
521 | (lambda (x) '())))) | |
522 | (let uf ((seed seed)) | |
523 | (if (p seed) | |
524 | (tail-gen seed) | |
525 | (cons (f seed) | |
526 | (uf (g seed))))))) | |
527 | ||
528 | (define (unfold-right p f g seed . rest) | |
529 | (let ((tail (if (pair? rest) | |
530 | (if (pair? (cdr rest)) | |
531 | (scm-error 'wrong-number-of-args | |
532 | "unfold-right" "too many arguments" '() | |
533 | '()) | |
534 | (car rest)) | |
535 | '()))) | |
536 | (let uf ((seed seed) (lis tail)) | |
537 | (if (p seed) | |
538 | lis | |
539 | (uf (g seed) (cons (f seed) lis)))))) | |
540 | ||
541 | (define (reduce f ridentity lst) | |
542 | (fold f ridentity lst)) | |
543 | ||
544 | (define (reduce-right f ridentity lst) | |
545 | (fold-right f ridentity lst)) | |
546 | ||
cef248dd MG |
547 | |
548 | ;; Internal helper procedure. Map `f' over the single list `ls'. | |
549 | ;; | |
7692d26b | 550 | (define map1 map) |
cef248dd | 551 | |
e9680547 MG |
552 | (define (append-map f clist1 . rest) |
553 | (if (null? rest) | |
554 | (let lp ((l clist1)) | |
555 | (if (null? l) | |
556 | '() | |
557 | (append (f (car l)) (lp (cdr l))))) | |
558 | (let lp ((l (cons clist1 rest))) | |
559 | (if (any1 null? l) | |
560 | '() | |
cef248dd MG |
561 | (append (apply f (map1 car l)) (lp (map1 cdr l))))))) |
562 | ||
e9680547 MG |
563 | |
564 | (define (append-map! f clist1 . rest) | |
565 | (if (null? rest) | |
566 | (let lp ((l clist1)) | |
567 | (if (null? l) | |
568 | '() | |
569 | (append! (f (car l)) (lp (cdr l))))) | |
570 | (let lp ((l (cons clist1 rest))) | |
571 | (if (any1 null? l) | |
572 | '() | |
cef248dd | 573 | (append! (apply f (map1 car l)) (lp (map1 cdr l))))))) |
e9680547 MG |
574 | |
575 | (define (map! f list1 . rest) | |
576 | (if (null? rest) | |
577 | (let lp ((l list1)) | |
578 | (if (null? l) | |
579 | '() | |
580 | (begin | |
581 | (set-car! l (f (car l))) | |
582 | (set-cdr! l (lp (cdr l))) | |
583 | l))) | |
584 | (let lp ((l (cons list1 rest)) (res list1)) | |
585 | (if (any1 null? l) | |
586 | '() | |
587 | (begin | |
cef248dd MG |
588 | (set-car! res (apply f (map1 car l))) |
589 | (set-cdr! res (lp (map1 cdr l) (cdr res))) | |
e9680547 MG |
590 | res))))) |
591 | ||
592 | (define (pair-for-each f clist1 . rest) | |
593 | (if (null? rest) | |
594 | (let lp ((l clist1)) | |
595 | (if (null? l) | |
596 | (if #f #f) | |
597 | (begin | |
598 | (f l) | |
599 | (lp (cdr l))))) | |
600 | (let lp ((l (cons clist1 rest))) | |
601 | (if (any1 null? l) | |
602 | (if #f #f) | |
603 | (begin | |
604 | (apply f l) | |
cef248dd | 605 | (lp (map1 cdr l))))))) |
e9680547 MG |
606 | |
607 | (define (filter-map f clist1 . rest) | |
608 | (if (null? rest) | |
609 | (let lp ((l clist1)) | |
610 | (if (null? l) | |
611 | '() | |
612 | (let ((res (f (car l)))) | |
613 | (if res | |
614 | (cons res (lp (cdr l))) | |
615 | (lp (cdr l)))))) | |
616 | (let lp ((l (cons clist1 rest))) | |
617 | (if (any1 null? l) | |
618 | '() | |
cef248dd | 619 | (let ((res (apply f (map1 car l)))) |
e9680547 | 620 | (if res |
cef248dd MG |
621 | (cons res (lp (map1 cdr l))) |
622 | (lp (map1 cdr l)))))))) | |
e9680547 MG |
623 | |
624 | ;;; Filtering & partitioning | |
625 | ||
e9680547 | 626 | (define (remove pred list) |
848458d9 | 627 | (filter (lambda (x) (not (pred x))) list)) |
e9680547 | 628 | |
e9680547 MG |
629 | (define (partition! pred list) |
630 | (partition pred list)) ; XXX:optimize | |
631 | ||
632 | (define (remove! pred list) | |
633 | (remove pred list)) ; XXX:optimize | |
634 | ||
635 | ;;; Searching | |
636 | ||
637 | (define (find pred clist) | |
638 | (if (null? clist) | |
639 | #f | |
640 | (if (pred (car clist)) | |
641 | (car clist) | |
642 | (find pred (cdr clist))))) | |
643 | ||
644 | (define (find-tail pred clist) | |
645 | (if (null? clist) | |
646 | #f | |
647 | (if (pred (car clist)) | |
648 | clist | |
649 | (find-tail pred (cdr clist))))) | |
650 | ||
e4cb30df TTN |
651 | (define (take-while pred ls) |
652 | (cond ((null? ls) '()) | |
653 | ((not (pred (car ls))) '()) | |
654 | (else | |
655 | (let ((result (list (car ls)))) | |
656 | (let lp ((ls (cdr ls)) (p result)) | |
657 | (cond ((null? ls) result) | |
658 | ((not (pred (car ls))) result) | |
659 | (else | |
660 | (set-cdr! p (list (car ls))) | |
661 | (lp (cdr ls) (cdr p))))))))) | |
e9680547 MG |
662 | |
663 | (define (take-while! pred clist) | |
664 | (take-while pred clist)) ; XXX:optimize | |
665 | ||
666 | (define (drop-while pred clist) | |
667 | (if (null? clist) | |
668 | '() | |
669 | (if (pred (car clist)) | |
670 | (drop-while pred (cdr clist)) | |
671 | clist))) | |
672 | ||
673 | (define (span pred clist) | |
674 | (if (null? clist) | |
675 | (values '() '()) | |
676 | (if (pred (car clist)) | |
677 | (receive (first last) (span pred (cdr clist)) | |
678 | (values (cons (car clist) first) last)) | |
679 | (values '() clist)))) | |
680 | ||
681 | (define (span! pred list) | |
682 | (span pred list)) ; XXX:optimize | |
683 | ||
684 | (define (break pred clist) | |
685 | (if (null? clist) | |
686 | (values '() '()) | |
687 | (if (pred (car clist)) | |
688 | (values '() clist) | |
689 | (receive (first last) (break pred (cdr clist)) | |
690 | (values (cons (car clist) first) last))))) | |
691 | ||
692 | (define (break! pred list) | |
693 | (break pred list)) ; XXX:optimize | |
694 | ||
695 | (define (any pred ls . lists) | |
696 | (if (null? lists) | |
697 | (any1 pred ls) | |
698 | (let lp ((lists (cons ls lists))) | |
699 | (cond ((any1 null? lists) | |
700 | #f) | |
cef248dd MG |
701 | ((any1 null? (map1 cdr lists)) |
702 | (apply pred (map1 car lists))) | |
e9680547 | 703 | (else |
cef248dd | 704 | (or (apply pred (map1 car lists)) (lp (map1 cdr lists)))))))) |
e9680547 MG |
705 | |
706 | (define (any1 pred ls) | |
707 | (let lp ((ls ls)) | |
708 | (cond ((null? ls) | |
709 | #f) | |
710 | ((null? (cdr ls)) | |
711 | (pred (car ls))) | |
712 | (else | |
713 | (or (pred (car ls)) (lp (cdr ls))))))) | |
714 | ||
715 | (define (every pred ls . lists) | |
716 | (if (null? lists) | |
717 | (every1 pred ls) | |
718 | (let lp ((lists (cons ls lists))) | |
719 | (cond ((any1 null? lists) | |
720 | #t) | |
cef248dd MG |
721 | ((any1 null? (map1 cdr lists)) |
722 | (apply pred (map1 car lists))) | |
e9680547 | 723 | (else |
cef248dd | 724 | (and (apply pred (map1 car lists)) (lp (map1 cdr lists)))))))) |
e9680547 MG |
725 | |
726 | (define (every1 pred ls) | |
727 | (let lp ((ls ls)) | |
728 | (cond ((null? ls) | |
729 | #t) | |
730 | ((null? (cdr ls)) | |
731 | (pred (car ls))) | |
732 | (else | |
733 | (and (pred (car ls)) (lp (cdr ls))))))) | |
734 | ||
735 | (define (list-index pred clist1 . rest) | |
736 | (if (null? rest) | |
737 | (let lp ((l clist1) (i 0)) | |
738 | (if (null? l) | |
739 | #f | |
740 | (if (pred (car l)) | |
741 | i | |
742 | (lp (cdr l) (+ i 1))))) | |
743 | (let lp ((lists (cons clist1 rest)) (i 0)) | |
744 | (cond ((any1 null? lists) | |
745 | #f) | |
cef248dd | 746 | ((apply pred (map1 car lists)) i) |
e9680547 | 747 | (else |
cef248dd | 748 | (lp (map1 cdr lists) (+ i 1))))))) |
e9680547 | 749 | |
e9680547 MG |
750 | ;;; Association lists |
751 | ||
e9680547 MG |
752 | (define (alist-cons key datum alist) |
753 | (acons key datum alist)) | |
754 | ||
755 | (define (alist-copy alist) | |
756 | (let lp ((a alist)) | |
757 | (if (null? a) | |
758 | '() | |
563058ef | 759 | (acons (caar a) (cdar a) (lp (cdr a)))))) |
e9680547 MG |
760 | |
761 | (define (alist-delete key alist . rest) | |
762 | (let ((k= (if (pair? rest) (car rest) equal?))) | |
763 | (let lp ((a alist)) | |
764 | (if (null? a) | |
765 | '() | |
766 | (if (k= (caar a) key) | |
767 | (lp (cdr a)) | |
768 | (cons (car a) (lp (cdr a)))))))) | |
769 | ||
770 | (define (alist-delete! key alist . rest) | |
771 | (let ((k= (if (pair? rest) (car rest) equal?))) | |
772 | (alist-delete key alist k=))) ; XXX:optimize | |
773 | ||
774 | ;;; Set operations on lists | |
775 | ||
776 | (define (lset<= = . rest) | |
777 | (if (null? rest) | |
778 | #t | |
779 | (let lp ((f (car rest)) (r (cdr rest))) | |
780 | (or (null? r) | |
781 | (and (every (lambda (el) (member el (car r) =)) f) | |
782 | (lp (car r) (cdr r))))))) | |
783 | ||
784 | (define (lset= = list1 . rest) | |
785 | (if (null? rest) | |
786 | #t | |
787 | (let lp ((f list1) (r rest)) | |
788 | (or (null? r) | |
789 | (and (every (lambda (el) (member el (car r) =)) f) | |
790 | (every (lambda (el) (member el f =)) (car r)) | |
791 | (lp (car r) (cdr r))))))) | |
792 | ||
793 | (define (lset-adjoin = list . rest) | |
794 | (let lp ((l rest) (acc list)) | |
795 | (if (null? l) | |
796 | acc | |
797 | (if (member (car l) acc) | |
798 | (lp (cdr l) acc) | |
799 | (lp (cdr l) (cons (car l) acc)))))) | |
800 | ||
801 | (define (lset-union = . rest) | |
802 | (let lp0 ((l rest) (acc '())) | |
803 | (if (null? l) | |
804 | (reverse! acc) | |
805 | (let lp1 ((ll (car l)) (acc acc)) | |
806 | (if (null? ll) | |
807 | (lp0 (cdr l) acc) | |
808 | (if (member (car ll) acc =) | |
809 | (lp1 (cdr ll) acc) | |
810 | (lp1 (cdr ll) (cons (car ll) acc)))))))) | |
811 | ||
812 | (define (lset-intersection = list1 . rest) | |
813 | (let lp ((l list1) (acc '())) | |
814 | (if (null? l) | |
815 | (reverse! acc) | |
816 | (if (every (lambda (ll) (member (car l) ll =)) rest) | |
817 | (lp (cdr l) (cons (car l) acc)) | |
818 | (lp (cdr l) acc))))) | |
819 | ||
820 | (define (lset-difference = list1 . rest) | |
821 | (if (null? rest) | |
822 | list1 | |
823 | (let lp ((l list1) (acc '())) | |
824 | (if (null? l) | |
825 | (reverse! acc) | |
826 | (if (any (lambda (ll) (member (car l) ll =)) rest) | |
827 | (lp (cdr l) acc) | |
828 | (lp (cdr l) (cons (car l) acc))))))) | |
829 | ||
830 | ;(define (fold kons knil list1 . rest) | |
831 | ||
832 | (define (lset-xor = . rest) | |
833 | (fold (lambda (lst res) | |
834 | (let lp ((l lst) (acc '())) | |
835 | (if (null? l) | |
836 | (let lp0 ((r res) (acc acc)) | |
837 | (if (null? r) | |
838 | (reverse! acc) | |
839 | (if (member (car r) lst =) | |
840 | (lp0 (cdr r) acc) | |
841 | (lp0 (cdr r) (cons (car r) acc))))) | |
842 | (if (member (car l) res =) | |
843 | (lp (cdr l) acc) | |
844 | (lp (cdr l) (cons (car l) acc)))))) | |
845 | '() | |
846 | rest)) | |
847 | ||
848 | (define (lset-diff+intersection = list1 . rest) | |
849 | (let lp ((l list1) (accd '()) (acci '())) | |
850 | (if (null? l) | |
851 | (values (reverse! accd) (reverse! acci)) | |
852 | (let ((appears (every (lambda (ll) (member (car l) ll =)) rest))) | |
853 | (if appears | |
854 | (lp (cdr l) accd (cons (car l) acci)) | |
855 | (lp (cdr l) (cons (car l) accd) acci)))))) | |
856 | ||
857 | ||
858 | (define (lset-union! = . rest) | |
859 | (apply lset-union = rest)) ; XXX:optimize | |
860 | ||
861 | (define (lset-intersection! = list1 . rest) | |
862 | (apply lset-intersection = list1 rest)) ; XXX:optimize | |
863 | ||
864 | (define (lset-difference! = list1 . rest) | |
865 | (apply lset-difference = list1 rest)) ; XXX:optimize | |
866 | ||
867 | (define (lset-xor! = . rest) | |
868 | (apply lset-xor = rest)) ; XXX:optimize | |
869 | ||
870 | (define (lset-diff+intersection! = list1 . rest) | |
871 | (apply lset-diff+intersection = list1 rest)) ; XXX:optimize | |
6be07c52 TTN |
872 | |
873 | ;;; srfi-1.scm ends here |