nendo 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/srfi-1.nnd ADDED
@@ -0,0 +1,1416 @@
1
+ ;;; -*- mode: nendo; syntax: scheme -*-
2
+ ;;;
3
+ ;; This code is based on the reference implementation by Olin Shivers
4
+ ;;
5
+ ;; Copyright (c) 1998, 1999 by Olin Shivers. You may do as you please with
6
+ ;; this code as long as you do not remove this copyright notice or
7
+ ;; hold me liable for its use. Please send bug reports to shivers@ai.mit.edu.
8
+ ;;
9
+
10
+ ;; Subsequently modified for nendo: Kiyoka Nishiyama
11
+ ;; I modified these functions, because Nendo does not support call/cc.
12
+ ;;
13
+ ;; %cdrs %cars+cdrs %cars+cdrs+
14
+ ;;
15
+
16
+ ;;; Occasionally useful as a value to be passed to a fold or other
17
+ ;;; higher-order procedure.
18
+ (define (xcons d a) (cons a d))
19
+
20
+ ;;;; Recursively copy every cons.
21
+ ;(define (tree-copy x)
22
+ ; (let recur ((x x))
23
+ ; (if (not (pair? x)) x
24
+ ; (cons (recur (car x)) (recur (cdr x))))))
25
+
26
+ ;;; Make a list of length LEN.
27
+
28
+ (define (make-list len . maybe-elt)
29
+ (check-arg (lambda (n) (and (integer? n) (>= n 0))) len make-list)
30
+ (let ((elt (cond ((null? maybe-elt) #f) ; Default value
31
+ ((null? (cdr maybe-elt)) (car maybe-elt))
32
+ (else (error "Too many arguments to MAKE-LIST"
33
+ (cons len maybe-elt))))))
34
+ (do ((i len (- i 1))
35
+ (ans '() (cons elt ans)))
36
+ ((<= i 0) ans))))
37
+
38
+
39
+ ;(define (list . ans) ans) ; R4RS
40
+
41
+
42
+ ;;; Make a list of length LEN. Elt i is (PROC i) for 0 <= i < LEN.
43
+
44
+ (define (list-tabulate len proc)
45
+ (check-arg (lambda (n) (and (integer? n) (>= n 0))) len list-tabulate)
46
+ (check-arg procedure? proc list-tabulate)
47
+ (do ((i (- len 1) (- i 1))
48
+ (ans '() (cons (proc i) ans)))
49
+ ((< i 0) ans)))
50
+
51
+ ;;; (cons* a1 a2 ... an) = (cons a1 (cons a2 (cons ... an)))
52
+ ;;; (cons* a1) = a1 (cons* a1 a2 ...) = (cons a1 (cons* a2 ...))
53
+ ;;;
54
+ ;;; (cons first (unfold not-pair? car cdr rest values))
55
+
56
+ (define (cons* first . rest)
57
+ (let recur ((x first) (rest rest))
58
+ (if (pair? rest)
59
+ (cons x (recur (car rest) (cdr rest)))
60
+ x)))
61
+
62
+ ;;; (unfold not-pair? car cdr lis values)
63
+
64
+ (define (list-copy lis)
65
+ (let recur ((lis lis))
66
+ (if (pair? lis)
67
+ (cons (car lis) (recur (cdr lis)))
68
+ lis)))
69
+
70
+ ;;; IOTA count [start step] (start start+step ... start+(count-1)*step)
71
+
72
+ (define (iota count . args)
73
+ (if (< count 0) (error "Negative step count" iota count))
74
+ (let ((start (if (pair? args) (car args) 0))
75
+ (step (if (and (pair? args) (pair? (cdr args))) (cadr args) 1)))
76
+ (check-arg number? start)
77
+ (check-arg number? step)
78
+ (let ((last-val (+ start (* (- count 1) step))))
79
+ (do ((count count (- count 1))
80
+ (val last-val (- val step))
81
+ (ans '() (cons val ans)))
82
+ ((<= count 0) ans)))))
83
+
84
+ ;;; I thought these were lovely, but the public at large did not share my
85
+ ;;; enthusiasm...
86
+ ;;; :IOTA to (0 ... to-1)
87
+ ;;; :IOTA from to (from ... to-1)
88
+ ;;; :IOTA from to step (from from+step ...)
89
+
90
+ ;;; IOTA: to (1 ... to)
91
+ ;;; IOTA: from to (from+1 ... to)
92
+ ;;; IOTA: from to step (from+step from+2step ...)
93
+
94
+ ;(define (%parse-iota-args arg1 rest-args proc)
95
+ ; (let ((check (lambda (n) (check-arg integer? n proc))))
96
+ ; (check arg1)
97
+ ; (if (pair? rest-args)
98
+ ; (let ((arg2 (check (car rest-args)))
99
+ ; (rest (cdr rest-args)))
100
+ ; (if (pair? rest)
101
+ ; (let ((arg3 (check (car rest)))
102
+ ; (rest (cdr rest)))
103
+ ; (if (pair? rest) (error "Too many parameters" proc arg1 rest-args)
104
+ ; (values arg1 arg2 arg3)))
105
+ ; (values arg1 arg2 1)))
106
+ ; (values 0 arg1 1))))
107
+ ;
108
+ ;(define (iota: arg1 . rest-args)
109
+ ; (receive (from to step) (%parse-iota-args arg1 rest-args iota:)
110
+ ; (let* ((numsteps (floor (/ (- to from) step)))
111
+ ; (last-val (+ from (* step numsteps))))
112
+ ; (if (< numsteps 0) (error "Negative step count" iota: from to step))
113
+ ; (do ((steps-left numsteps (- steps-left 1))
114
+ ; (val last-val (- val step))
115
+ ; (ans '() (cons val ans)))
116
+ ; ((<= steps-left 0) ans)))))
117
+ ;
118
+ ;
119
+ ;(define (:iota arg1 . rest-args)
120
+ ; (receive (from to step) (%parse-iota-args arg1 rest-args :iota)
121
+ ; (let* ((numsteps (ceiling (/ (- to from) step)))
122
+ ; (last-val (+ from (* step (- numsteps 1)))))
123
+ ; (if (< numsteps 0) (error "Negative step count" :iota from to step))
124
+ ; (do ((steps-left numsteps (- steps-left 1))
125
+ ; (val last-val (- val step))
126
+ ; (ans '() (cons val ans)))
127
+ ; ((<= steps-left 0) ans)))))
128
+
129
+
130
+
131
+ (define (circular-list val1 . vals)
132
+ (let ((ans (cons val1 vals)))
133
+ (set-cdr! (last-pair ans) ans)
134
+ ans))
135
+
136
+ ;;; <proper-list> ::= () ; Empty proper list
137
+ ;;; | (cons <x> <proper-list>) ; Proper-list pair
138
+ ;;; Note that this definition rules out circular lists -- and this
139
+ ;;; function is required to detect this case and return false.
140
+
141
+ (define (proper-list? x)
142
+ (let lp ((x x) (lag x))
143
+ (if (pair? x)
144
+ (let ((x (cdr x)))
145
+ (if (pair? x)
146
+ (let ((x (cdr x))
147
+ (lag (cdr lag)))
148
+ (and (not (eq? x lag)) (lp x lag)))
149
+ (null? x)))
150
+ (null? x))))
151
+
152
+ ;;; A dotted list is a finite list (possibly of length 0) terminated
153
+ ;;; by a non-nil value. Any non-cons, non-nil value (e.g., "foo" or 5)
154
+ ;;; is a dotted list of length 0.
155
+ ;;;
156
+ ;;; <dotted-list> ::= <non-nil,non-pair> ; Empty dotted list
157
+ ;;; | (cons <x> <dotted-list>) ; Proper-list pair
158
+
159
+ (define (dotted-list? x)
160
+ (let lp ((x x) (lag x))
161
+ (if (pair? x)
162
+ (let ((x (cdr x)))
163
+ (if (pair? x)
164
+ (let ((x (cdr x))
165
+ (lag (cdr lag)))
166
+ (and (not (eq? x lag)) (lp x lag)))
167
+ (not (null? x))))
168
+ (not (null? x)))))
169
+
170
+ (define (circular-list? x)
171
+ (let lp ((x x) (lag x))
172
+ (and (pair? x)
173
+ (let ((x (cdr x)))
174
+ (and (pair? x)
175
+ (let ((x (cdr x))
176
+ (lag (cdr lag)))
177
+ (or (eq? x lag) (lp x lag))))))))
178
+
179
+ (define (list? x)
180
+ (if (circular-list? x)
181
+ #f
182
+ (proper-list? x)))
183
+
184
+ (define (not-pair? x) (not (pair? x))) ; Inline me.
185
+
186
+ ;;; This is a legal definition which is fast and sloppy:
187
+ ;;; (define null-list? not-pair?)
188
+ ;;; but we'll provide a more careful one:
189
+ (define (null-list? l)
190
+ (cond ((pair? l) #f)
191
+ ((null? l) #t)
192
+ (else (error "null-list?: argument out of domain" l))))
193
+
194
+ (define (list= = . lists)
195
+ (or (null? lists) ; special case
196
+
197
+ (let lp1 ((list-a (car lists))
198
+ (others (cdr lists)))
199
+ (or (null? others)
200
+ (let ((list-b (car others))
201
+ (others (cdr others)))
202
+ (if (eq? list-a list-b) ; EQ? => LIST=
203
+ (lp1 list-b others)
204
+ (let lp2 ((list-a list-a) (list-c list-b))
205
+ (if (null-list? list-a)
206
+ (and (null-list? list-c)
207
+ (lp1 list-b others))
208
+ (and (not (null-list? list-c))
209
+ (= (car list-a) (car list-c))
210
+ (lp2 (cdr list-a) (cdr list-c)))))))))))
211
+
212
+
213
+
214
+ ;;; R4RS, so commented out.
215
+ ;(define (length x) ; LENGTH may diverge or
216
+ ; (let lp ((x x) (len 0)) ; raise an error if X is
217
+ ; (if (pair? x) ; a circular list. This version
218
+ ; (lp (cdr x) (+ len 1)) ; diverges.
219
+ ; len)))
220
+
221
+ (define (length+ x) ; Returns #f if X is circular.
222
+ (let lp ((x x) (lag x) (len 0))
223
+ (if (pair? x)
224
+ (let ((x (cdr x))
225
+ (len (+ len 1)))
226
+ (if (pair? x)
227
+ (let ((x (cdr x))
228
+ (lag (cdr lag))
229
+ (len (+ len 1)))
230
+ (and (not (eq? x lag)) (lp x lag len)))
231
+ len))
232
+ len)))
233
+
234
+ (define (zip list1 . more-lists) (apply map list list1 more-lists))
235
+
236
+
237
+ ;;; Selectors
238
+ ;;;;;;;;;;;;;
239
+
240
+ ;;; R4RS non-primitives:
241
+ ;(define (caar x) (car (car x)))
242
+ ;(define (cadr x) (car (cdr x)))
243
+ ;(define (cdar x) (cdr (car x)))
244
+ ;(define (cddr x) (cdr (cdr x)))
245
+ ;
246
+ ;(define (caaar x) (caar (car x)))
247
+ ;(define (caadr x) (caar (cdr x)))
248
+ ;(define (cadar x) (cadr (car x)))
249
+ ;(define (caddr x) (cadr (cdr x)))
250
+ ;(define (cdaar x) (cdar (car x)))
251
+ ;(define (cdadr x) (cdar (cdr x)))
252
+ ;(define (cddar x) (cddr (car x)))
253
+ ;(define (cdddr x) (cddr (cdr x)))
254
+ ;
255
+ ;(define (caaaar x) (caaar (car x)))
256
+ ;(define (caaadr x) (caaar (cdr x)))
257
+ ;(define (caadar x) (caadr (car x)))
258
+ ;(define (caaddr x) (caadr (cdr x)))
259
+ ;(define (cadaar x) (cadar (car x)))
260
+ ;(define (cadadr x) (cadar (cdr x)))
261
+ ;(define (caddar x) (caddr (car x)))
262
+ ;(define (cadddr x) (caddr (cdr x)))
263
+ ;(define (cdaaar x) (cdaar (car x)))
264
+ ;(define (cdaadr x) (cdaar (cdr x)))
265
+ ;(define (cdadar x) (cdadr (car x)))
266
+ ;(define (cdaddr x) (cdadr (cdr x)))
267
+ ;(define (cddaar x) (cddar (car x)))
268
+ ;(define (cddadr x) (cddar (cdr x)))
269
+ ;(define (cdddar x) (cdddr (car x)))
270
+ ;(define (cddddr x) (cdddr (cdr x)))
271
+
272
+
273
+ (define first car)
274
+ (define second cadr)
275
+ (define third caddr)
276
+ (define fourth cadddr)
277
+ (define (fifth x) (car (cddddr x)))
278
+ (define (sixth x) (cadr (cddddr x)))
279
+ (define (seventh x) (caddr (cddddr x)))
280
+ (define (eighth x) (cadddr (cddddr x)))
281
+ (define (ninth x) (car (cddddr (cddddr x))))
282
+ (define (tenth x) (cadr (cddddr (cddddr x))))
283
+
284
+ (define (car+cdr pair) (values (car pair) (cdr pair)))
285
+
286
+ ;;; take & drop
287
+
288
+ (define (take lis k)
289
+ (check-arg integer? k take)
290
+ (let recur ((lis lis) (k k))
291
+ (if (zero? k) '()
292
+ (cons (car lis)
293
+ (recur (cdr lis) (- k 1))))))
294
+
295
+ (define (drop lis k)
296
+ (check-arg integer? k drop)
297
+ (let iter ((lis lis) (k k))
298
+ (if (zero? k) lis (iter (cdr lis) (- k 1)))))
299
+
300
+ (define (take! lis k)
301
+ (check-arg integer? k take!)
302
+ (if (zero? k) '()
303
+ (begin (set-cdr! (drop lis (- k 1)) '())
304
+ lis)))
305
+
306
+ ;;; TAKE-RIGHT and DROP-RIGHT work by getting two pointers into the list,
307
+ ;;; off by K, then chasing down the list until the lead pointer falls off
308
+ ;;; the end.
309
+
310
+ (define (take-right lis k)
311
+ (check-arg integer? k take-right)
312
+ (let lp ((lag lis) (lead (drop lis k)))
313
+ (if (pair? lead)
314
+ (lp (cdr lag) (cdr lead))
315
+ lag)))
316
+
317
+ (define (drop-right lis k)
318
+ (check-arg integer? k drop-right)
319
+ (let recur ((lag lis) (lead (drop lis k)))
320
+ (if (pair? lead)
321
+ (cons (car lag) (recur (cdr lag) (cdr lead)))
322
+ '())))
323
+
324
+ ;;; In this function, LEAD is actually K+1 ahead of LAG. This lets
325
+ ;;; us stop LAG one step early, in time to smash its cdr to ().
326
+ (define (drop-right! lis k)
327
+ (check-arg integer? k drop-right!)
328
+ (let ((lead (drop lis k)))
329
+ (if (pair? lead)
330
+
331
+ (let lp ((lag lis) (lead (cdr lead))) ; Standard case
332
+ (if (pair? lead)
333
+ (lp (cdr lag) (cdr lead))
334
+ (begin (set-cdr! lag '())
335
+ lis)))
336
+
337
+ '()))) ; Special case dropping everything -- no cons to side-effect.
338
+
339
+ ;(define (list-ref lis i) (car (drop lis i))) ; R4RS
340
+
341
+ ;;; These use the APL convention, whereby negative indices mean
342
+ ;;; "from the right." I liked them, but they didn't win over the
343
+ ;;; SRFI reviewers.
344
+ ;;; K >= 0: Take and drop K elts from the front of the list.
345
+ ;;; K <= 0: Take and drop -K elts from the end of the list.
346
+
347
+ ;(define (take lis k)
348
+ ; (check-arg integer? k take)
349
+ ; (if (negative? k)
350
+ ; (list-tail lis (+ k (length lis)))
351
+ ; (let recur ((lis lis) (k k))
352
+ ; (if (zero? k) '()
353
+ ; (cons (car lis)
354
+ ; (recur (cdr lis) (- k 1)))))))
355
+ ;
356
+ ;(define (drop lis k)
357
+ ; (check-arg integer? k drop)
358
+ ; (if (negative? k)
359
+ ; (let recur ((lis lis) (nelts (+ k (length lis))))
360
+ ; (if (zero? nelts) '()
361
+ ; (cons (car lis)
362
+ ; (recur (cdr lis) (- nelts 1)))))
363
+ ; (list-tail lis k)))
364
+ ;
365
+ ;
366
+ ;(define (take! lis k)
367
+ ; (check-arg integer? k take!)
368
+ ; (cond ((zero? k) '())
369
+ ; ((positive? k)
370
+ ; (set-cdr! (list-tail lis (- k 1)) '())
371
+ ; lis)
372
+ ; (else (list-tail lis (+ k (length lis))))))
373
+ ;
374
+ ;(define (drop! lis k)
375
+ ; (check-arg integer? k drop!)
376
+ ; (if (negative? k)
377
+ ; (let ((nelts (+ k (length lis))))
378
+ ; (if (zero? nelts) '()
379
+ ; (begin (set-cdr! (list-tail lis (- nelts 1)) '())
380
+ ; lis)))
381
+ ; (list-tail lis k)))
382
+
383
+ (define (split-at x k)
384
+ (check-arg integer? k split-at)
385
+ (let recur ((lis x) (k k))
386
+ (if (zero? k) (values '() lis)
387
+ (receive (prefix suffix) (recur (cdr lis) (- k 1))
388
+ (values (cons (car lis) prefix) suffix)))))
389
+
390
+ (define (split-at! x k)
391
+ (check-arg integer? k split-at!)
392
+ (if (zero? k) (values '() x)
393
+ (let* ((prev (drop x (- k 1)))
394
+ (suffix (cdr prev)))
395
+ (set-cdr! prev '())
396
+ (values x suffix))))
397
+
398
+
399
+ (define (last lis) (car (last-pair lis)))
400
+
401
+ (define (last-pair lis)
402
+ (check-arg pair? lis last-pair)
403
+ (let lp ((lis lis))
404
+ (let ((tail (cdr lis)))
405
+ (if (pair? tail) (lp tail) lis))))
406
+
407
+
408
+ ;;; Unzippers -- 1 through 5
409
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
410
+
411
+ (define (unzip1 lis) (map car lis))
412
+
413
+ (define (unzip2 lis)
414
+ (let recur ((lis lis))
415
+ (if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle
416
+ (let ((elt (car lis))) ; dotted lists.
417
+ (receive (a b) (recur (cdr lis))
418
+ (values (cons (car elt) a)
419
+ (cons (cadr elt) b)))))))
420
+
421
+ (define (unzip3 lis)
422
+ (let recur ((lis lis))
423
+ (if (null-list? lis) (values lis lis lis)
424
+ (let ((elt (car lis)))
425
+ (receive (a b c) (recur (cdr lis))
426
+ (values (cons (car elt) a)
427
+ (cons (cadr elt) b)
428
+ (cons (caddr elt) c)))))))
429
+
430
+ (define (unzip4 lis)
431
+ (let recur ((lis lis))
432
+ (if (null-list? lis) (values lis lis lis lis)
433
+ (let ((elt (car lis)))
434
+ (receive (a b c d) (recur (cdr lis))
435
+ (values (cons (car elt) a)
436
+ (cons (cadr elt) b)
437
+ (cons (caddr elt) c)
438
+ (cons (cadddr elt) d)))))))
439
+
440
+ (define (unzip5 lis)
441
+ (let recur ((lis lis))
442
+ (if (null-list? lis) (values lis lis lis lis lis)
443
+ (let ((elt (car lis)))
444
+ (receive (a b c d e) (recur (cdr lis))
445
+ (values (cons (car elt) a)
446
+ (cons (cadr elt) b)
447
+ (cons (caddr elt) c)
448
+ (cons (cadddr elt) d)
449
+ (cons (car (cddddr elt)) e)))))))
450
+
451
+
452
+ ;;; append! append-reverse append-reverse! concatenate concatenate!
453
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
454
+
455
+ (define (append! . lists)
456
+ ;; First, scan through lists looking for a non-empty one.
457
+ (let lp ((lists lists) (prev '()))
458
+ (if (not (pair? lists)) prev
459
+ (let ((first (car lists))
460
+ (rest (cdr lists)))
461
+ (if (not (pair? first)) (lp rest first)
462
+
463
+ ;; Now, do the splicing.
464
+ (let lp2 ((tail-cons (last-pair first))
465
+ (rest rest))
466
+ (if (pair? rest)
467
+ (let ((next (car rest))
468
+ (rest (cdr rest)))
469
+ (set-cdr! tail-cons next)
470
+ (lp2 (if (pair? next) (last-pair next) tail-cons)
471
+ rest))
472
+ first)))))))
473
+
474
+ ;;; APPEND is R4RS.
475
+ ;(define (append . lists)
476
+ ; (if (pair? lists)
477
+ ; (let recur ((list1 (car lists)) (lists (cdr lists)))
478
+ ; (if (pair? lists)
479
+ ; (let ((tail (recur (car lists) (cdr lists))))
480
+ ; (fold-right cons tail list1)) ; Append LIST1 & TAIL.
481
+ ; list1))
482
+ ; '()))
483
+
484
+ ;(define (append-reverse rev-head tail) (fold cons tail rev-head))
485
+
486
+ ;(define (append-reverse! rev-head tail)
487
+ ; (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair)
488
+ ; tail
489
+ ; rev-head))
490
+
491
+ ;;; Hand-inline the FOLD and PAIR-FOLD ops for speed.
492
+
493
+ (define (append-reverse rev-head tail)
494
+ (let lp ((rev-head rev-head) (tail tail))
495
+ (if (null-list? rev-head) tail
496
+ (lp (cdr rev-head) (cons (car rev-head) tail)))))
497
+
498
+ (define (append-reverse! rev-head tail)
499
+ (let lp ((rev-head rev-head) (tail tail))
500
+ (if (null-list? rev-head) tail
501
+ (let ((next-rev (cdr rev-head)))
502
+ (set-cdr! rev-head tail)
503
+ (lp next-rev rev-head)))))
504
+
505
+
506
+ (define (concatenate lists) (reduce-right append '() lists))
507
+ (define (concatenate! lists) (reduce-right append! '() lists))
508
+
509
+ ;;; Fold/map internal utilities
510
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
511
+ ;;; These little internal utilities are used by the general
512
+ ;;; fold & mapper funs for the n-ary cases . It'd be nice if they got inlined.
513
+ ;;; One the other hand, the n-ary cases are painfully inefficient as it is.
514
+ ;;; An aggressive implementation should simply re-write these functions
515
+ ;;; for raw efficiency; I have written them for as much clarity, portability,
516
+ ;;; and simplicity as can be achieved.
517
+ ;;;
518
+ ;;; I use the dreaded call/cc to do local aborts. A good compiler could
519
+ ;;; handle this with extreme efficiency. An implementation that provides
520
+ ;;; a one-shot, non-persistent continuation grabber could help the compiler
521
+ ;;; out by using that in place of the call/cc's in these routines.
522
+ ;;;
523
+ ;;; These functions have funky definitions that are precisely tuned to
524
+ ;;; the needs of the fold/map procs -- for example, to minimize the number
525
+ ;;; of times the argument lists need to be examined.
526
+
527
+ ;;; Return (map cdr lists).
528
+ ;;; However, if any element of LISTS is empty, just abort and return '().
529
+ (define (%cdrs lists)
530
+ (let1 abort (find
531
+ (lambda (lis)
532
+ (null-list? lis))
533
+ lists)
534
+ (if abort
535
+ '()
536
+ (let recur ((lists lists))
537
+ (if (pair? lists)
538
+ (let ((lis (car lists)))
539
+ (if (null-list? lis)
540
+ '()
541
+ (cons (cdr lis) (recur (cdr lists)))))
542
+ '())))))
543
+
544
+ (define (%cars+ lists last-elt) ; (append! (map car lists) (list last-elt))
545
+ (let recur ((lists lists))
546
+ (if (pair? lists) (cons (caar lists) (recur (cdr lists))) (list last-elt))))
547
+
548
+ ;;; LISTS is a (not very long) non-empty list of lists.
549
+ ;;; Return two lists: the cars & the cdrs of the lists.
550
+ ;;; However, if any of the lists is empty, just abort and return [() ()].
551
+
552
+ (define (%cars+cdrs lists)
553
+ (let1 abort (find
554
+ (lambda (lis)
555
+ (null-list? lis))
556
+ lists)
557
+ (if abort
558
+ (values '() '())
559
+ (let recur ((lists lists))
560
+ (if (pair? lists)
561
+ (receive (list other-lists) (car+cdr lists)
562
+ (if (null-list? list)
563
+ (values '() '()) ; LIST is empty -- bail out
564
+ (receive (a d) (car+cdr list)
565
+ (receive (cars cdrs) (recur other-lists)
566
+ (values (cons a cars) (cons d cdrs))))))
567
+ (values '() '()))))))
568
+
569
+ ;;; Like %CARS+CDRS, but we pass in a final elt tacked onto the end of the
570
+ ;;; cars list. What a hack.
571
+ (define (%cars+cdrs+ lists cars-final)
572
+ (let1 abort (find
573
+ (lambda (lis)
574
+ (null-list? lis))
575
+ lists)
576
+ (if abort
577
+ (values '() '())
578
+ (let recur ((lists lists))
579
+ (if (pair? lists)
580
+ (receive (list other-lists) (car+cdr lists)
581
+ (if (null-list? list)
582
+ (values '() '()) ; LIST is empty -- bail out
583
+ (receive (a d) (car+cdr list)
584
+ (receive (cars cdrs) (recur other-lists)
585
+ (values (cons a cars) (cons d cdrs))))))
586
+ (values (list cars-final) '()))))))
587
+
588
+
589
+ ;;; Like %CARS+CDRS, but blow up if any list is empty.
590
+ (define (%cars+cdrs/no-test lists)
591
+ (let recur ((lists lists))
592
+ (if (pair? lists)
593
+ (receive (list other-lists) (car+cdr lists)
594
+ (receive (a d) (car+cdr list)
595
+ (receive (cars cdrs) (recur other-lists)
596
+ (values (cons a cars) (cons d cdrs)))))
597
+ (values '() '()))))
598
+
599
+
600
+ ;;; count
601
+ ;;;;;;;;;
602
+ (define (count pred list1 . lists)
603
+ (check-arg procedure? pred count)
604
+ (if (pair? lists)
605
+
606
+ ;; N-ary case
607
+ (let lp ((list1 list1) (lists lists) (i 0))
608
+ (if (null-list? list1) i
609
+ (receive (as ds) (%cars+cdrs lists)
610
+ (if (null? as) i
611
+ (lp (cdr list1) ds
612
+ (if (apply pred (car list1) as) (+ i 1) i))))))
613
+
614
+ ;; Fast path
615
+ (let lp ((lis list1) (i 0))
616
+ (if (null-list? lis) i
617
+ (lp (cdr lis) (if (pred (car lis)) (+ i 1) i))))))
618
+
619
+
620
+ ;;; fold/unfold
621
+ ;;;;;;;;;;;;;;;
622
+
623
+ (define (unfold-right p f g seed . maybe-tail)
624
+ (check-arg procedure? p unfold-right)
625
+ (check-arg procedure? f unfold-right)
626
+ (check-arg procedure? g unfold-right)
627
+ (let lp ((seed seed) (ans (get-optional maybe-tail '())))
628
+ (if (p seed) ans
629
+ (lp (g seed)
630
+ (cons (f seed) ans)))))
631
+
632
+
633
+ (define (unfold p f g seed . maybe-tail-gen)
634
+ (check-arg procedure? p unfold)
635
+ (check-arg procedure? f unfold)
636
+ (check-arg procedure? g unfold)
637
+ (if (pair? maybe-tail-gen)
638
+
639
+ (let ((tail-gen (car maybe-tail-gen)))
640
+ (if (pair? (cdr maybe-tail-gen))
641
+ (apply error "Too many arguments" unfold p f g seed maybe-tail-gen)
642
+
643
+ (let recur ((seed seed))
644
+ (if (p seed) (tail-gen seed)
645
+ (cons (f seed) (recur (g seed)))))))
646
+
647
+ (let recur ((seed seed))
648
+ (if (p seed) '()
649
+ (cons (f seed) (recur (g seed)))))))
650
+
651
+
652
+ (define (fold kons knil lis1 . lists)
653
+ (check-arg procedure? kons fold)
654
+ (if (pair? lists)
655
+ (let lp ((lists (cons lis1 lists)) (ans knil)) ; N-ary case
656
+ (receive (cars+ans cdrs) (%cars+cdrs+ lists ans)
657
+ (if (null? cars+ans) ans ; Done.
658
+ (lp cdrs (apply kons cars+ans)))))
659
+
660
+ (let lp ((lis lis1) (ans knil)) ; Fast path
661
+ (if (null-list? lis) ans
662
+ (lp (cdr lis) (kons (car lis) ans))))))
663
+
664
+
665
+ (define (fold-right kons knil lis1 . lists)
666
+ (check-arg procedure? kons fold-right)
667
+ (if (pair? lists)
668
+ (let recur ((lists (cons lis1 lists))) ; N-ary case
669
+ (let ((cdrs (%cdrs lists)))
670
+ (if (null? cdrs) knil
671
+ (apply kons (%cars+ lists (recur cdrs))))))
672
+
673
+ (let recur ((lis lis1)) ; Fast path
674
+ (if (null-list? lis) knil
675
+ (let ((head (car lis)))
676
+ (kons head (recur (cdr lis))))))))
677
+
678
+
679
+ (define (pair-fold-right f zero lis1 . lists)
680
+ (check-arg procedure? f pair-fold-right)
681
+ (if (pair? lists)
682
+ (let recur ((lists (cons lis1 lists))) ; N-ary case
683
+ (let ((cdrs (%cdrs lists)))
684
+ (if (null? cdrs) zero
685
+ (apply f (append! lists (list (recur cdrs)))))))
686
+
687
+ (let recur ((lis lis1)) ; Fast path
688
+ (if (null-list? lis) zero (f lis (recur (cdr lis)))))))
689
+
690
+ (define (pair-fold f zero lis1 . lists)
691
+ (check-arg procedure? f pair-fold)
692
+ (if (pair? lists)
693
+ (let lp ((lists (cons lis1 lists)) (ans zero)) ; N-ary case
694
+ (let ((tails (%cdrs lists)))
695
+ (if (null? tails) ans
696
+ (lp tails (apply f (append! lists (list ans)))))))
697
+
698
+ (let lp ((lis lis1) (ans zero))
699
+ (if (null-list? lis) ans
700
+ (let ((tail (cdr lis))) ; Grab the cdr now,
701
+ (lp tail (f lis ans))))))) ; in case F SET-CDR!s LIS.
702
+
703
+
704
+ ;;; REDUCE and REDUCE-RIGHT only use RIDENTITY in the empty-list case.
705
+ ;;; These cannot meaningfully be n-ary.
706
+
707
+ (define (reduce f ridentity lis)
708
+ (check-arg procedure? f reduce)
709
+ (if (null-list? lis) ridentity
710
+ (fold f (car lis) (cdr lis))))
711
+
712
+ (define (reduce-right f ridentity lis)
713
+ (check-arg procedure? f reduce-right)
714
+ (if (null-list? lis) ridentity
715
+ (let recur ((head (car lis)) (lis (cdr lis)))
716
+ (if (pair? lis)
717
+ (f head (recur (car lis) (cdr lis)))
718
+ head))))
719
+
720
+
721
+
722
+ ;;; Mappers: append-map append-map! pair-for-each map! filter-map map-in-order
723
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
724
+
725
+ (define (append-map f lis1 . lists)
726
+ (really-append-map append-map append f lis1 lists))
727
+ (define (append-map! f lis1 . lists)
728
+ (really-append-map append-map! append! f lis1 lists))
729
+
730
+ (define (really-append-map who appender f lis1 lists)
731
+ (check-arg procedure? f who)
732
+ (if (pair? lists)
733
+ (receive (cars cdrs) (%cars+cdrs (cons lis1 lists))
734
+ (if (null? cars) '()
735
+ (let recur ((cars cars) (cdrs cdrs))
736
+ (let ((vals (apply f cars)))
737
+ (receive (cars2 cdrs2) (%cars+cdrs cdrs)
738
+ (if (null? cars2) vals
739
+ (appender vals (recur cars2 cdrs2))))))))
740
+
741
+ ;; Fast path
742
+ (if (null-list? lis1) '()
743
+ (let recur ((elt (car lis1)) (rest (cdr lis1)))
744
+ (let ((vals (f elt)))
745
+ (if (null-list? rest) vals
746
+ (appender vals (recur (car rest) (cdr rest)))))))))
747
+
748
+
749
+ (define (pair-for-each proc lis1 . lists)
750
+ (check-arg procedure? proc pair-for-each)
751
+ (if (pair? lists)
752
+
753
+ (let lp ((lists (cons lis1 lists)))
754
+ (let ((tails (%cdrs lists)))
755
+ (if (pair? tails)
756
+ (begin (apply proc lists)
757
+ (lp tails)))))
758
+
759
+ ;; Fast path.
760
+ (let lp ((lis lis1))
761
+ (if (not (null-list? lis))
762
+ (let ((tail (cdr lis))) ; Grab the cdr now,
763
+ (proc lis) ; in case PROC SET-CDR!s LIS.
764
+ (lp tail))))))
765
+
766
+ ;;; We stop when LIS1 runs out, not when any list runs out.
767
+ (define (map! f lis1 . lists)
768
+ (check-arg procedure? f map!)
769
+ (if (pair? lists)
770
+ (let lp ((lis1 lis1) (lists lists))
771
+ (if (not (null-list? lis1))
772
+ (receive (heads tails) (%cars+cdrs/no-test lists)
773
+ (set-car! lis1 (apply f (car lis1) heads))
774
+ (lp (cdr lis1) tails))))
775
+
776
+ ;; Fast path.
777
+ (pair-for-each (lambda (pair) (set-car! pair (f (car pair)))) lis1))
778
+ lis1)
779
+
780
+
781
+ ;;; Map F across L, and save up all the non-false results.
782
+ (define (filter-map f lis1 . lists)
783
+ (check-arg procedure? f filter-map)
784
+ (if (pair? lists)
785
+ (let recur ((lists (cons lis1 lists)))
786
+ (receive (cars cdrs) (%cars+cdrs lists)
787
+ (if (pair? cars)
788
+ (cond ((apply f cars) => (lambda (x) (cons x (recur cdrs))))
789
+ (else (recur cdrs))) ; Tail call in this arm.
790
+ '())))
791
+
792
+ ;; Fast path.
793
+ (let recur ((lis lis1))
794
+ (if (null-list? lis) lis
795
+ (let ((tail (recur (cdr lis))))
796
+ (cond ((f (car lis)) => (lambda (x) (cons x tail)))
797
+ (else tail)))))))
798
+
799
+
800
+ ;;; Map F across lists, guaranteeing to go left-to-right.
801
+ ;;; NOTE: Some implementations of R5RS MAP are compliant with this spec;
802
+ ;;; in which case this procedure may simply be defined as a synonym for MAP.
803
+
804
+ (define (map-in-order f lis1 . lists)
805
+ (check-arg procedure? f map-in-order)
806
+ (if (pair? lists)
807
+ (let recur ((lists (cons lis1 lists)))
808
+ (receive (cars cdrs) (%cars+cdrs lists)
809
+ (if (pair? cars)
810
+ (let ((x (apply f cars))) ; Do head first,
811
+ (cons x (recur cdrs))) ; then tail.
812
+ '())))
813
+
814
+ ;; Fast path.
815
+ (let recur ((lis lis1))
816
+ (if (null-list? lis) lis
817
+ (let ((tail (cdr lis))
818
+ (x (f (car lis)))) ; Do head first,
819
+ (cons x (recur tail))))))) ; then tail.
820
+
821
+
822
+ ;;; We extend MAP to handle arguments of unequal length.
823
+ (define map map-in-order)
824
+ (define for-each map-in-order)
825
+
826
+
827
+ ;;; filter, remove, partition
828
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
829
+ ;;; FILTER, REMOVE, PARTITION and their destructive counterparts do not
830
+ ;;; disorder the elements of their argument.
831
+
832
+ ;; This FILTER shares the longest tail of L that has no deleted elements.
833
+ ;; If Scheme had multi-continuation calls, they could be made more efficient.
834
+
835
+ (define (filter pred lis) ; Sleazing with EQ? makes this
836
+ (check-arg procedure? pred filter) ; one faster.
837
+ (let recur ((lis lis))
838
+ (if (null-list? lis) lis ; Use NOT-PAIR? to handle dotted lists.
839
+ (let ((head (car lis))
840
+ (tail (cdr lis)))
841
+ (if (pred head)
842
+ (let ((new-tail (recur tail))) ; Replicate the RECUR call so
843
+ (if (eq? tail new-tail) lis
844
+ (cons head new-tail)))
845
+ (recur tail)))))) ; this one can be a tail call.
846
+
847
+
848
+ ;;; Another version that shares longest tail.
849
+ ;(define (filter pred lis)
850
+ ; (receive (ans no-del?)
851
+ ; ;; (recur l) returns L with (pred x) values filtered.
852
+ ; ;; It also returns a flag NO-DEL? if the returned value
853
+ ; ;; is EQ? to L, i.e. if it didn't have to delete anything.
854
+ ; (let recur ((l l))
855
+ ; (if (null-list? l) (values l #t)
856
+ ; (let ((x (car l))
857
+ ; (tl (cdr l)))
858
+ ; (if (pred x)
859
+ ; (receive (ans no-del?) (recur tl)
860
+ ; (if no-del?
861
+ ; (values l #t)
862
+ ; (values (cons x ans) #f)))
863
+ ; (receive (ans no-del?) (recur tl) ; Delete X.
864
+ ; (values ans #f))))))
865
+ ; ans))
866
+
867
+
868
+
869
+ ;(define (filter! pred lis) ; Things are much simpler
870
+ ; (let recur ((lis lis)) ; if you are willing to
871
+ ; (if (pair? lis) ; push N stack frames & do N
872
+ ; (cond ((pred (car lis)) ; SET-CDR! writes, where N is
873
+ ; (set-cdr! lis (recur (cdr lis))); the length of the answer.
874
+ ; lis)
875
+ ; (else (recur (cdr lis))))
876
+ ; lis)))
877
+
878
+
879
+ ;;; This implementation of FILTER!
880
+ ;;; - doesn't cons, and uses no stack;
881
+ ;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
882
+ ;;; usually expensive on modern machines, and can be extremely expensive on
883
+ ;;; modern Schemes (e.g., ones that have generational GC's).
884
+ ;;; It just zips down contiguous runs of in and out elts in LIS doing the
885
+ ;;; minimal number of SET-CDR!s to splice the tail of one run of ins to the
886
+ ;;; beginning of the next.
887
+
888
+ (define (filter! pred lis)
889
+ (check-arg procedure? pred filter!)
890
+ (let lp ((ans lis))
891
+ (cond ((null-list? ans) ans) ; Scan looking for
892
+ ((not (pred (car ans))) (lp (cdr ans))) ; first cons of result.
893
+
894
+ ;; ANS is the eventual answer.
895
+ ;; SCAN-IN: (CDR PREV) = LIS and (CAR PREV) satisfies PRED.
896
+ ;; Scan over a contiguous segment of the list that
897
+ ;; satisfies PRED.
898
+ ;; SCAN-OUT: (CAR PREV) satisfies PRED. Scan over a contiguous
899
+ ;; segment of the list that *doesn't* satisfy PRED.
900
+ ;; When the segment ends, patch in a link from PREV
901
+ ;; to the start of the next good segment, and jump to
902
+ ;; SCAN-IN.
903
+ (else (letrec ((scan-in (lambda (prev lis)
904
+ (if (pair? lis)
905
+ (if (pred (car lis))
906
+ (scan-in lis (cdr lis))
907
+ (scan-out prev (cdr lis))))))
908
+ (scan-out (lambda (prev lis)
909
+ (let lp ((lis lis))
910
+ (if (pair? lis)
911
+ (if (pred (car lis))
912
+ (begin (set-cdr! prev lis)
913
+ (scan-in lis (cdr lis)))
914
+ (lp (cdr lis)))
915
+ (set-cdr! prev lis))))))
916
+ (scan-in ans (cdr ans))
917
+ ans)))))
918
+
919
+
920
+
921
+ ;;; Answers share common tail with LIS where possible;
922
+ ;;; the technique is slightly subtle.
923
+
924
+ (define (partition pred lis)
925
+ (check-arg procedure? pred partition)
926
+ (let recur ((lis lis))
927
+ (if (null-list? lis) (values lis lis) ; Use NOT-PAIR? to handle dotted lists.
928
+ (let ((elt (car lis))
929
+ (tail (cdr lis)))
930
+ (receive (in out) (recur tail)
931
+ (if (pred elt)
932
+ (values (if (pair? out) (cons elt in) lis) out)
933
+ (values in (if (pair? in) (cons elt out) lis))))))))
934
+
935
+
936
+
937
+ ;(define (partition! pred lis) ; Things are much simpler
938
+ ; (let recur ((lis lis)) ; if you are willing to
939
+ ; (if (null-list? lis) (values lis lis) ; push N stack frames & do N
940
+ ; (let ((elt (car lis))) ; SET-CDR! writes, where N is
941
+ ; (receive (in out) (recur (cdr lis)) ; the length of LIS.
942
+ ; (cond ((pred elt)
943
+ ; (set-cdr! lis in)
944
+ ; (values lis out))
945
+ ; (else (set-cdr! lis out)
946
+ ; (values in lis))))))))
947
+
948
+
949
+ ;;; This implementation of PARTITION!
950
+ ;;; - doesn't cons, and uses no stack;
951
+ ;;; - is careful not to do redundant SET-CDR! writes, as writes to memory are
952
+ ;;; usually expensive on modern machines, and can be extremely expensive on
953
+ ;;; modern Schemes (e.g., ones that have generational GC's).
954
+ ;;; It just zips down contiguous runs of in and out elts in LIS doing the
955
+ ;;; minimal number of SET-CDR!s to splice these runs together into the result
956
+ ;;; lists.
957
+
958
+ (define (partition! pred lis)
959
+ (check-arg procedure? pred partition!)
960
+ (if (null-list? lis) (values lis lis)
961
+
962
+ ;; This pair of loops zips down contiguous in & out runs of the
963
+ ;; list, splicing the runs together. The invariants are
964
+ ;; SCAN-IN: (cdr in-prev) = LIS.
965
+ ;; SCAN-OUT: (cdr out-prev) = LIS.
966
+ (letrec ((scan-in (lambda (in-prev out-prev lis)
967
+ (let lp ((in-prev in-prev) (lis lis))
968
+ (if (pair? lis)
969
+ (if (pred (car lis))
970
+ (lp lis (cdr lis))
971
+ (begin (set-cdr! out-prev lis)
972
+ (scan-out in-prev lis (cdr lis))))
973
+ (set-cdr! out-prev lis))))) ; Done.
974
+
975
+ (scan-out (lambda (in-prev out-prev lis)
976
+ (let lp ((out-prev out-prev) (lis lis))
977
+ (if (pair? lis)
978
+ (if (pred (car lis))
979
+ (begin (set-cdr! in-prev lis)
980
+ (scan-in lis out-prev (cdr lis)))
981
+ (lp lis (cdr lis)))
982
+ (set-cdr! in-prev lis)))))) ; Done.
983
+
984
+ ;; Crank up the scan&splice loops.
985
+ (if (pred (car lis))
986
+ ;; LIS begins in-list. Search for out-list's first pair.
987
+ (let lp ((prev-l lis) (l (cdr lis)))
988
+ (cond ((not (pair? l)) (values lis l))
989
+ ((pred (car l)) (lp l (cdr l)))
990
+ (else (scan-out prev-l l (cdr l))
991
+ (values lis l)))) ; Done.
992
+
993
+ ;; LIS begins out-list. Search for in-list's first pair.
994
+ (let lp ((prev-l lis) (l (cdr lis)))
995
+ (cond ((not (pair? l)) (values l lis))
996
+ ((pred (car l))
997
+ (scan-in l prev-l (cdr l))
998
+ (values l lis)) ; Done.
999
+ (else (lp l (cdr l)))))))))
1000
+
1001
+
1002
+ ;;; Inline us, please.
1003
+ (define (remove pred l) (filter (lambda (x) (not (pred x))) l))
1004
+ (define (remove! pred l) (filter! (lambda (x) (not (pred x))) l))
1005
+
1006
+
1007
+
1008
+ ;;; Here's the taxonomy for the DELETE/ASSOC/MEMBER functions.
1009
+ ;;; (I don't actually think these are the world's most important
1010
+ ;;; functions -- the procedural FILTER/REMOVE/FIND/FIND-TAIL variants
1011
+ ;;; are far more general.)
1012
+ ;;;
1013
+ ;;; Function Action
1014
+ ;;; ---------------------------------------------------------------------------
1015
+ ;;; remove pred lis Delete by general predicate
1016
+ ;;; delete x lis [=] Delete by element comparison
1017
+ ;;;
1018
+ ;;; find pred lis Search by general predicate
1019
+ ;;; find-tail pred lis Search by general predicate
1020
+ ;;; member x lis [=] Search by element comparison
1021
+ ;;;
1022
+ ;;; assoc key lis [=] Search alist by key comparison
1023
+ ;;; alist-delete key alist [=] Alist-delete by key comparison
1024
+
1025
+ (define (delete x lis . maybe-=)
1026
+ (let ((= (get-optional maybe-= equal?)))
1027
+ (filter (lambda (y) (not (= x y))) lis)))
1028
+
1029
+ (define (delete! x lis . maybe-=)
1030
+ (let ((= (get-optional maybe-= equal?)))
1031
+ (filter! (lambda (y) (not (= x y))) lis)))
1032
+
1033
+ ;;; Extended from R4RS to take an optional comparison argument.
1034
+ (define (member x lis . maybe-=)
1035
+ (let ((= (get-optional maybe-= equal?)))
1036
+ (find-tail (lambda (y) (= x y)) lis)))
1037
+
1038
+ ;;; R4RS, hence we don't bother to define.
1039
+ ;;; The MEMBER and then FIND-TAIL call should definitely
1040
+ ;;; be inlined for MEMQ & MEMV.
1041
+ ;(define (memq x lis) (member x lis eq?))
1042
+ ;(define (memv x lis) (member x lis eqv?))
1043
+
1044
+
1045
+ ;;; right-duplicate deletion
1046
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1047
+ ;;; delete-duplicates delete-duplicates!
1048
+ ;;;
1049
+ ;;; Beware -- these are N^2 algorithms. To efficiently remove duplicates
1050
+ ;;; in long lists, sort the list to bring duplicates together, then use a
1051
+ ;;; linear-time algorithm to kill the dups. Or use an algorithm based on
1052
+ ;;; element-marking. The former gives you O(n lg n), the latter is linear.
1053
+
1054
+ (define (delete-duplicates lis . maybe-=)
1055
+ (let ((elt= (get-optional maybe-= equal?)))
1056
+ (check-arg procedure? elt= delete-duplicates)
1057
+ (let recur ((lis lis))
1058
+ (if (null-list? lis) lis
1059
+ (let* ((x (car lis))
1060
+ (tail (cdr lis))
1061
+ (new-tail (recur (delete x tail elt=))))
1062
+ (if (eq? tail new-tail) lis (cons x new-tail)))))))
1063
+
1064
+ (define (delete-duplicates! lis . maybe-=)
1065
+ (let ((elt= (get-optional maybe-= equal?)))
1066
+ (check-arg procedure? elt= delete-duplicates!)
1067
+ (let recur ((lis lis))
1068
+ (if (null-list? lis) lis
1069
+ (let* ((x (car lis))
1070
+ (tail (cdr lis))
1071
+ (new-tail (recur (delete! x tail elt=))))
1072
+ (if (eq? tail new-tail) lis (cons x new-tail)))))))
1073
+
1074
+
1075
+ ;;; alist stuff
1076
+ ;;;;;;;;;;;;;;;
1077
+
1078
+ ;;; Extended from R4RS to take an optional comparison argument.
1079
+ (define (assoc x lis . maybe-=)
1080
+ (let ((= (get-optional maybe-= equal?)))
1081
+ (find (lambda (entry) (= x (car entry))) lis)))
1082
+
1083
+ (define (alist-cons key datum alist) (cons (cons key datum) alist))
1084
+
1085
+ (define (alist-copy alist)
1086
+ (map (lambda (elt) (cons (car elt) (cdr elt)))
1087
+ alist))
1088
+
1089
+ (define (alist-delete key alist . maybe-=)
1090
+ (let ((= (get-optional maybe-= equal?)))
1091
+ (filter (lambda (elt) (not (= key (car elt)))) alist)))
1092
+
1093
+ (define (alist-delete! key alist . maybe-=)
1094
+ (let ((= (get-optional maybe-= equal?)))
1095
+ (filter! (lambda (elt) (not (= key (car elt)))) alist)))
1096
+
1097
+
1098
+ ;;; find find-tail take-while drop-while span break any every list-index
1099
+ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1100
+
1101
+ (define (find pred list)
1102
+ (cond ((find-tail pred list) => car)
1103
+ (else #f)))
1104
+
1105
+ (define (find-tail pred list)
1106
+ (check-arg procedure? pred find-tail)
1107
+ (let lp ((list list))
1108
+ (and (not (null-list? list))
1109
+ (if (pred (car list)) list
1110
+ (lp (cdr list))))))
1111
+
1112
+ (define (take-while pred lis)
1113
+ (check-arg procedure? pred take-while)
1114
+ (let recur ((lis lis))
1115
+ (if (null-list? lis) '()
1116
+ (let ((x (car lis)))
1117
+ (if (pred x)
1118
+ (cons x (recur (cdr lis)))
1119
+ '())))))
1120
+
1121
+ (define (drop-while pred lis)
1122
+ (check-arg procedure? pred drop-while)
1123
+ (let lp ((lis lis))
1124
+ (if (null-list? lis) '()
1125
+ (if (pred (car lis))
1126
+ (lp (cdr lis))
1127
+ lis))))
1128
+
1129
+ (define (take-while! pred lis)
1130
+ (check-arg procedure? pred take-while!)
1131
+ (if (or (null-list? lis) (not (pred (car lis)))) '()
1132
+ (begin (let lp ((prev lis) (rest (cdr lis)))
1133
+ (if (pair? rest)
1134
+ (let ((x (car rest)))
1135
+ (if (pred x) (lp rest (cdr rest))
1136
+ (set-cdr! prev '())))))
1137
+ lis)))
1138
+
1139
+ (define (span pred lis)
1140
+ (check-arg procedure? pred span)
1141
+ (let recur ((lis lis))
1142
+ (if (null-list? lis) (values '() '())
1143
+ (let ((x (car lis)))
1144
+ (if (pred x)
1145
+ (receive (prefix suffix) (recur (cdr lis))
1146
+ (values (cons x prefix) suffix))
1147
+ (values '() lis))))))
1148
+
1149
+ (define (span! pred lis)
1150
+ (check-arg procedure? pred span!)
1151
+ (if (or (null-list? lis) (not (pred (car lis)))) (values '() lis)
1152
+ (let ((suffix (let lp ((prev lis) (rest (cdr lis)))
1153
+ (if (null-list? rest) rest
1154
+ (let ((x (car rest)))
1155
+ (if (pred x) (lp rest (cdr rest))
1156
+ (begin (set-cdr! prev '())
1157
+ rest)))))))
1158
+ (values lis suffix))))
1159
+
1160
+
1161
+ (define (break pred lis) (span (lambda (x) (not (pred x))) lis))
1162
+ (define (break! pred lis) (span! (lambda (x) (not (pred x))) lis))
1163
+
1164
+ (define (any pred lis1 . lists)
1165
+ (check-arg procedure? pred any)
1166
+ (if (pair? lists)
1167
+
1168
+ ;; N-ary case
1169
+ (receive (heads tails) (%cars+cdrs (cons lis1 lists))
1170
+ (and (pair? heads)
1171
+ (let lp ((heads heads) (tails tails))
1172
+ (receive (next-heads next-tails) (%cars+cdrs tails)
1173
+ (if (pair? next-heads)
1174
+ (or (apply pred heads) (lp next-heads next-tails))
1175
+ (apply pred heads)))))) ; Last PRED app is tail call.
1176
+
1177
+ ;; Fast path
1178
+ (and (not (null-list? lis1))
1179
+ (let lp ((head (car lis1)) (tail (cdr lis1)))
1180
+ (if (null-list? tail)
1181
+ (pred head) ; Last PRED app is tail call.
1182
+ (or (pred head) (lp (car tail) (cdr tail))))))))
1183
+
1184
+
1185
+ ;(define (every pred list) ; Simple definition.
1186
+ ; (let lp ((list list)) ; Doesn't return the last PRED value.
1187
+ ; (or (not (pair? list))
1188
+ ; (and (pred (car list))
1189
+ ; (lp (cdr list))))))
1190
+
1191
+ (define (every pred lis1 . lists)
1192
+ (check-arg procedure? pred every)
1193
+ (if (pair? lists)
1194
+
1195
+ ;; N-ary case
1196
+ (receive (heads tails) (%cars+cdrs (cons lis1 lists))
1197
+ (or (not (pair? heads))
1198
+ (let lp ((heads heads) (tails tails))
1199
+ (receive (next-heads next-tails) (%cars+cdrs tails)
1200
+ (if (pair? next-heads)
1201
+ (and (apply pred heads) (lp next-heads next-tails))
1202
+ (apply pred heads)))))) ; Last PRED app is tail call.
1203
+
1204
+ ;; Fast path
1205
+ (or (null-list? lis1)
1206
+ (let lp ((head (car lis1)) (tail (cdr lis1)))
1207
+ (if (null-list? tail)
1208
+ (pred head) ; Last PRED app is tail call.
1209
+ (and (pred head) (lp (car tail) (cdr tail))))))))
1210
+
1211
+ (define (list-index pred lis1 . lists)
1212
+ (check-arg procedure? pred list-index)
1213
+ (if (pair? lists)
1214
+
1215
+ ;; N-ary case
1216
+ (let lp ((lists (cons lis1 lists)) (n 0))
1217
+ (receive (heads tails) (%cars+cdrs lists)
1218
+ (and (pair? heads)
1219
+ (if (apply pred heads) n
1220
+ (lp tails (+ n 1))))))
1221
+
1222
+ ;; Fast path
1223
+ (let lp ((lis lis1) (n 0))
1224
+ (and (not (null-list? lis))
1225
+ (if (pred (car lis)) n (lp (cdr lis) (+ n 1)))))))
1226
+
1227
+ ;;; Reverse
1228
+ ;;;;;;;;;;;
1229
+
1230
+ ;R4RS, so not defined here.
1231
+ ;(define (reverse lis) (fold cons '() lis))
1232
+
1233
+ ;(define (reverse! lis)
1234
+ ; (pair-fold (lambda (pair tail) (set-cdr! pair tail) pair) '() lis))
1235
+
1236
+ (define (reverse! lis)
1237
+ (let lp ((lis lis) (ans '()))
1238
+ (if (null-list? lis) ans
1239
+ (let ((tail (cdr lis)))
1240
+ (set-cdr! lis ans)
1241
+ (lp tail lis)))))
1242
+
1243
+ ;;; Lists-as-sets
1244
+ ;;;;;;;;;;;;;;;;;
1245
+
1246
+ ;;; This is carefully tuned code; do not modify casually.
1247
+ ;;; - It is careful to share storage when possible;
1248
+ ;;; - Side-effecting code tries not to perform redundant writes.
1249
+ ;;; - It tries to avoid linear-time scans in special cases where constant-time
1250
+ ;;; computations can be performed.
1251
+ ;;; - It relies on similar properties from the other list-lib procs it calls.
1252
+ ;;; For example, it uses the fact that the implementations of MEMBER and
1253
+ ;;; FILTER in this source code share longest common tails between args
1254
+ ;;; and results to get structure sharing in the lset procedures.
1255
+
1256
+ (define (%lset2<= = lis1 lis2) (every (lambda (x) (member x lis2 =)) lis1))
1257
+
1258
+ (define (lset<= = . lists)
1259
+ (check-arg procedure? = lset<=)
1260
+ (or (not (pair? lists)) ; 0-ary case
1261
+ (let lp ((s1 (car lists)) (rest (cdr lists)))
1262
+ (or (not (pair? rest))
1263
+ (let ((s2 (car rest)) (rest (cdr rest)))
1264
+ (and (or (eq? s2 s1) ; Fast path
1265
+ (%lset2<= = s1 s2)) ; Real test
1266
+ (lp s2 rest)))))))
1267
+
1268
+ (define (lset= = . lists)
1269
+ (check-arg procedure? = lset=)
1270
+ (or (not (pair? lists)) ; 0-ary case
1271
+ (let lp ((s1 (car lists)) (rest (cdr lists)))
1272
+ (or (not (pair? rest))
1273
+ (let ((s2 (car rest))
1274
+ (rest (cdr rest)))
1275
+ (and (or (eq? s1 s2) ; Fast path
1276
+ (and (%lset2<= = s1 s2) (%lset2<= = s2 s1))) ; Real test
1277
+ (lp s2 rest)))))))
1278
+
1279
+
1280
+ (define (lset-adjoin = lis . elts)
1281
+ (check-arg procedure? = lset-adjoin)
1282
+ (fold (lambda (elt ans) (if (member elt ans =) ans (cons elt ans)))
1283
+ lis elts))
1284
+
1285
+
1286
+ (define (lset-union = . lists)
1287
+ (check-arg procedure? = lset-union)
1288
+ (reduce (lambda (lis ans) ; Compute ANS + LIS.
1289
+ (cond ((null? lis) ans) ; Don't copy any lists
1290
+ ((null? ans) lis) ; if we don't have to.
1291
+ ((eq? lis ans) ans)
1292
+ (else
1293
+ (fold (lambda (elt ans) (if (any (lambda (x) (= x elt)) ans)
1294
+ ans
1295
+ (cons elt ans)))
1296
+ ans lis))))
1297
+ '() lists))
1298
+
1299
+ (define (lset-union! = . lists)
1300
+ (check-arg procedure? = lset-union!)
1301
+ (reduce (lambda (lis ans) ; Splice new elts of LIS onto the front of ANS.
1302
+ (cond ((null? lis) ans) ; Don't copy any lists
1303
+ ((null? ans) lis) ; if we don't have to.
1304
+ ((eq? lis ans) ans)
1305
+ (else
1306
+ (pair-fold (lambda (pair ans)
1307
+ (let ((elt (car pair)))
1308
+ (if (any (lambda (x) (= x elt)) ans)
1309
+ ans
1310
+ (begin (set-cdr! pair ans) pair))))
1311
+ ans lis))))
1312
+ '() lists))
1313
+
1314
+
1315
+ (define (lset-intersection = lis1 . lists)
1316
+ (check-arg procedure? = lset-intersection)
1317
+ (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
1318
+ (cond ((any null-list? lists) '()) ; Short cut
1319
+ ((null? lists) lis1) ; Short cut
1320
+ (else (filter (lambda (x)
1321
+ (every (lambda (lis) (member x lis =)) lists))
1322
+ lis1)))))
1323
+
1324
+ (define (lset-intersection! = lis1 . lists)
1325
+ (check-arg procedure? = lset-intersection!)
1326
+ (let ((lists (delete lis1 lists eq?))) ; Throw out any LIS1 vals.
1327
+ (cond ((any null-list? lists) '()) ; Short cut
1328
+ ((null? lists) lis1) ; Short cut
1329
+ (else (filter! (lambda (x)
1330
+ (every (lambda (lis) (member x lis =)) lists))
1331
+ lis1)))))
1332
+
1333
+
1334
+ (define (lset-difference = lis1 . lists)
1335
+ (check-arg procedure? = lset-difference)
1336
+ (let ((lists (filter pair? lists))) ; Throw out empty lists.
1337
+ (cond ((null? lists) lis1) ; Short cut
1338
+ ((memq lis1 lists) '()) ; Short cut
1339
+ (else (filter (lambda (x)
1340
+ (every (lambda (lis) (not (member x lis =)))
1341
+ lists))
1342
+ lis1)))))
1343
+
1344
+ (define (lset-difference! = lis1 . lists)
1345
+ (check-arg procedure? = lset-difference!)
1346
+ (let ((lists (filter pair? lists))) ; Throw out empty lists.
1347
+ (cond ((null? lists) lis1) ; Short cut
1348
+ ((memq lis1 lists) '()) ; Short cut
1349
+ (else (filter! (lambda (x)
1350
+ (every (lambda (lis) (not (member x lis =)))
1351
+ lists))
1352
+ lis1)))))
1353
+
1354
+
1355
+ (define (lset-xor = . lists)
1356
+ (check-arg procedure? = lset-xor)
1357
+ (reduce (lambda (b a) ; Compute A xor B:
1358
+ ;; Note that this code relies on the constant-time
1359
+ ;; short-cuts provided by LSET-DIFF+INTERSECTION,
1360
+ ;; LSET-DIFFERENCE & APPEND to provide constant-time short
1361
+ ;; cuts for the cases A = (), B = (), and A eq? B. It takes
1362
+ ;; a careful case analysis to see it, but it's carefully
1363
+ ;; built in.
1364
+
1365
+ ;; Compute a-b and a^b, then compute b-(a^b) and
1366
+ ;; cons it onto the front of a-b.
1367
+ (receive (a-b a-int-b) (lset-diff+intersection = a b)
1368
+ (cond ((null? a-b) (lset-difference = b a))
1369
+ ((null? a-int-b) (append b a))
1370
+ (else (fold (lambda (xb ans)
1371
+ (if (member xb a-int-b =) ans (cons xb ans)))
1372
+ a-b
1373
+ b)))))
1374
+ '() lists))
1375
+
1376
+
1377
+ (define (lset-xor! = . lists)
1378
+ (check-arg procedure? = lset-xor!)
1379
+ (reduce (lambda (b a) ; Compute A xor B:
1380
+ ;; Note that this code relies on the constant-time
1381
+ ;; short-cuts provided by LSET-DIFF+INTERSECTION,
1382
+ ;; LSET-DIFFERENCE & APPEND to provide constant-time short
1383
+ ;; cuts for the cases A = (), B = (), and A eq? B. It takes
1384
+ ;; a careful case analysis to see it, but it's carefully
1385
+ ;; built in.
1386
+
1387
+ ;; Compute a-b and a^b, then compute b-(a^b) and
1388
+ ;; cons it onto the front of a-b.
1389
+ (receive (a-b a-int-b) (lset-diff+intersection! = a b)
1390
+ (cond ((null? a-b) (lset-difference! = b a))
1391
+ ((null? a-int-b) (append! b a))
1392
+ (else (pair-fold (lambda (b-pair ans)
1393
+ (if (member (car b-pair) a-int-b =) ans
1394
+ (begin (set-cdr! b-pair ans) b-pair)))
1395
+ a-b
1396
+ b)))))
1397
+ '() lists))
1398
+
1399
+
1400
+ (define (lset-diff+intersection = lis1 . lists)
1401
+ (check-arg procedure? = lset-diff+intersection)
1402
+ (cond ((every null-list? lists) (values lis1 '())) ; Short cut
1403
+ ((memq lis1 lists) (values '() lis1)) ; Short cut
1404
+ (else (partition (lambda (elt)
1405
+ (not (any (lambda (lis) (member elt lis =))
1406
+ lists)))
1407
+ lis1))))
1408
+
1409
+ (define (lset-diff+intersection! = lis1 . lists)
1410
+ (check-arg procedure? = lset-diff+intersection!)
1411
+ (cond ((every null-list? lists) (values lis1 '())) ; Short cut
1412
+ ((memq lis1 lists) (values '() lis1)) ; Short cut
1413
+ (else (partition! (lambda (elt)
1414
+ (not (any (lambda (lis) (member elt lis =))
1415
+ lists)))
1416
+ lis1))))