*** empty log message ***
[bpt/guile.git] / srfi / srfi-1.scm
CommitLineData
e9680547
MG
1;;;; srfi-1.scm --- SRFI-1 procedures for Guile
2;;;;
3;;;; Copyright (C) 2001 Free Software Foundation, Inc.
4;;;;
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.
9;;;;
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.
14;;;;
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
63(export
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
fc183bc8 73 ;; iota ; exported below
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
fc183bc8
GH
167 ;; map ; exported below
168 ;; for-each ; exported below
e9680547
MG
169 append-map
170 append-map!
171 map!
6d52dbf2 172 ;; map-in-order ; exported below
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
fc183bc8
GH
196 ;; list-index ; exported below.
197 ;; member ; exported below ; Extended.
e9680547
MG
198 ;; memq <= in the core
199 ;; memv <= in the core
200
201;;; Deletion
fc183bc8
GH
202;; delete ; exported below ; Extended.
203;; delete! ; exported below
e9680547
MG
204 delete-duplicates
205 delete-duplicates!
206
207;;; Association lists
fc183bc8 208 ;; assoc ; exported below ; 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
243(define (list-tabulate n init-proc)
244 (let lp ((n n) (acc '()))
018adcae 245 (if (<= n 0)
e9680547
MG
246 acc
247 (lp (- n 1) (cons (init-proc (- n 1)) acc)))))
248
249(define (circular-list elt1 . rest)
250 (let ((start (cons elt1 '())))
251 (let lp ((r rest) (p start))
252 (if (null? r)
253 (begin
254 (set-cdr! p start)
255 start)
256 (begin
257 (set-cdr! p (cons (car r) '()))
258 (lp (cdr r) (cdr p)))))))
259
260(define (iota count . rest)
261 (let ((start (if (pair? rest) (car rest) 0))
262 (step (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 1)))
263 (let lp ((n 0) (acc '()))
264 (if (= n count)
265 (reverse! acc)
266 (lp (+ n 1) (cons (+ start (* n step)) acc))))))
267
268;;; Predicates
269
270(define (proper-list? x)
271 (list? x))
272
273(define (circular-list? x)
274 (if (not-pair? x)
275 #f
276 (let lp ((hare (cdr x)) (tortoise x))
277 (if (not-pair? hare)
278 #f
279 (let ((hare (cdr hare)))
280 (if (not-pair? hare)
281 #f
282 (if (eq? hare tortoise)
283 #t
284 (lp (cdr hare) (cdr tortoise)))))))))
285
286(define (dotted-list? x)
287 (cond
288 ((null? x) #f)
289 ((not-pair? x) #t)
290 (else
291 (let lp ((hare (cdr x)) (tortoise x))
292 (cond
293 ((null? hare) #f)
294 ((not-pair? hare) #t)
295 (else
296 (let ((hare (cdr hare)))
297 (cond
298 ((null? hare) #f)
299 ((not-pair? hare) #t)
300 ((eq? hare tortoise) #f)
301 (else
302 (lp (cdr hare) (cdr tortoise)))))))))))
303
304(define (null-list? x)
305 (cond
306 ((proper-list? x)
307 (null? x))
308 ((circular-list? x)
309 #f)
310 (else
311 (error "not a proper list in null-list?"))))
312
313(define (not-pair? x)
314 (not (pair? x)))
315
316(define (list= elt= . rest)
317 (define (lists-equal a b)
318 (let lp ((a a) (b b))
319 (cond ((null? a)
320 (null? b))
321 ((null? b)
322 #f)
323 (else
324 (and (elt= (car a) (car b))
325 (lp (cdr a) (cdr b)))))))
326 (or (null? rest)
327 (let ((first (car rest)))
328 (let lp ((lists rest))
329 (or (null? lists)
330 (and (lists-equal first (car lists))
331 (lp (cdr lists))))))))
332
333;;; Selectors
334
335(define first car)
336(define second cadr)
337(define third caddr)
338(define fourth cadddr)
339(define (fifth x) (car (cddddr x)))
340(define (sixth x) (cadr (cddddr x)))
341(define (seventh x) (caddr (cddddr x)))
342(define (eighth x) (cadddr (cddddr x)))
343(define (ninth x) (car (cddddr (cddddr x))))
344(define (tenth x) (cadr (cddddr (cddddr x))))
345
346(define (car+cdr x) (values (car x) (cdr x)))
347
348(define (take x i)
349 (let lp ((n i) (l x) (acc '()))
e800aa04 350 (if (<= n 0)
e9680547
MG
351 (reverse! acc)
352 (lp (- n 1) (cdr l) (cons (car l) acc)))))
353(define (drop x i)
354 (let lp ((n i) (l x))
e800aa04 355 (if (<= n 0)
e9680547
MG
356 l
357 (lp (- n 1) (cdr l)))))
358(define (take-right flist i)
359 (let lp ((n i) (l flist))
e800aa04 360 (if (<= n 0)
e9680547
MG
361 (let lp0 ((s flist) (l l))
362 (if (null? l)
363 s
364 (lp0 (cdr s) (cdr l))))
365 (lp (- n 1) (cdr l)))))
366
367(define (drop-right flist i)
368 (let lp ((n i) (l flist))
e800aa04 369 (if (<= n 0)
e9680547
MG
370 (let lp0 ((s flist) (l l) (acc '()))
371 (if (null? l)
372 (reverse! acc)
373 (lp0 (cdr s) (cdr l) (cons (car s) acc))))
374 (lp (- n 1) (cdr l)))))
375
376(define (take! x i)
e800aa04 377 (if (<= i 0)
e9680547
MG
378 '()
379 (let lp ((n (- i 1)) (l x))
e800aa04 380 (if (<= n 0)
e9680547
MG
381 (begin
382 (set-cdr! l '())
383 x)
384 (lp (- n 1) (cdr l))))))
385
386(define (drop-right! flist i)
e800aa04 387 (if (<= i 0)
e9680547
MG
388 flist
389 (let lp ((n (+ i 1)) (l flist))
e800aa04 390 (if (<= n 0)
e9680547
MG
391 (let lp0 ((s flist) (l l))
392 (if (null? l)
393 (begin
394 (set-cdr! s '())
395 flist)
396 (lp0 (cdr s) (cdr l))))
397 (if (null? l)
398 '()
399 (lp (- n 1) (cdr l)))))))
400
401(define (split-at x i)
402 (let lp ((l x) (n i) (acc '()))
e800aa04 403 (if (<= n 0)
e9680547
MG
404 (values (reverse! acc) l)
405 (lp (cdr l) (- n 1) (cons (car l) acc)))))
406
407(define (split-at! x i)
e800aa04 408 (if (<= i 0)
e9680547
MG
409 (values '() x)
410 (let lp ((l x) (n (- i 1)))
e800aa04 411 (if (<= n 0)
e9680547
MG
412 (let ((tmp (cdr l)))
413 (set-cdr! l '())
414 (values x tmp))
415 (lp (cdr l) (- n 1))))))
416
417(define (last pair)
418 (car (last-pair pair)))
419
420;;; Miscelleneous: length, append, concatenate, reverse, zip & count
421
422(define (length+ clist)
423 (if (null? clist)
424 0
425 (let lp ((hare (cdr clist)) (tortoise clist) (l 1))
426 (if (null? hare)
427 l
428 (let ((hare (cdr hare)))
429 (if (null? hare)
430 (+ l 1)
431 (if (eq? hare tortoise)
432 #f
433 (lp (cdr hare) (cdr tortoise) (+ l 2)))))))))
434
435(define (concatenate l-o-l)
436 (let lp ((l l-o-l) (acc '()))
437 (if (null? l)
438 (reverse! acc)
439 (let lp0 ((ll (car l)) (acc acc))
440 (if (null? ll)
441 (lp (cdr l) acc)
442 (lp0 (cdr ll) (cons (car ll) acc)))))))
443
444(define (concatenate! l-o-l)
445 (let lp0 ((l-o-l l-o-l))
446 (cond
447 ((null? l-o-l)
448 '())
449 ((null? (car l-o-l))
450 (lp0 (cdr l-o-l)))
451 (else
452 (let ((result (car l-o-l)) (tail (last-pair (car l-o-l))))
453 (let lp ((l (cdr l-o-l)) (ntail tail))
454 (if (null? l)
455 result
456 (begin
457 (set-cdr! ntail (car l))
458 (lp (cdr l) (last-pair ntail))))))))))
459
460
461(define (append-reverse rev-head tail)
462 (let lp ((l rev-head) (acc tail))
463 (if (null? l)
464 acc
465 (lp (cdr l) (cons (car l) acc)))))
466
467(define (append-reverse! rev-head tail)
468 (append-reverse rev-head tail)) ; XXX:optimize
469
470(define (zip clist1 . rest)
471 (let lp ((l (cons clist1 rest)) (acc '()))
472 (if (any null? l)
473 (reverse! acc)
cef248dd 474 (lp (map1 cdr l) (cons (map1 car l) acc)))))
e9680547
MG
475
476
477(define (unzip1 l)
cef248dd 478 (map1 first l))
e9680547 479(define (unzip2 l)
cef248dd 480 (values (map1 first l) (map1 second l)))
e9680547 481(define (unzip3 l)
cef248dd 482 (values (map1 first l) (map1 second l) (map1 third l)))
e9680547 483(define (unzip4 l)
cef248dd 484 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)))
e9680547 485(define (unzip5 l)
cef248dd
MG
486 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)
487 (map1 fifth l)))
e9680547
MG
488
489(define (count pred clist1 . rest)
490 (if (null? rest)
491 (count1 pred clist1)
492 (let lp ((lists (cons clist1 rest)))
493 (cond ((any1 null? lists)
494 0)
495 (else
cef248dd
MG
496 (if (apply pred (map1 car lists))
497 (+ 1 (lp (map1 cdr lists)))
498 (lp (map1 cdr lists))))))))
e9680547
MG
499
500(define (count1 pred clist)
501 (if (null? clist)
502 0
503 (if (pred (car clist))
504 (+ 1 (count1 pred (cdr clist)))
505 (count1 pred (cdr clist)))))
506
507;;; Fold, unfold & map
508
509(define (fold kons knil list1 . rest)
510 (if (null? rest)
511 (let f ((knil knil) (list1 list1))
512 (if (null? list1)
513 knil
514 (f (kons (car list1) knil) (cdr list1))))
515 (let f ((knil knil) (lists (cons list1 rest)))
516 (if (any null? lists)
517 knil
cef248dd
MG
518 (let ((cars (map1 car lists))
519 (cdrs (map1 cdr lists)))
563058ef 520 (f (apply kons (append! cars (list knil))) cdrs))))))
e9680547
MG
521
522(define (fold-right kons knil clist1 . rest)
523 (if (null? rest)
524 (let f ((list1 clist1))
525 (if (null? list1)
526 knil
527 (kons (car list1) (f (cdr list1)))))
528 (let f ((lists (cons clist1 rest)))
529 (if (any null? lists)
530 knil
cef248dd 531 (apply kons (append! (map1 car lists) (list (f (map1 cdr lists)))))))))
e9680547
MG
532
533(define (pair-fold kons knil clist1 . rest)
534 (if (null? rest)
535 (let f ((knil knil) (list1 clist1))
536 (if (null? list1)
537 knil
538 (let ((tail (cdr list1)))
539 (f (kons list1 knil) tail))))
540 (let f ((knil knil) (lists (cons clist1 rest)))
541 (if (any null? lists)
542 knil
cef248dd 543 (let ((tails (map1 cdr lists)))
563058ef 544 (f (apply kons (append! lists (list knil))) tails))))))
e9680547
MG
545
546
547(define (pair-fold-right kons knil clist1 . rest)
548 (if (null? rest)
549 (let f ((list1 clist1))
550 (if (null? list1)
551 knil
552 (kons list1 (f (cdr list1)))))
553 (let f ((lists (cons clist1 rest)))
554 (if (any null? lists)
555 knil
cef248dd 556 (apply kons (append! lists (list (f (map1 cdr lists)))))))))
e9680547
MG
557
558(define (unfold p f g seed . rest)
559 (let ((tail-gen (if (pair? rest)
560 (if (pair? (cdr rest))
561 (scm-error 'wrong-number-of-args
562 "unfold" "too many arguments" '() '())
563 (car rest))
564 (lambda (x) '()))))
565 (let uf ((seed seed))
566 (if (p seed)
567 (tail-gen seed)
568 (cons (f seed)
569 (uf (g seed)))))))
570
571(define (unfold-right p f g seed . rest)
572 (let ((tail (if (pair? rest)
573 (if (pair? (cdr rest))
574 (scm-error 'wrong-number-of-args
575 "unfold-right" "too many arguments" '()
576 '())
577 (car rest))
578 '())))
579 (let uf ((seed seed) (lis tail))
580 (if (p seed)
581 lis
582 (uf (g seed) (cons (f seed) lis))))))
583
584(define (reduce f ridentity lst)
585 (fold f ridentity lst))
586
587(define (reduce-right f ridentity lst)
588 (fold-right f ridentity lst))
589
cef248dd
MG
590
591;; Internal helper procedure. Map `f' over the single list `ls'.
592;;
593(define (map1 f ls)
594 (let lp ((l ls))
595 (if (null? l)
596 '()
597 (cons (f (car l)) (lp (cdr l))))))
598
599;; This `map' is extended from the standard `map'. It allows argument
600;; lists of different length, so that the shortest list determines the
601;; number of elements processed.
602;;
603(define (map f list1 . rest)
604 (if (null? rest)
605 (map1 f list1)
606 (let lp ((l (cons list1 rest)))
607 (if (any1 null? l)
608 '()
609 (cons (apply f (map1 car l)) (lp (map1 cdr l)))))))
610
6d52dbf2
GH
611;; extended to lists of unequal length.
612(define map-in-order map)
cef248dd
MG
613
614;; This `for-each' is extended from the standard `for-each'. It
615;; allows argument lists of different length, so that the shortest
616;; list determines the number of elements processed.
617;;
618(define (for-each f list1 . rest)
619 (if (null? rest)
620 (let lp ((l list1))
621 (if (null? l)
622 (if #f #f) ; Return unspecified value.
623 (begin
624 (f (car l))
625 (lp (cdr l)))))
626 (let lp ((l (cons list1 rest)))
627 (if (any1 null? l)
628 (if #f #f)
629 (begin
630 (apply f (map1 car l))
631 (lp (map1 cdr l)))))))
632
633
e9680547
MG
634(define (append-map f clist1 . rest)
635 (if (null? rest)
636 (let lp ((l clist1))
637 (if (null? l)
638 '()
639 (append (f (car l)) (lp (cdr l)))))
640 (let lp ((l (cons clist1 rest)))
641 (if (any1 null? l)
642 '()
cef248dd
MG
643 (append (apply f (map1 car l)) (lp (map1 cdr l)))))))
644
e9680547
MG
645
646(define (append-map! f clist1 . rest)
647 (if (null? rest)
648 (let lp ((l clist1))
649 (if (null? l)
650 '()
651 (append! (f (car l)) (lp (cdr l)))))
652 (let lp ((l (cons clist1 rest)))
653 (if (any1 null? l)
654 '()
cef248dd 655 (append! (apply f (map1 car l)) (lp (map1 cdr l)))))))
e9680547
MG
656
657(define (map! f list1 . rest)
658 (if (null? rest)
659 (let lp ((l list1))
660 (if (null? l)
661 '()
662 (begin
663 (set-car! l (f (car l)))
664 (set-cdr! l (lp (cdr l)))
665 l)))
666 (let lp ((l (cons list1 rest)) (res list1))
667 (if (any1 null? l)
668 '()
669 (begin
cef248dd
MG
670 (set-car! res (apply f (map1 car l)))
671 (set-cdr! res (lp (map1 cdr l) (cdr res)))
e9680547
MG
672 res)))))
673
674(define (pair-for-each f clist1 . rest)
675 (if (null? rest)
676 (let lp ((l clist1))
677 (if (null? l)
678 (if #f #f)
679 (begin
680 (f l)
681 (lp (cdr l)))))
682 (let lp ((l (cons clist1 rest)))
683 (if (any1 null? l)
684 (if #f #f)
685 (begin
686 (apply f l)
cef248dd 687 (lp (map1 cdr l)))))))
e9680547
MG
688
689(define (filter-map f clist1 . rest)
690 (if (null? rest)
691 (let lp ((l clist1))
692 (if (null? l)
693 '()
694 (let ((res (f (car l))))
695 (if res
696 (cons res (lp (cdr l)))
697 (lp (cdr l))))))
698 (let lp ((l (cons clist1 rest)))
699 (if (any1 null? l)
700 '()
cef248dd 701 (let ((res (apply f (map1 car l))))
e9680547 702 (if res
cef248dd
MG
703 (cons res (lp (map1 cdr l)))
704 (lp (map1 cdr l))))))))
e9680547
MG
705
706;;; Filtering & partitioning
707
708(define (filter pred list)
709 (if (null? list)
710 '()
711 (if (pred (car list))
712 (cons (car list) (filter pred (cdr list)))
713 (filter pred (cdr list)))))
714
715(define (partition pred list)
716 (if (null? list)
717 (values '() '())
718 (if (pred (car list))
719 (receive (in out) (partition pred (cdr list))
720 (values (cons (car list) in) out))
721 (receive (in out) (partition pred (cdr list))
722 (values in (cons (car list) out))))))
723
724(define (remove pred list)
725 (if (null? list)
726 '()
727 (if (pred (car list))
728 (remove pred (cdr list))
729 (cons (car list) (remove pred (cdr list))))))
730
731(define (filter! pred list)
732 (filter pred list)) ; XXX:optimize
733
734(define (partition! pred list)
735 (partition pred list)) ; XXX:optimize
736
737(define (remove! pred list)
738 (remove pred list)) ; XXX:optimize
739
740;;; Searching
741
742(define (find pred clist)
743 (if (null? clist)
744 #f
745 (if (pred (car clist))
746 (car clist)
747 (find pred (cdr clist)))))
748
749(define (find-tail pred clist)
750 (if (null? clist)
751 #f
752 (if (pred (car clist))
753 clist
754 (find-tail pred (cdr clist)))))
755
756(define (take-while pred clist)
757 (if (null? clist)
758 '()
759 (if (pred (car clist))
760 (cons (car clist) (take-while pred (cdr clist)))
761 '())))
762
763(define (take-while! pred clist)
764 (take-while pred clist)) ; XXX:optimize
765
766(define (drop-while pred clist)
767 (if (null? clist)
768 '()
769 (if (pred (car clist))
770 (drop-while pred (cdr clist))
771 clist)))
772
773(define (span pred clist)
774 (if (null? clist)
775 (values '() '())
776 (if (pred (car clist))
777 (receive (first last) (span pred (cdr clist))
778 (values (cons (car clist) first) last))
779 (values '() clist))))
780
781(define (span! pred list)
782 (span pred list)) ; XXX:optimize
783
784(define (break pred clist)
785 (if (null? clist)
786 (values '() '())
787 (if (pred (car clist))
788 (values '() clist)
789 (receive (first last) (break pred (cdr clist))
790 (values (cons (car clist) first) last)))))
791
792(define (break! pred list)
793 (break pred list)) ; XXX:optimize
794
795(define (any pred ls . lists)
796 (if (null? lists)
797 (any1 pred ls)
798 (let lp ((lists (cons ls lists)))
799 (cond ((any1 null? lists)
800 #f)
cef248dd
MG
801 ((any1 null? (map1 cdr lists))
802 (apply pred (map1 car lists)))
e9680547 803 (else
cef248dd 804 (or (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
e9680547
MG
805
806(define (any1 pred ls)
807 (let lp ((ls ls))
808 (cond ((null? ls)
809 #f)
810 ((null? (cdr ls))
811 (pred (car ls)))
812 (else
813 (or (pred (car ls)) (lp (cdr ls)))))))
814
815(define (every pred ls . lists)
816 (if (null? lists)
817 (every1 pred ls)
818 (let lp ((lists (cons ls lists)))
819 (cond ((any1 null? lists)
820 #t)
cef248dd
MG
821 ((any1 null? (map1 cdr lists))
822 (apply pred (map1 car lists)))
e9680547 823 (else
cef248dd 824 (and (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
e9680547
MG
825
826(define (every1 pred ls)
827 (let lp ((ls ls))
828 (cond ((null? ls)
829 #t)
830 ((null? (cdr ls))
831 (pred (car ls)))
832 (else
833 (and (pred (car ls)) (lp (cdr ls)))))))
834
835(define (list-index pred clist1 . rest)
836 (if (null? rest)
837 (let lp ((l clist1) (i 0))
838 (if (null? l)
839 #f
840 (if (pred (car l))
841 i
842 (lp (cdr l) (+ i 1)))))
843 (let lp ((lists (cons clist1 rest)) (i 0))
844 (cond ((any1 null? lists)
845 #f)
cef248dd 846 ((apply pred (map1 car lists)) i)
e9680547 847 (else
cef248dd 848 (lp (map1 cdr lists) (+ i 1)))))))
e9680547
MG
849
850(define (member x list . rest)
851 (let ((l= (if (pair? rest) (car rest) equal?)))
852 (let lp ((l list))
853 (if (null? l)
854 #f
563058ef 855 (if (l= x (car l))
e9680547
MG
856 l
857 (lp (cdr l)))))))
858
859;;; Deletion
860
861(define (delete x list . rest)
862 (let ((l= (if (pair? rest) (car rest) equal?)))
863 (let lp ((l list))
864 (if (null? l)
865 '()
866 (if (l= (car l) x)
867 (lp (cdr l))
868 (cons (car l) (lp (cdr l))))))))
869
870(define (delete! x list . rest)
871 (let ((l= (if (pair? rest) (car rest) equal?)))
872 (delete x list l=))) ; XXX:optimize
873
874(define (delete-duplicates list . rest)
875 (let ((l= (if (pair? rest) (car rest) equal?)))
876 (let lp0 ((l1 list))
877 (if (null? l1)
878 '()
879 (if (let lp1 ((l2 (cdr l1)))
880 (if (null? l2)
881 #f
882 (if (l= (car l1) (car l2))
883 #t
884 (lp1 (cdr l2)))))
885 (lp0 (cdr l1))
563058ef
MG
886 (cons (car l1) (lp0 (cdr l1))))))))
887
888(define (delete-duplicates list . rest)
889 (let ((l= (if (pair? rest) (car rest) equal?)))
890 (let lp ((list list))
891 (if (null? list)
892 '()
893 (cons (car list) (lp (delete (car list) (cdr list) l=)))))))
e9680547
MG
894
895(define (delete-duplicates! list . rest)
896 (let ((l= (if (pair? rest) (car rest) equal?)))
897 (delete-duplicates list l=))) ; XXX:optimize
898
899;;; Association lists
900
901(define (assoc key alist . rest)
902 (let ((k= (if (pair? rest) (car rest) equal?)))
903 (let lp ((a alist))
904 (if (null? a)
905 #f
563058ef 906 (if (k= key (caar a))
e9680547
MG
907 (car a)
908 (lp (cdr a)))))))
909
910(define (alist-cons key datum alist)
911 (acons key datum alist))
912
913(define (alist-copy alist)
914 (let lp ((a alist))
915 (if (null? a)
916 '()
563058ef 917 (acons (caar a) (cdar a) (lp (cdr a))))))
e9680547
MG
918
919(define (alist-delete key alist . rest)
920 (let ((k= (if (pair? rest) (car rest) equal?)))
921 (let lp ((a alist))
922 (if (null? a)
923 '()
924 (if (k= (caar a) key)
925 (lp (cdr a))
926 (cons (car a) (lp (cdr a))))))))
927
928(define (alist-delete! key alist . rest)
929 (let ((k= (if (pair? rest) (car rest) equal?)))
930 (alist-delete key alist k=))) ; XXX:optimize
931
932;;; Set operations on lists
933
934(define (lset<= = . rest)
935 (if (null? rest)
936 #t
937 (let lp ((f (car rest)) (r (cdr rest)))
938 (or (null? r)
939 (and (every (lambda (el) (member el (car r) =)) f)
940 (lp (car r) (cdr r)))))))
941
942(define (lset= = list1 . rest)
943 (if (null? rest)
944 #t
945 (let lp ((f list1) (r rest))
946 (or (null? r)
947 (and (every (lambda (el) (member el (car r) =)) f)
948 (every (lambda (el) (member el f =)) (car r))
949 (lp (car r) (cdr r)))))))
950
951(define (lset-adjoin = list . rest)
952 (let lp ((l rest) (acc list))
953 (if (null? l)
954 acc
955 (if (member (car l) acc)
956 (lp (cdr l) acc)
957 (lp (cdr l) (cons (car l) acc))))))
958
959(define (lset-union = . rest)
960 (let lp0 ((l rest) (acc '()))
961 (if (null? l)
962 (reverse! acc)
963 (let lp1 ((ll (car l)) (acc acc))
964 (if (null? ll)
965 (lp0 (cdr l) acc)
966 (if (member (car ll) acc =)
967 (lp1 (cdr ll) acc)
968 (lp1 (cdr ll) (cons (car ll) acc))))))))
969
970(define (lset-intersection = list1 . rest)
971 (let lp ((l list1) (acc '()))
972 (if (null? l)
973 (reverse! acc)
974 (if (every (lambda (ll) (member (car l) ll =)) rest)
975 (lp (cdr l) (cons (car l) acc))
976 (lp (cdr l) acc)))))
977
978(define (lset-difference = list1 . rest)
979 (if (null? rest)
980 list1
981 (let lp ((l list1) (acc '()))
982 (if (null? l)
983 (reverse! acc)
984 (if (any (lambda (ll) (member (car l) ll =)) rest)
985 (lp (cdr l) acc)
986 (lp (cdr l) (cons (car l) acc)))))))
987
988;(define (fold kons knil list1 . rest)
989
990(define (lset-xor = . rest)
991 (fold (lambda (lst res)
992 (let lp ((l lst) (acc '()))
993 (if (null? l)
994 (let lp0 ((r res) (acc acc))
995 (if (null? r)
996 (reverse! acc)
997 (if (member (car r) lst =)
998 (lp0 (cdr r) acc)
999 (lp0 (cdr r) (cons (car r) acc)))))
1000 (if (member (car l) res =)
1001 (lp (cdr l) acc)
1002 (lp (cdr l) (cons (car l) acc))))))
1003 '()
1004 rest))
1005
1006(define (lset-diff+intersection = list1 . rest)
1007 (let lp ((l list1) (accd '()) (acci '()))
1008 (if (null? l)
1009 (values (reverse! accd) (reverse! acci))
1010 (let ((appears (every (lambda (ll) (member (car l) ll =)) rest)))
1011 (if appears
1012 (lp (cdr l) accd (cons (car l) acci))
1013 (lp (cdr l) (cons (car l) accd) acci))))))
1014
1015
1016(define (lset-union! = . rest)
1017 (apply lset-union = rest)) ; XXX:optimize
1018
1019(define (lset-intersection! = list1 . rest)
1020 (apply lset-intersection = list1 rest)) ; XXX:optimize
1021
1022(define (lset-difference! = list1 . rest)
1023 (apply lset-difference = list1 rest)) ; XXX:optimize
1024
1025(define (lset-xor! = . rest)
1026 (apply lset-xor = rest)) ; XXX:optimize
1027
1028(define (lset-diff+intersection! = list1 . rest)
1029 (apply lset-diff+intersection = list1 rest)) ; XXX:optimize
fc183bc8
GH
1030
1031;; extended versions of builtin procedures. exporting is delayed until the
1032;; new bindings have been created.
6d52dbf2 1033(export iota map map-in-order for-each list-index member delete delete! assoc)