(make-shared-array): Add failing case shared of shared.
[bpt/guile.git] / srfi / srfi-1.scm
CommitLineData
6be07c52
TTN
1;;; srfi-1.scm --- List Library
2
8e15d7f5 3;; Copyright (C) 2001, 2002, 2003, 2004, 2005 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)
f595ccfe 36 :export (
e9680547
MG
37;;; Constructors
38 ;; cons <= in the core
39 ;; list <= in the core
40 xcons
41 ;; cons* <= in the core
42 ;; make-list <= in the core
43 list-tabulate
d61261f0 44 list-copy
e9680547 45 circular-list
f595ccfe 46 ;; iota ; Extended.
e9680547
MG
47
48;;; Predicates
49 proper-list?
50 circular-list?
51 dotted-list?
52 ;; pair? <= in the core
53 ;; null? <= in the core
54 null-list?
55 not-pair?
56 list=
57
58;;; Selectors
59 ;; car <= in the core
60 ;; cdr <= in the core
61 ;; caar <= in the core
62 ;; cadr <= in the core
63 ;; cdar <= in the core
2a326497 64 ;; cddr <= in the core
e9680547
MG
65 ;; caaar <= in the core
66 ;; caadr <= in the core
67 ;; cadar <= in the core
68 ;; caddr <= in the core
69 ;; cdaar <= in the core
70 ;; cdadr <= in the core
71 ;; cddar <= in the core
72 ;; cdddr <= in the core
73 ;; caaaar <= in the core
74 ;; caaadr <= in the core
75 ;; caadar <= in the core
76 ;; caaddr <= in the core
77 ;; cadaar <= in the core
78 ;; cadadr <= in the core
79 ;; caddar <= in the core
80 ;; cadddr <= in the core
81 ;; cdaaar <= in the core
82 ;; cdaadr <= in the core
83 ;; cdadar <= in the core
84 ;; cdaddr <= in the core
85 ;; cddaar <= in the core
86 ;; cddadr <= in the core
87 ;; cdddar <= in the core
88 ;; cddddr <= in the core
89 ;; list-ref <= in the core
90 first
91 second
92 third
93 fourth
94 fifth
95 sixth
96 seventh
97 eighth
98 ninth
99 tenth
100 car+cdr
101 take
102 drop
103 take-right
104 drop-right
105 take!
106 drop-right!
107 split-at
108 split-at!
109 last
110 ;; last-pair <= in the core
111
112;;; Miscelleneous: length, append, concatenate, reverse, zip & count
113 ;; length <= in the core
114 length+
115 ;; append <= in the core
116 ;; append! <= in the core
117 concatenate
118 concatenate!
119 ;; reverse <= in the core
120 ;; reverse! <= in the core
121 append-reverse
122 append-reverse!
123 zip
124 unzip1
125 unzip2
126 unzip3
127 unzip4
128 unzip5
129 count
130
131;;; Fold, unfold & map
132 fold
133 fold-right
134 pair-fold
135 pair-fold-right
136 reduce
137 reduce-right
138 unfold
139 unfold-right
f595ccfe
MD
140 ;; map ; Extended.
141 ;; for-each ; Extended.
e9680547
MG
142 append-map
143 append-map!
144 map!
f595ccfe 145 ;; map-in-order ; Extended.
e9680547
MG
146 pair-for-each
147 filter-map
148
149;;; Filtering & partitioning
c614a00b 150 ;; filter <= in the core
e9680547
MG
151 partition
152 remove
c614a00b 153 ;; filter! <= in the core
e9680547
MG
154 partition!
155 remove!
156
157;;; Searching
158 find
159 find-tail
160 take-while
161 take-while!
162 drop-while
163 span
164 span!
165 break
166 break!
167 any
168 every
f595ccfe
MD
169 ;; list-index ; Extended.
170 ;; member ; Extended.
e9680547
MG
171 ;; memq <= in the core
172 ;; memv <= in the core
173
174;;; Deletion
f595ccfe
MD
175 ;; delete ; Extended.
176 ;; delete! ; Extended.
e9680547
MG
177 delete-duplicates
178 delete-duplicates!
179
180;;; Association lists
f595ccfe 181 ;; assoc ; Extended.
e9680547
MG
182 ;; assq <= in the core
183 ;; assv <= in the core
184 alist-cons
185 alist-copy
186 alist-delete
187 alist-delete!
188
189;;; Set operations on lists
190 lset<=
191 lset=
192 lset-adjoin
193 lset-union
194 lset-intersection
195 lset-difference
196 lset-xor
197 lset-diff+intersection
198 lset-union!
199 lset-intersection!
200 lset-difference!
201 lset-xor!
202 lset-diff+intersection!
203
204;;; Primitive side-effects
205 ;; set-car! <= in the core
206 ;; set-cdr! <= in the core
207 )
d61261f0 208 :re-export (cons list cons* make-list pair? null?
b8b0abf0
MD
209 car cdr caar cadr cdar cddr
210 caaar caadr cadar caddr cdaar cdadr cddar cdddr
211 caaaar caaadr caadar caaddr cadaar cadadr caddar cadddr
212 cdaaar cdaadr cdadar cdaddr cddaar cddadr cdddar cddddr
213 list-ref last-pair length append append! reverse reverse!
214 filter filter! memq memv assq assv set-car! set-cdr!)
d61261f0 215 :replace (iota map for-each map-in-order list-copy list-index member
f595ccfe
MD
216 delete delete! assoc)
217 )
e9680547
MG
218
219(cond-expand-provide (current-module) '(srfi-1))
220
ee6aac97
MD
221;; Load the compiled primitives from the shared library.
222;;
bd453596 223(load-extension "libguile-srfi-srfi-1-v-2" "scm_init_srfi_1")
ee6aac97
MD
224
225
e9680547
MG
226;;; Constructors
227
228(define (xcons d a)
229 (cons a d))
230
5753f02f
GH
231;; internal helper, similar to (scsh utilities) check-arg.
232(define (check-arg-type pred arg caller)
233 (if (pred arg)
234 arg
235 (scm-error 'wrong-type-arg caller
236 "Wrong type argument: ~S" (list arg) '())))
237
238;; the srfi spec doesn't seem to forbid inexact integers.
239(define (non-negative-integer? x) (and (integer? x) (>= x 0)))
240
e9680547 241(define (list-tabulate n init-proc)
5753f02f 242 (check-arg-type non-negative-integer? n "list-tabulate")
e9680547 243 (let lp ((n n) (acc '()))
018adcae 244 (if (<= n 0)
e9680547
MG
245 acc
246 (lp (- n 1) (cons (init-proc (- n 1)) acc)))))
247
2d411b05
KR
248(define (circular-list elt1 . elts)
249 (set! elts (cons elt1 elts))
250 (set-cdr! (last-pair elts) elts)
251 elts)
e9680547
MG
252
253(define (iota count . rest)
5753f02f 254 (check-arg-type non-negative-integer? count "iota")
e9680547
MG
255 (let ((start (if (pair? rest) (car rest) 0))
256 (step (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 1)))
257 (let lp ((n 0) (acc '()))
258 (if (= n count)
259 (reverse! acc)
260 (lp (+ n 1) (cons (+ start (* n step)) acc))))))
261
262;;; Predicates
263
264(define (proper-list? x)
265 (list? x))
266
267(define (circular-list? x)
268 (if (not-pair? x)
269 #f
270 (let lp ((hare (cdr x)) (tortoise x))
271 (if (not-pair? hare)
272 #f
273 (let ((hare (cdr hare)))
274 (if (not-pair? hare)
275 #f
276 (if (eq? hare tortoise)
277 #t
278 (lp (cdr hare) (cdr tortoise)))))))))
279
280(define (dotted-list? x)
281 (cond
282 ((null? x) #f)
283 ((not-pair? x) #t)
284 (else
285 (let lp ((hare (cdr x)) (tortoise x))
286 (cond
287 ((null? hare) #f)
288 ((not-pair? hare) #t)
289 (else
290 (let ((hare (cdr hare)))
513a3bd7 291 (cond
e9680547
MG
292 ((null? hare) #f)
293 ((not-pair? hare) #t)
294 ((eq? hare tortoise) #f)
295 (else
296 (lp (cdr hare) (cdr tortoise)))))))))))
297
298(define (null-list? x)
299 (cond
513a3bd7 300 ((proper-list? x)
e9680547
MG
301 (null? x))
302 ((circular-list? x)
303 #f)
304 (else
305 (error "not a proper list in null-list?"))))
306
307(define (not-pair? x)
308 (not (pair? x)))
309
310(define (list= elt= . rest)
311 (define (lists-equal a b)
312 (let lp ((a a) (b b))
313 (cond ((null? a)
314 (null? b))
315 ((null? b)
316 #f)
317 (else
318 (and (elt= (car a) (car b))
319 (lp (cdr a) (cdr b)))))))
320 (or (null? rest)
1bc8745f
KR
321 (let lp ((lists rest))
322 (or (null? (cdr lists))
323 (and (lists-equal (car lists) (cadr lists))
324 (lp (cdr lists)))))))
e9680547
MG
325
326;;; Selectors
327
328(define first car)
329(define second cadr)
330(define third caddr)
331(define fourth cadddr)
e9680547
MG
332
333(define (car+cdr x) (values (car x) (cdr x)))
334
4dd6bd84
KR
335(define take list-head)
336(define drop list-tail)
337
e9680547
MG
338(define (last pair)
339 (car (last-pair pair)))
340
341;;; Miscelleneous: length, append, concatenate, reverse, zip & count
342
e9680547
MG
343(define (append-reverse rev-head tail)
344 (let lp ((l rev-head) (acc tail))
345 (if (null? l)
346 acc
347 (lp (cdr l) (cons (car l) acc)))))
348
349(define (append-reverse! rev-head tail)
350 (append-reverse rev-head tail)) ; XXX:optimize
351
352(define (zip clist1 . rest)
353 (let lp ((l (cons clist1 rest)) (acc '()))
354 (if (any null? l)
355 (reverse! acc)
cef248dd 356 (lp (map1 cdr l) (cons (map1 car l) acc)))))
513a3bd7 357
e9680547
MG
358
359(define (unzip1 l)
cef248dd 360 (map1 first l))
e9680547 361(define (unzip2 l)
cef248dd 362 (values (map1 first l) (map1 second l)))
e9680547 363(define (unzip3 l)
cef248dd 364 (values (map1 first l) (map1 second l) (map1 third l)))
e9680547 365(define (unzip4 l)
cef248dd 366 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)))
e9680547 367(define (unzip5 l)
cef248dd
MG
368 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)
369 (map1 fifth l)))
e9680547 370
e9680547
MG
371;;; Fold, unfold & map
372
373(define (fold kons knil list1 . rest)
374 (if (null? rest)
375 (let f ((knil knil) (list1 list1))
376 (if (null? list1)
377 knil
378 (f (kons (car list1) knil) (cdr list1))))
379 (let f ((knil knil) (lists (cons list1 rest)))
380 (if (any null? lists)
381 knil
cef248dd
MG
382 (let ((cars (map1 car lists))
383 (cdrs (map1 cdr lists)))
563058ef 384 (f (apply kons (append! cars (list knil))) cdrs))))))
e9680547
MG
385
386(define (fold-right kons knil clist1 . rest)
387 (if (null? rest)
388 (let f ((list1 clist1))
389 (if (null? list1)
390 knil
391 (kons (car list1) (f (cdr list1)))))
392 (let f ((lists (cons clist1 rest)))
393 (if (any null? lists)
394 knil
cef248dd 395 (apply kons (append! (map1 car lists) (list (f (map1 cdr lists)))))))))
e9680547
MG
396
397(define (pair-fold kons knil clist1 . rest)
398 (if (null? rest)
399 (let f ((knil knil) (list1 clist1))
400 (if (null? list1)
401 knil
402 (let ((tail (cdr list1)))
403 (f (kons list1 knil) tail))))
404 (let f ((knil knil) (lists (cons clist1 rest)))
405 (if (any null? lists)
406 knil
cef248dd 407 (let ((tails (map1 cdr lists)))
563058ef 408 (f (apply kons (append! lists (list knil))) tails))))))
e9680547
MG
409
410
411(define (pair-fold-right kons knil clist1 . rest)
412 (if (null? rest)
413 (let f ((list1 clist1))
414 (if (null? list1)
415 knil
416 (kons list1 (f (cdr list1)))))
417 (let f ((lists (cons clist1 rest)))
418 (if (any null? lists)
419 knil
cef248dd 420 (apply kons (append! lists (list (f (map1 cdr lists)))))))))
e9680547
MG
421
422(define (unfold p f g seed . rest)
423 (let ((tail-gen (if (pair? rest)
424 (if (pair? (cdr rest))
425 (scm-error 'wrong-number-of-args
426 "unfold" "too many arguments" '() '())
427 (car rest))
428 (lambda (x) '()))))
429 (let uf ((seed seed))
430 (if (p seed)
431 (tail-gen seed)
432 (cons (f seed)
433 (uf (g seed)))))))
434
435(define (unfold-right p f g seed . rest)
436 (let ((tail (if (pair? rest)
437 (if (pair? (cdr rest))
438 (scm-error 'wrong-number-of-args
439 "unfold-right" "too many arguments" '()
440 '())
441 (car rest))
442 '())))
443 (let uf ((seed seed) (lis tail))
444 (if (p seed)
445 lis
446 (uf (g seed) (cons (f seed) lis))))))
447
cef248dd
MG
448
449;; Internal helper procedure. Map `f' over the single list `ls'.
450;;
7692d26b 451(define map1 map)
cef248dd 452
e9680547 453(define (append-map f clist1 . rest)
8b981efd 454 (concatenate (apply map f clist1 rest)))
558d5c03 455
e9680547 456(define (append-map! f clist1 . rest)
8b981efd 457 (concatenate! (apply map f clist1 rest)))
e9680547 458
c6424115
KR
459;; OPTIMIZE-ME: Re-use cons cells of list1
460(define map! map)
e9680547
MG
461
462(define (pair-for-each f clist1 . rest)
463 (if (null? rest)
464 (let lp ((l clist1))
465 (if (null? l)
466 (if #f #f)
467 (begin
468 (f l)
469 (lp (cdr l)))))
470 (let lp ((l (cons clist1 rest)))
471 (if (any1 null? l)
472 (if #f #f)
473 (begin
474 (apply f l)
cef248dd 475 (lp (map1 cdr l)))))))
e9680547 476
e9680547
MG
477;;; Searching
478
e9680547
MG
479(define (any pred ls . lists)
480 (if (null? lists)
481 (any1 pred ls)
482 (let lp ((lists (cons ls lists)))
483 (cond ((any1 null? lists)
484 #f)
cef248dd
MG
485 ((any1 null? (map1 cdr lists))
486 (apply pred (map1 car lists)))
e9680547 487 (else
cef248dd 488 (or (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
e9680547
MG
489
490(define (any1 pred ls)
491 (let lp ((ls ls))
492 (cond ((null? ls)
493 #f)
494 ((null? (cdr ls))
495 (pred (car ls)))
496 (else
497 (or (pred (car ls)) (lp (cdr ls)))))))
498
499(define (every pred ls . lists)
500 (if (null? lists)
501 (every1 pred ls)
502 (let lp ((lists (cons ls lists)))
503 (cond ((any1 null? lists)
504 #t)
cef248dd
MG
505 ((any1 null? (map1 cdr lists))
506 (apply pred (map1 car lists)))
e9680547 507 (else
cef248dd 508 (and (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
e9680547
MG
509
510(define (every1 pred ls)
511 (let lp ((ls ls))
512 (cond ((null? ls)
513 #t)
514 ((null? (cdr ls))
515 (pred (car ls)))
516 (else
517 (and (pred (car ls)) (lp (cdr ls)))))))
518
519(define (list-index pred clist1 . rest)
520 (if (null? rest)
521 (let lp ((l clist1) (i 0))
522 (if (null? l)
523 #f
524 (if (pred (car l))
525 i
526 (lp (cdr l) (+ i 1)))))
527 (let lp ((lists (cons clist1 rest)) (i 0))
528 (cond ((any1 null? lists)
529 #f)
cef248dd 530 ((apply pred (map1 car lists)) i)
e9680547 531 (else
cef248dd 532 (lp (map1 cdr lists) (+ i 1)))))))
e9680547 533
e9680547
MG
534;;; Association lists
535
0b5adedd 536(define alist-cons acons)
e9680547 537
e9680547
MG
538(define (alist-delete key alist . rest)
539 (let ((k= (if (pair? rest) (car rest) equal?)))
49ae1e25 540 (let lp ((a alist) (rl '()))
e9680547 541 (if (null? a)
49ae1e25 542 (reverse! rl)
41ab236c 543 (if (k= key (caar a))
49ae1e25
KR
544 (lp (cdr a) rl)
545 (lp (cdr a) (cons (car a) rl)))))))
e9680547
MG
546
547(define (alist-delete! key alist . rest)
548 (let ((k= (if (pair? rest) (car rest) equal?)))
549 (alist-delete key alist k=))) ; XXX:optimize
550
551;;; Set operations on lists
552
553(define (lset<= = . rest)
554 (if (null? rest)
555 #t
556 (let lp ((f (car rest)) (r (cdr rest)))
557 (or (null? r)
558 (and (every (lambda (el) (member el (car r) =)) f)
559 (lp (car r) (cdr r)))))))
560
9d494a73 561(define (lset= = . rest)
e9680547
MG
562 (if (null? rest)
563 #t
9d494a73 564 (let lp ((f (car rest)) (r (cdr rest)))
e9680547
MG
565 (or (null? r)
566 (and (every (lambda (el) (member el (car r) =)) f)
600af2ed 567 (every (lambda (el) (member el f (lambda (x y) (= y x)))) (car r))
e9680547
MG
568 (lp (car r) (cdr r)))))))
569
e9680547 570(define (lset-union = . rest)
62a87500
KR
571 (let ((acc '()))
572 (for-each (lambda (lst)
573 (if (null? acc)
574 (set! acc lst)
575 (for-each (lambda (elem)
576 (if (not (member elem acc
577 (lambda (x y) (= y x))))
578 (set! acc (cons elem acc))))
579 lst)))
580 rest)
581 acc))
e9680547
MG
582
583(define (lset-intersection = list1 . rest)
584 (let lp ((l list1) (acc '()))
585 (if (null? l)
586 (reverse! acc)
587 (if (every (lambda (ll) (member (car l) ll =)) rest)
588 (lp (cdr l) (cons (car l) acc))
589 (lp (cdr l) acc)))))
590
591(define (lset-difference = list1 . rest)
592 (if (null? rest)
593 list1
594 (let lp ((l list1) (acc '()))
595 (if (null? l)
596 (reverse! acc)
597 (if (any (lambda (ll) (member (car l) ll =)) rest)
598 (lp (cdr l) acc)
599 (lp (cdr l) (cons (car l) acc)))))))
600
601;(define (fold kons knil list1 . rest)
602
603(define (lset-xor = . rest)
604 (fold (lambda (lst res)
605 (let lp ((l lst) (acc '()))
606 (if (null? l)
607 (let lp0 ((r res) (acc acc))
608 (if (null? r)
609 (reverse! acc)
610 (if (member (car r) lst =)
611 (lp0 (cdr r) acc)
612 (lp0 (cdr r) (cons (car r) acc)))))
613 (if (member (car l) res =)
614 (lp (cdr l) acc)
615 (lp (cdr l) (cons (car l) acc))))))
616 '()
617 rest))
618
619(define (lset-diff+intersection = list1 . rest)
620 (let lp ((l list1) (accd '()) (acci '()))
621 (if (null? l)
622 (values (reverse! accd) (reverse! acci))
623 (let ((appears (every (lambda (ll) (member (car l) ll =)) rest)))
624 (if appears
625 (lp (cdr l) accd (cons (car l) acci))
626 (lp (cdr l) (cons (car l) accd) acci))))))
627
628
629(define (lset-union! = . rest)
630 (apply lset-union = rest)) ; XXX:optimize
631
632(define (lset-intersection! = list1 . rest)
633 (apply lset-intersection = list1 rest)) ; XXX:optimize
634
635(define (lset-difference! = list1 . rest)
636 (apply lset-difference = list1 rest)) ; XXX:optimize
637
638(define (lset-xor! = . rest)
639 (apply lset-xor = rest)) ; XXX:optimize
640
641(define (lset-diff+intersection! = list1 . rest)
642 (apply lset-diff+intersection = list1 rest)) ; XXX:optimize
6be07c52
TTN
643
644;;; srfi-1.scm ends here