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