(lset-difference!): Rewrite in C.
[bpt/guile.git] / srfi / srfi-1.scm
1 ;;; srfi-1.scm --- List Library
2
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 ;;
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,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
20 ;;; Date: 2001-06-06
21
22 ;;; Commentary:
23
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.
32
33 ;;; Code:
34
35 (define-module (srfi srfi-1)
36 :export (
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
44 list-copy
45 circular-list
46 ;; iota ; Extended.
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
64 ;; cddr <= in the core
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
140 ;; map ; Extended.
141 ;; for-each ; Extended.
142 append-map
143 append-map!
144 map!
145 ;; map-in-order ; Extended.
146 pair-for-each
147 filter-map
148
149 ;;; Filtering & partitioning
150 ;; filter <= in the core
151 partition
152 remove
153 ;; filter! <= in the core
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
169 ;; list-index ; Extended.
170 ;; member ; Extended.
171 ;; memq <= in the core
172 ;; memv <= in the core
173
174 ;;; Deletion
175 ;; delete ; Extended.
176 ;; delete! ; Extended.
177 delete-duplicates
178 delete-duplicates!
179
180 ;;; Association lists
181 ;; assoc ; Extended.
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 )
208 :re-export (cons list cons* make-list pair? null?
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!)
215 :replace (iota map for-each map-in-order list-copy list-index member
216 delete delete! assoc)
217 )
218
219 (cond-expand-provide (current-module) '(srfi-1))
220
221 ;; Load the compiled primitives from the shared library.
222 ;;
223 (load-extension "libguile-srfi-srfi-1-v-2" "scm_init_srfi_1")
224
225
226 ;;; Constructors
227
228 ;; internal helper, similar to (scsh utilities) check-arg.
229 (define (check-arg-type pred arg caller)
230 (if (pred arg)
231 arg
232 (scm-error 'wrong-type-arg caller
233 "Wrong type argument: ~S" (list arg) '())))
234
235 ;; the srfi spec doesn't seem to forbid inexact integers.
236 (define (non-negative-integer? x) (and (integer? x) (>= x 0)))
237
238
239
240 (define (circular-list elt1 . elts)
241 (set! elts (cons elt1 elts))
242 (set-cdr! (last-pair elts) elts)
243 elts)
244
245 (define (iota count . rest)
246 (check-arg-type non-negative-integer? count "iota")
247 (let ((start (if (pair? rest) (car rest) 0))
248 (step (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 1)))
249 (let lp ((n 0) (acc '()))
250 (if (= n count)
251 (reverse! acc)
252 (lp (+ n 1) (cons (+ start (* n step)) acc))))))
253
254 ;;; Predicates
255
256 (define (proper-list? x)
257 (list? x))
258
259 (define (circular-list? x)
260 (if (not-pair? x)
261 #f
262 (let lp ((hare (cdr x)) (tortoise x))
263 (if (not-pair? hare)
264 #f
265 (let ((hare (cdr hare)))
266 (if (not-pair? hare)
267 #f
268 (if (eq? hare tortoise)
269 #t
270 (lp (cdr hare) (cdr tortoise)))))))))
271
272 (define (dotted-list? x)
273 (cond
274 ((null? x) #f)
275 ((not-pair? x) #t)
276 (else
277 (let lp ((hare (cdr x)) (tortoise x))
278 (cond
279 ((null? hare) #f)
280 ((not-pair? hare) #t)
281 (else
282 (let ((hare (cdr hare)))
283 (cond
284 ((null? hare) #f)
285 ((not-pair? hare) #t)
286 ((eq? hare tortoise) #f)
287 (else
288 (lp (cdr hare) (cdr tortoise)))))))))))
289
290 (define (null-list? x)
291 (cond
292 ((proper-list? x)
293 (null? x))
294 ((circular-list? x)
295 #f)
296 (else
297 (error "not a proper list in null-list?"))))
298
299 (define (list= elt= . rest)
300 (define (lists-equal a b)
301 (let lp ((a a) (b b))
302 (cond ((null? a)
303 (null? b))
304 ((null? b)
305 #f)
306 (else
307 (and (elt= (car a) (car b))
308 (lp (cdr a) (cdr b)))))))
309 (or (null? rest)
310 (let lp ((lists rest))
311 (or (null? (cdr lists))
312 (and (lists-equal (car lists) (cadr lists))
313 (lp (cdr lists)))))))
314
315 ;;; Selectors
316
317 (define first car)
318 (define second cadr)
319 (define third caddr)
320 (define fourth cadddr)
321
322 (define take list-head)
323 (define drop list-tail)
324
325 ;;; Miscelleneous: length, append, concatenate, reverse, zip & count
326
327 (define (append-reverse rev-head tail)
328 (let lp ((l rev-head) (acc tail))
329 (if (null? l)
330 acc
331 (lp (cdr l) (cons (car l) acc)))))
332
333 (define (append-reverse! rev-head tail)
334 (append-reverse rev-head tail)) ; XXX:optimize
335
336 (define (zip clist1 . rest)
337 (let lp ((l (cons clist1 rest)) (acc '()))
338 (if (any null? l)
339 (reverse! acc)
340 (lp (map1 cdr l) (cons (map1 car l) acc)))))
341
342
343 (define (unzip1 l)
344 (map1 first l))
345 (define (unzip2 l)
346 (values (map1 first l) (map1 second l)))
347 (define (unzip3 l)
348 (values (map1 first l) (map1 second l) (map1 third l)))
349 (define (unzip4 l)
350 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)))
351 (define (unzip5 l)
352 (values (map1 first l) (map1 second l) (map1 third l) (map1 fourth l)
353 (map1 fifth l)))
354
355 ;;; Fold, unfold & map
356
357 (define (fold-right kons knil clist1 . rest)
358 (if (null? rest)
359 (let f ((list1 clist1))
360 (if (null? list1)
361 knil
362 (kons (car list1) (f (cdr list1)))))
363 (let f ((lists (cons clist1 rest)))
364 (if (any null? lists)
365 knil
366 (apply kons (append! (map1 car lists) (list (f (map1 cdr lists)))))))))
367
368 (define (pair-fold kons knil clist1 . rest)
369 (if (null? rest)
370 (let f ((knil knil) (list1 clist1))
371 (if (null? list1)
372 knil
373 (let ((tail (cdr list1)))
374 (f (kons list1 knil) tail))))
375 (let f ((knil knil) (lists (cons clist1 rest)))
376 (if (any null? lists)
377 knil
378 (let ((tails (map1 cdr lists)))
379 (f (apply kons (append! lists (list knil))) tails))))))
380
381
382 (define (pair-fold-right kons knil clist1 . rest)
383 (if (null? rest)
384 (let f ((list1 clist1))
385 (if (null? list1)
386 knil
387 (kons list1 (f (cdr list1)))))
388 (let f ((lists (cons clist1 rest)))
389 (if (any null? lists)
390 knil
391 (apply kons (append! lists (list (f (map1 cdr lists)))))))))
392
393 (define (unfold p f g seed . rest)
394 (let ((tail-gen (if (pair? rest)
395 (if (pair? (cdr rest))
396 (scm-error 'wrong-number-of-args
397 "unfold" "too many arguments" '() '())
398 (car rest))
399 (lambda (x) '()))))
400 (let uf ((seed seed))
401 (if (p seed)
402 (tail-gen seed)
403 (cons (f seed)
404 (uf (g seed)))))))
405
406 (define (unfold-right p f g seed . rest)
407 (let ((tail (if (pair? rest)
408 (if (pair? (cdr rest))
409 (scm-error 'wrong-number-of-args
410 "unfold-right" "too many arguments" '()
411 '())
412 (car rest))
413 '())))
414 (let uf ((seed seed) (lis tail))
415 (if (p seed)
416 lis
417 (uf (g seed) (cons (f seed) lis))))))
418
419
420 ;; Internal helper procedure. Map `f' over the single list `ls'.
421 ;;
422 (define map1 map)
423
424 (define (append-map f clist1 . rest)
425 (concatenate (apply map f clist1 rest)))
426
427 (define (append-map! f clist1 . rest)
428 (concatenate! (apply map f clist1 rest)))
429
430 ;; OPTIMIZE-ME: Re-use cons cells of list1
431 (define map! map)
432
433 (define (pair-for-each f clist1 . rest)
434 (if (null? rest)
435 (let lp ((l clist1))
436 (if (null? l)
437 (if #f #f)
438 (begin
439 (f l)
440 (lp (cdr l)))))
441 (let lp ((l (cons clist1 rest)))
442 (if (any1 null? l)
443 (if #f #f)
444 (begin
445 (apply f l)
446 (lp (map1 cdr l)))))))
447
448 ;;; Searching
449
450 (define (any pred ls . lists)
451 (if (null? lists)
452 (any1 pred ls)
453 (let lp ((lists (cons ls lists)))
454 (cond ((any1 null? lists)
455 #f)
456 ((any1 null? (map1 cdr lists))
457 (apply pred (map1 car lists)))
458 (else
459 (or (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
460
461 (define (any1 pred ls)
462 (let lp ((ls ls))
463 (cond ((null? ls)
464 #f)
465 ((null? (cdr ls))
466 (pred (car ls)))
467 (else
468 (or (pred (car ls)) (lp (cdr ls)))))))
469
470 (define (every pred ls . lists)
471 (if (null? lists)
472 (every1 pred ls)
473 (let lp ((lists (cons ls lists)))
474 (cond ((any1 null? lists)
475 #t)
476 ((any1 null? (map1 cdr lists))
477 (apply pred (map1 car lists)))
478 (else
479 (and (apply pred (map1 car lists)) (lp (map1 cdr lists))))))))
480
481 (define (every1 pred ls)
482 (let lp ((ls ls))
483 (cond ((null? ls)
484 #t)
485 ((null? (cdr ls))
486 (pred (car ls)))
487 (else
488 (and (pred (car ls)) (lp (cdr ls)))))))
489
490 ;;; Association lists
491
492 (define alist-cons acons)
493
494 (define (alist-delete key alist . rest)
495 (let ((k= (if (pair? rest) (car rest) equal?)))
496 (let lp ((a alist) (rl '()))
497 (if (null? a)
498 (reverse! rl)
499 (if (k= key (caar a))
500 (lp (cdr a) rl)
501 (lp (cdr a) (cons (car a) rl)))))))
502
503 (define (alist-delete! key alist . rest)
504 (let ((k= (if (pair? rest) (car rest) equal?)))
505 (alist-delete key alist k=))) ; XXX:optimize
506
507 ;;; Set operations on lists
508
509 (define (lset<= = . rest)
510 (if (null? rest)
511 #t
512 (let lp ((f (car rest)) (r (cdr rest)))
513 (or (null? r)
514 (and (every (lambda (el) (member el (car r) =)) f)
515 (lp (car r) (cdr r)))))))
516
517 (define (lset= = . rest)
518 (if (null? rest)
519 #t
520 (let lp ((f (car rest)) (r (cdr rest)))
521 (or (null? r)
522 (and (every (lambda (el) (member el (car r) =)) f)
523 (every (lambda (el) (member el f (lambda (x y) (= y x)))) (car r))
524 (lp (car r) (cdr r)))))))
525
526 (define (lset-union = . rest)
527 (let ((acc '()))
528 (for-each (lambda (lst)
529 (if (null? acc)
530 (set! acc lst)
531 (for-each (lambda (elem)
532 (if (not (member elem acc
533 (lambda (x y) (= y x))))
534 (set! acc (cons elem acc))))
535 lst)))
536 rest)
537 acc))
538
539 (define (lset-intersection = list1 . rest)
540 (let lp ((l list1) (acc '()))
541 (if (null? l)
542 (reverse! acc)
543 (if (every (lambda (ll) (member (car l) ll =)) rest)
544 (lp (cdr l) (cons (car l) acc))
545 (lp (cdr l) acc)))))
546
547 (define (lset-difference = list1 . rest)
548 (if (null? rest)
549 list1
550 (let lp ((l list1) (acc '()))
551 (if (null? l)
552 (reverse! acc)
553 (if (any (lambda (ll) (member (car l) ll =)) rest)
554 (lp (cdr l) acc)
555 (lp (cdr l) (cons (car l) acc)))))))
556
557 ;(define (fold kons knil list1 . rest)
558
559 (define (lset-xor = . rest)
560 (fold (lambda (lst res)
561 (let lp ((l lst) (acc '()))
562 (if (null? l)
563 (let lp0 ((r res) (acc acc))
564 (if (null? r)
565 (reverse! acc)
566 (if (member (car r) lst =)
567 (lp0 (cdr r) acc)
568 (lp0 (cdr r) (cons (car r) acc)))))
569 (if (member (car l) res =)
570 (lp (cdr l) acc)
571 (lp (cdr l) (cons (car l) acc))))))
572 '()
573 rest))
574
575 (define (lset-diff+intersection = list1 . rest)
576 (let lp ((l list1) (accd '()) (acci '()))
577 (if (null? l)
578 (values (reverse! accd) (reverse! acci))
579 (let ((appears (every (lambda (ll) (member (car l) ll =)) rest)))
580 (if appears
581 (lp (cdr l) accd (cons (car l) acci))
582 (lp (cdr l) (cons (car l) accd) acci))))))
583
584
585 (define (lset-union! = . rest)
586 (apply lset-union = rest)) ; XXX:optimize
587
588 (define (lset-intersection! = list1 . rest)
589 (apply lset-intersection = list1 rest)) ; XXX:optimize
590
591 (define (lset-xor! = . rest)
592 (apply lset-xor = rest)) ; XXX:optimize
593
594 (define (lset-diff+intersection! = list1 . rest)
595 (apply lset-diff+intersection = list1 rest)) ; XXX:optimize
596
597 ;;; srfi-1.scm ends here