Debian Codegraph @

scheme/ikarus.lists.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus lists)
18 (export $memq list? list cons* make-list append length list-ref reverse
19 last-pair memq memp memv member find assq assp assv assoc
20 remq remv remove remp filter map for-each andmap ormap list-tail
21 partition for-all exists fold-left fold-right)
22 (import
23 (ikarus system $fx)
24 (ikarus system $pairs)
25 (except (ikarus) list? list cons* make-list append reverse
26 last-pair length list-ref memq memp memv member find
27 assq assp assv assoc remq remv remove remp filter
28 map for-each andmap ormap list-tail partition
29 for-all exists fold-left fold-right))
30
31 (define $memq
32 (lambda (x ls)
33 (let f ([x x] [ls ls])
34 (and (pair? ls)
35 (if (eq? x (car ls))
36 ls
37 (f x (cdr ls)))))))
38
39 (define list (lambda x x))
40
41 (define cons*
42 (lambda (fst . rest)
43 (let f ([fst fst] [rest rest])
44 (cond
45 [(null? rest) fst]
46 [else
47 (cons fst (f ($car rest) ($cdr rest)))]))))
48
49 (define list?
50 (letrec ([race
51 (lambda (h t)
52 (if (pair? h)
53 (let ([h ($cdr h)])
54 (if (pair? h)
55 (and (not (eq? h t))
56 (race ($cdr h) ($cdr t)))
57 (null? h)))
58 (null? h)))])
59 (lambda (x) (race x x))))
60
61 (module (make-list)
62 (define f
63 (lambda (n fill ls)
64 (cond
65 [($fxzero? n) ls]
66 [else
67 (f ($fxsub1 n) fill (cons fill ls))])))
68 (define make-list
69 (case-lambda
70 [(n)
71 (if (and (fixnum? n) ($fx>= n 0))
72 (f n (void) '())
73 (die 'make-list "not a valid length" n))]
74 [(n fill)
75 (if (and (fixnum? n) ($fx>= n 0))
76 (f n fill '())
77 (die 'make-list "not a valid length" n))])))
78
79
80 (define length
81 (letrec ([race
82 (lambda (h t ls n)
83 (if (pair? h)
84 (let ([h ($cdr h)])
85 (if (pair? h)
86 (if (not (eq? h t))
87 (race ($cdr h) ($cdr t) ls ($fx+ n 2))
88 (die 'length "circular list" ls))
89 (if (null? h)
90 ($fx+ n 1)
91 (die 'length "not a proper list" ls))))
92 (if (null? h)
93 n
94 (die 'length "not a proper list" ls))))])
95 (lambda (ls)
96 (race ls ls ls 0))))
97
98 (define list-ref
99 (lambda (list index)
100 (define f
101 (lambda (ls i)
102 (cond
103 [($fxzero? i)
104 (if (pair? ls)
105 ($car ls)
106 (die 'list-ref "index is out of range" index list))]
107 [(pair? ls)
108 (f ($cdr ls) ($fxsub1 i))]
109 [(null? ls)
110 (die 'list-rec "index is out of range" index list)]
111 [else (die 'list-ref "not a list" list)])))
112 (unless (and (fixnum? index) ($fx>= index 0))
113 (die 'list-ref "not a valid index" index))
114 (f list index)))
115
116
117 (define list-tail
118 (lambda (list index)
119 (define f
120 (lambda (ls i)
121 (cond
122 [($fxzero? i) ls]
123 [(pair? ls)
124 (f ($cdr ls) ($fxsub1 i))]
125 [(null? ls)
126 (die 'list-tail "index is out of range" index list)]
127 [else (die 'list-tail "not a list" list)])))
128 (unless (and (fixnum? index) ($fx>= index 0))
129 (die 'list-tail "not a valid index" index))
130 (f list index)))
131
132 (module (append)
133 (define reverse
134 (lambda (h t ls ac)
135 (if (pair? h)
136 (let ([h ($cdr h)] [a1 ($car h)])
137 (if (pair? h)
138 (if (not (eq? h t))
139 (let ([a2 ($car h)])
140 (reverse ($cdr h) ($cdr t) ls (cons a2 (cons a1 ac))))
141 (die 'append "circular list" ls))
142 (if (null? h)
143 (cons a1 ac)
144 (die 'append "not a proper list" ls))))
145 (if (null? h)
146 ac
147 (die 'append "not a proper list" ls)))))
148 (define rev!
149 (lambda (ls ac)
150 (cond
151 [(null? ls) ac]
152 [else
153 (let ([ls^ ($cdr ls)])
154 ($set-cdr! ls ac)
155 (rev! ls^ ls))])))
156 (define append1
157 (lambda (ls ls*)
158 (cond
159 [(null? ls*) ls]
160 [else
161 (rev! (reverse ls ls ls '())
162 (append1 ($car ls*) ($cdr ls*)))])))
163 (define append
164 (case-lambda
165 [() '()]
166 [(ls) ls]
167 [(ls . ls*)
168 (append1 ls ls*)])))
169
170 (define reverse
171 (letrec ([race
172 (lambda (h t ls ac)
173 (if (pair? h)
174 (let ([h ($cdr h)] [ac (cons ($car h) ac)])
175 (if (pair? h)
176 (if (not (eq? h t))
177 (race ($cdr h) ($cdr t) ls (cons ($car h) ac))
178 (die 'reverse "circular list" ls))
179 (if (null? h)
180 ac
181 (die 'reverse "not a proper list" ls))))
182 (if (null? h)
183 ac
184 (die 'reverse "not a proper list" ls))))])
185 (lambda (x)
186 (race x x x '()))))
187
188 (define last-pair
189 (letrec ([race
190 (lambda (h t ls last)
191 (if (pair? h)
192 (let ([h ($cdr h)] [last h])
193 (if (pair? h)
194 (if (not (eq? h t))
195 (race ($cdr h) ($cdr t) ls h)
196 (die 'last-pair "circular list" ls))
197 last))
198 last))])
199 (lambda (x)
200 (if (pair? x)
201 (let ([d (cdr x)])
202 (race d d x x))
203 (die 'last-pair "not a pair" x)))))
204
205 (define memq
206 (letrec ([race
207 (lambda (h t ls x)
208 (if (pair? h)
209 (if (eq? ($car h) x)
210 h
211 (let ([h ($cdr h)])
212 (if (pair? h)
213 (if (eq? ($car h) x)
214 h
215 (if (not (eq? h t))
216 (race ($cdr h) ($cdr t) ls x)
217 (die 'memq "circular list" ls)))
218 (if (null? h)
219 '#f
220 (die 'memq "not a proper list" ls)))))
221 (if (null? h)
222 '#f
223 (die 'memq "not a proper list" ls))))])
224 (lambda (x ls)
225 (race ls ls ls x))))
226
227 (define memv
228 (letrec ([race
229 (lambda (h t ls x)
230 (if (pair? h)
231 (if (eqv? ($car h) x)
232 h
233 (let ([h ($cdr h)])
234 (if (pair? h)
235 (if (eqv? ($car h) x)
236 h
237 (if (not (eq? h t))
238 (race ($cdr h) ($cdr t) ls x)
239 (die 'memv "circular list" ls)))
240 (if (null? h)
241 '#f
242 (die 'memv "not a proper list" ls)))))
243 (if (null? h)
244 '#f
245 (die 'memv "not a proper list" ls))))])
246 (lambda (x ls)
247 (race ls ls ls x))))
248
249 (define member
250 (letrec ([race
251 (lambda (h t ls x)
252 (if (pair? h)
253 (if (equal? ($car h) x)
254 h
255 (let ([h ($cdr h)])
256 (if (pair? h)
257 (if (equal? ($car h) x)
258 h
259 (if (not (eq? h t))
260 (race ($cdr h) ($cdr t) ls x)
261 (die 'member "circular list" ls)))
262 (if (null? h)
263 '#f
264 (die 'member "not a proper list" ls)))))
265 (if (null? h)
266 '#f
267 (die 'member "not a proper list" ls))))])
268 (lambda (x ls)
269 (race ls ls ls x))))
270
271
272 (define memp
273 (letrec ([race
274 (lambda (h t ls p)
275 (if (pair? h)
276 (if (p ($car h))
277 h
278 (let ([h ($cdr h)])
279 (if (pair? h)
280 (if (p ($car h))
281 h
282 (if (not (eq? h t))
283 (race ($cdr h) ($cdr t) ls p)
284 (die 'memp "circular list" ls)))
285 (if (null? h)
286 '#f
287 (die 'memp "not a proper list" ls)))))
288 (if (null? h)
289 '#f
290 (die 'memp "not a proper list" ls))))])
291 (lambda (p ls)
292 (unless (procedure? p)
293 (die 'memp "not a procedure" p))
294 (race ls ls ls p))))
295
296 (define find
297 (letrec ([race
298 (lambda (h t ls p)
299 (if (pair? h)
300 (let ([a ($car h)])
301 (if (p a)
302 a
303 (let ([h ($cdr h)])
304 (if (pair? h)
305 (let ([a ($car h)])
306 (if (p a)
307 a
308 (if (not (eq? h t))
309 (race ($cdr h) ($cdr t) ls p)
310 (die 'find "circular list" ls))))
311 (if (null? h)
312 '#f
313 (die 'find "not a proper list" ls))))))
314 (if (null? h)
315 '#f
316 (die 'find "not a proper list" ls))))])
317 (lambda (p ls)
318 (unless (procedure? p)
319 (die 'find "not a procedure" p))
320 (race ls ls ls p))))
321
322 (define assq
323 (letrec ([race
324 (lambda (x h t ls)
325 (if (pair? h)
326 (let ([a ($car h)] [h ($cdr h)])
327 (if (pair? a)
328 (if (eq? ($car a) x)
329 a
330 (if (pair? h)
331 (if (not (eq? h t))
332 (let ([a ($car h)])
333 (if (pair? a)
334 (if (eq? ($car a) x)
335 a
336 (race x ($cdr h) ($cdr t) ls))
337 (die 'assq "malformed alist"
338 ls)))
339 (die 'assq "circular list" ls))
340 (if (null? h)
341 #f
342 (die 'assq "not a proper list" ls))))
343 (die 'assq "malformed alist" ls)))
344 (if (null? h)
345 #f
346 (die 'assq "not a proper list" ls))))])
347 (lambda (x ls)
348 (race x ls ls ls))))
349
350
351 (define assp
352 (letrec ([race
353 (lambda (p h t ls)
354 (if (pair? h)
355 (let ([a ($car h)] [h ($cdr h)])
356 (if (pair? a)
357 (if (p ($car a))
358 a
359 (if (pair? h)
360 (if (not (eq? h t))
361 (let ([a ($car h)])
362 (if (pair? a)
363 (if (p ($car a))
364 a
365 (race p ($cdr h) ($cdr t) ls))
366 (die 'assp "malformed alist"
367 ls)))
368 (die 'assp "circular list" ls))
369 (if (null? h)
370 #f
371 (die 'assp "not a proper list" ls))))
372 (die 'assp "malformed alist" ls)))
373 (if (null? h)
374 #f
375 (die 'assp "not a proper list" ls))))])
376 (lambda (p ls)
377 (unless (procedure? p)
378 (die 'assp "not a procedure" p))
379 (race p ls ls ls))))
380
381 (define assv
382 (letrec ([race
383 (lambda (x h t ls)
384 (if (pair? h)
385 (let ([a ($car h)] [h ($cdr h)])
386 (if (pair? a)
387 (if (eqv? ($car a) x)
388 a
389 (if (pair? h)
390 (if (not (eq? h t))
391 (let ([a ($car h)])
392 (if (pair? a)
393 (if (eqv? ($car a) x)
394 a
395 (race x ($cdr h) ($cdr t) ls))
396 (die 'assv "malformed alist"
397 ls)))
398 (die 'assv "circular list" ls))
399 (if (null? h)
400 #f
401 (die 'assv "not a proper list" ls))))
402 (die 'assv "malformed alist" ls)))
403 (if (null? h)
404 #f
405 (die 'assv "not a proper list" ls))))])
406 (lambda (x ls)
407 (race x ls ls ls))))
408
409 (define assoc
410 (letrec ([race
411 (lambda (x h t ls)
412 (if (pair? h)
413 (let ([a ($car h)] [h ($cdr h)])
414 (if (pair? a)
415 (if (equal? ($car a) x)
416 a
417 (if (pair? h)
418 (if (not (eq? h t))
419 (let ([a ($car h)])
420 (if (pair? a)
421 (if (equal? ($car a) x)
422 a
423 (race x ($cdr h) ($cdr t) ls))
424 (die 'assoc "malformed alist"
425 ls)))
426 (die 'assoc "circular list" ls))
427 (if (null? h)
428 #f
429 (die 'assoc "not a proper list" ls))))
430 (die 'assoc "malformed alist" ls)))
431 (if (null? h)
432 #f
433 (die 'assoc "not a proper list" ls))))])
434 (lambda (x ls)
435 (race x ls ls ls))))
436
437
438 (module (remq remv remove remp filter)
439 (define-syntax define-remover
440 (syntax-rules ()
441 [(_ name cmp check)
442 (define name
443 (letrec ([race
444 (lambda (h t ls x)
445 (if (pair? h)
446 (if (cmp ($car h) x)
447 (let ([h ($cdr h)])
448 (if (pair? h)
449 (if (not (eq? h t))
450 (if (cmp ($car h) x)
451 (race ($cdr h) ($cdr t) ls x)
452 (cons ($car h) (race ($cdr h) ($cdr t) ls x)))
453 (die 'name "circular list" ls))
454 (if (null? h)
455 '()
456 (die 'name "not a proper list" ls))))
457 (let ([a0 ($car h)] [h ($cdr h)])
458 (if (pair? h)
459 (if (not (eq? h t))
460 (if (cmp ($car h) x)
461 (cons a0 (race ($cdr h) ($cdr t) ls x))
462 (cons* a0 ($car h) (race ($cdr h) ($cdr t) ls x)))
463 (die 'name "circular list" ls))
464 (if (null? h)
465 (list a0)
466 (die 'name "not a proper list" ls)))))
467 (if (null? h)
468 '()
469 (die 'name "not a proper list" ls))))])
470 (lambda (x ls)
471 (check x ls)
472 (race ls ls ls x))))]))
473 (define-remover remq eq? (lambda (x ls) #t))
474 (define-remover remv eqv? (lambda (x ls) #t))
475 (define-remover remove equal? (lambda (x ls) #t))
476 (define-remover remp (lambda (elt p) (p elt))
477 (lambda (x ls)
478 (unless (procedure? x)
479 (die 'remp "not a procedure" x))))
480 (define-remover filter (lambda (elt p) (not (p elt)))
481 (lambda (x ls)
482 (unless (procedure? x)
483 (die 'filter "not a procedure" x)))))
484
485
486 (module (map)
487 (define who 'map)
488 (define len
489 (lambda (h t n)
490 (if (pair? h)
491 (let ([h ($cdr h)])
492 (if (pair? h)
493 (if (eq? h t)
494 (die who "circular list")
495 (len ($cdr h) ($cdr t) ($fx+ n 2)))
496 (if (null? h)
497 ($fxadd1 n)
498 (die who "improper list"))))
499 (if (null? h)
500 n
501 (die who "improper list")))))
502 (define map1
503 (lambda (f a d n)
504 (cond
505 [(pair? d)
506 (if ($fxzero? n)
507 (die who "list was altered!")
508 (cons (f a) (map1 f ($car d) ($cdr d) ($fxsub1 n))))]
509 [(null? d)
510 (if ($fxzero? n)
511 (cons (f a) '())
512 (die who "list was altered"))]
513 [else (die who "list was altered")])))
514 (define map2
515 (lambda (f a1 a2 d1 d2 n)
516 (cond
517 [(pair? d1)
518 (cond
519 [(pair? d2)
520 (if ($fxzero? n)
521 (die who "list was altered")
522 (cons (f a1 a2)
523 (map2 f
524 ($car d1) ($car d2)
525 ($cdr d1) ($cdr d2)
526 ($fxsub1 n))))]
527 [(null? d2) (die who "length mismatch")]
528 [else (die who "not a proper list")])]
529 [(null? d1)
530 (cond
531 [(null? d2)
532 (if ($fxzero? n)
533 (cons (f a1 a2) '())
534 (die who "list was altered"))]
535 [else
536 (die who
537 (if (list? d2) "length mismatch" "not a proper list"))])]
538 [else (die who "list was altered")])))
539 (define cars
540 (lambda (ls*)
541 (cond
542 [(null? ls*) '()]
543 [else
544 (let ([a (car ls*)])
545 (cond
546 [(pair? a)
547 (cons (car a) (cars (cdr ls*)))]
548 [else
549 (die 'map "length mismatch")]))])))
550 (define cdrs
551 (lambda (ls*)
552 (cond
553 [(null? ls*) '()]
554 [else
555 (let ([a (car ls*)])
556 (cond
557 [(pair? a)
558 (cons (cdr a) (cdrs (cdr ls*)))]
559 [else
560 (die 'map "length mismatch")]))])))
561 (define (err-mutated all-lists)
562 (apply die 'map "some lists were mutated during operation" all-lists))
563 (define (err-mismatch all-lists)
564 (apply die 'map "length mismatch" all-lists))
565 (define (err-invalid all-lists)
566 (apply die 'map "invalid arguments" all-lists))
567 (define mapm
568 (lambda (f ls ls* n all-lists)
569 (cond
570 [(null? ls)
571 (if (andmap null? ls*)
572 (if (fxzero? n)
573 '()
574 (err-mutated all-lists))
575 (err-mismatch all-lists))]
576 [(fxzero? n) (err-mutated all-lists)]
577 [else
578 (cons
579 (apply f (car ls) (cars ls*))
580 (mapm f (cdr ls) (cdrs ls*) (fxsub1 n) all-lists))])))
581 (define map
582 (case-lambda
583 [(f ls)
584 (unless (procedure? f)
585 (die who "not a procedure" f))
586 (cond
587 [(pair? ls)
588 (let ([d ($cdr ls)])
589 (map1 f ($car ls) d (len d d 0)))]
590 [(null? ls) '()]
591 [else (err-invalid (list ls))])]
592 [(f ls ls2)
593 (unless (procedure? f)
594 (die who "not a procedure" f))
595 (cond
596 [(pair? ls)
597 (if (pair? ls2)
598 (let ([d ($cdr ls)])
599 (map2 f ($car ls) ($car ls2) d ($cdr ls2) (len d d 0)))
600 (err-invalid (list ls ls2)))]
601 [(and (null? ls) (null? ls2)) '()]
602 [else (err-invalid (list ls ls2))])]
603 [(f ls . ls*)
604 (unless (procedure? f)
605 (die who "not a procedure" f))
606 (cond
607 [(pair? ls)
608 (let ([n (len ls ls 0)])
609 (mapm f ls ls* n (cons ls ls*)))]
610 [(and (null? ls) (andmap null? ls*)) '()]
611 [else (err-invalid (cons ls ls*))])])))
612
613 (module (for-each)
614 (define who 'for-each)
615 (define len
616 (lambda (h t n)
617 (if (pair? h)
618 (let ([h ($cdr h)])
619 (if (pair? h)
620 (if (eq? h t)
621 (die who "circular list")
622 (len ($cdr h) ($cdr t) ($fx+ n 2)))
623 (if (null? h)
624 ($fxadd1 n)
625 (die who "improper list"))))
626 (if (null? h)
627 n
628 (die who "improper list")))))
629 (define for-each1
630 (lambda (f a d n)
631 (cond
632 [(pair? d)
633 (if ($fxzero? n)
634 (die who "list was altered!")
635 (begin
636 (f a)
637 (for-each1 f ($car d) ($cdr d) ($fxsub1 n))))]
638 [(null? d)
639 (if ($fxzero? n)
640 (f a)
641 (die who "list was altered"))]
642 [else (die who "list was altered")])))
643 (define for-each2
644 (lambda (f a1 a2 d1 d2 n)
645 (cond
646 [(pair? d1)
647 (cond
648 [(pair? d2)
649 (if ($fxzero? n)
650 (die who "list was altered")
651 (begin
652 (f a1 a2)
653 (for-each2 f
654 ($car d1) ($car d2)
655 ($cdr d1) ($cdr d2)
656 ($fxsub1 n))))]
657 [else (die who "length mismatch")])]
658 [(null? d1)
659 (cond
660 [(null? d2)
661 (if ($fxzero? n)
662 (f a1 a2)
663 (die who "list was altered"))]
664 [else (die who "length mismatch")])]
665 [else (die who "list was altered")])))
666 (define for-each
667 (case-lambda
668 [(f ls)
669 (unless (procedure? f)
670 (die who "not a procedure" f))
671 (cond
672 [(pair? ls)
673 (let ([d ($cdr ls)])
674 (for-each1 f ($car ls) d (len d d 0)))]
675 [(null? ls) (void)]
676 [else (die who "improper list")])]
677 [(f ls ls2)
678 (unless (procedure? f)
679 (die who "not a procedure" f))
680 (cond
681 [(pair? ls)
682 (if (pair? ls2)
683 (let ([d ($cdr ls)])
684 (for-each2 f
685 ($car ls) ($car ls2) d ($cdr ls2) (len d d 0)))
686 (die who "length mismatch"))]
687 [(null? ls)
688 (if (null? ls2)
689 (void)
690 (die who "length mismatch"))]
691 [else (die who "not a list")])]
692 [(f ls . ls*)
693 (unless (procedure? f)
694 (die 'for-each "not a procedure" f))
695 (unless (list? ls)
696 (die 'for-each "not a list" ls))
697 (let ([n (length ls)])
698 (for-each
699 (lambda (x)
700 (unless (and (list? x) (= (length x) n))
701 (die 'for-each "not a list" x)))
702 ls*)
703 (let loop ([n (length ls)] [ls ls] [ls* ls*])
704 (cond
705 [($fx= n 0)
706 (unless (and (null? ls) (andmap null? ls*))
707 (die 'for-each "list modified" f))]
708 [else
709 (unless (and (pair? ls) (andmap pair? ls*))
710 (die 'for-each "list modified" f))
711 (apply f (car ls) (map car ls*))
712 (loop (fx- n 1) (cdr ls) (map cdr ls*))])))])))
713
714 (module (andmap)
715 (define who 'andmap)
716 (define len
717 (lambda (h t n)
718 (if (pair? h)
719 (let ([h ($cdr h)])
720 (if (pair? h)
721 (if (eq? h t)
722 (die who "circular list")
723 (len ($cdr h) ($cdr t) ($fx+ n 2)))
724 (if (null? h)
725 ($fxadd1 n)
726 (die who "improper list"))))
727 (if (null? h)
728 n
729 (die who "improper list")))))
730 (define andmap1
731 (lambda (f a d n)
732 (cond
733 [(pair? d)
734 (if ($fxzero? n)
735 (die who "list was altered!")
736 (and (f a)
737 (andmap1 f ($car d) ($cdr d) ($fxsub1 n))))]
738 [(null? d)
739 (if ($fxzero? n)
740 (f a)
741 (die who "list was altered"))]
742 [else (die who "list was altered")])))
743 (define andmap2
744 (lambda (f a1 a2 d1 d2 n)
745 (cond
746 [(pair? d1)
747 (cond
748 [(pair? d2)
749 (if ($fxzero? n)
750 (die who "list was altered")
751 (and
752 (f a1 a2)
753 (andmap2 f
754 ($car d1) ($car d2)
755 ($cdr d1) ($cdr d2)
756 ($fxsub1 n))))]
757 [else (die who "length mismatch")])]
758 [(null? d1)
759 (cond
760 [(null? d2)
761 (if ($fxzero? n)
762 (f a1 a2)
763 (die who "list was altered"))]
764 [else (die who "length mismatch")])]
765 [else (die who "list was altered")])))
766 (define andmap
767 (case-lambda
768 [(f ls)
769 (unless (procedure? f)
770 (die who "not a procedure" f))
771 (cond
772 [(pair? ls)
773 (let ([d ($cdr ls)])
774 (andmap1 f ($car ls) d (len d d 0)))]
775 [(null? ls) #t]
776 [else (die who "improper list")])]
777 [(f ls ls2)
778 (unless (procedure? f)
779 (die who "not a procedure" f))
780 (cond
781 [(pair? ls)
782 (if (pair? ls2)
783 (let ([d ($cdr ls)])
784 (andmap2 f
785 ($car ls) ($car ls2) d ($cdr ls2) (len d d 0)))
786 (die who "length mismatch"))]
787 [(null? ls)
788 (if (null? ls2)
789 #t
790 (die who "length mismatch"))]
791 [else (die who "not a list")])]
792 [(f ls . ls*)
793 (unless (procedure? f)
794 (die who "not a procedure" f))
795 (die who "vararg not yet supported")])))
796
797
798
799
800 (module (ormap)
801 (define who 'ormap)
802 (define len
803 (lambda (h t n)
804 (if (pair? h)
805 (let ([h ($cdr h)])
806 (if (pair? h)
807 (if (eq? h t)
808 (die who "circular list")
809 (len ($cdr h) ($cdr t) ($fx+ n 2)))
810 (if (null? h)
811 ($fxadd1 n)
812 (die who "improper list"))))
813 (if (null? h)
814 n
815 (die who "improper list")))))
816 (define ormap1
817 (lambda (f a d n)
818 (cond
819 [(pair? d)
820 (if ($fxzero? n)
821 (die who "list was altered!")
822 (or (f a)
823 (ormap1 f ($car d) ($cdr d) ($fxsub1 n))))]
824 [(null? d)
825 (if ($fxzero? n)
826 (f a)
827 (die who "list was altered"))]
828 [else (die who "list was altered")])))
829 (define ormap
830 (case-lambda
831 [(f ls)
832 (unless (procedure? f)
833 (die who "not a procedure" f))
834 (cond
835 [(pair? ls)
836 (let ([d ($cdr ls)])
837 (ormap1 f ($car ls) d (len d d 0)))]
838 [(null? ls) #f]
839 [else (die who "improper list")])]
840 [_ (die who "vararg not supported yet")])))
841
842
843
844 (define partition
845 (letrec ([race
846 (lambda (h t ls p)
847 (if (pair? h)
848 (let ([a0 ($car h)] [h ($cdr h)])
849 (if (pair? h)
850 (if (eq? h t)
851 (die 'partition "circular list" ls)
852 (let ([a1 ($car h)])
853 (let-values ([(a* b*) (race ($cdr h) ($cdr t) ls p)])
854 (if (p a0)
855 (if (p a1)
856 (values (cons* a0 a1 a*) b*)
857 (values (cons a0 a*) (cons a1 b*)))
858 (if (p a1)
859 (values (cons a1 a*) (cons a0 b*))
860 (values a* (cons* a0 a1 b*)))))))
861 (if (null? h)
862 (if (p a0)
863 (values (list a0) '())
864 (values '() (list a0)))
865 (die 'parititon "not a proper list" ls))))
866 (if (null? h)
867 (values '() '())
868 (die 'parition "not a proper list" ls))))])
869 (lambda (p ls)
870 (unless (procedure? p)
871 (die 'partition "not a procedure" p))
872 (race ls ls ls p))))
873
874
875
876 (define-syntax define-iterator
877 (syntax-rules ()
878 [(_ name combine)
879 (module (name)
880 (define who 'name)
881 (define (null*? ls)
882 (or (null? ls) (and (null? (car ls)) (null*? (cdr ls)))))
883 (define (err* ls*)
884 (if (null? ls*)
885 (die who "length mismatch")
886 (if (list? (car ls*))
887 (err* (cdr ls*))
888 (die who "not a proper list" (car ls*)))))
889 (define (cars+cdrs ls ls*)
890 (cond
891 [(null? ls) (values '() '())]
892 [else
893 (let ([a (car ls)])
894 (if (pair? a)
895 (let-values ([(cars cdrs) (cars+cdrs (cdr ls) (cdr ls*))])
896 (values (cons (car a) cars) (cons (cdr a) cdrs)))
897 (if (list? (car ls*))
898 (die who "length mismatch")
899 (die who "not a proper list" (car ls*)))))]))
900 (define (loop1 f a h t ls)
901 (if (pair? h)
902 (let ([b (car h)] [h (cdr h)])
903 (combine (f a)
904 (if (pair? h)
905 (if (eq? h t)
906 (die who "circular" ls)
907 (let ([c (car h)] [h (cdr h)])
908 (combine (f b) (loop1 f c h (cdr t) ls))))
909 (if (null? h)
910 (f b)
911 (combine (f b) (die who "not a proper list" ls))))))
912 (if (null? h)
913 (f a)
914 (combine (f a) (die who "not a proper list" ls)))))
915 (define (loopn f a a* h h* t ls ls*)
916 (if (pair? h)
917 (let-values ([(b* h*) (cars+cdrs h* ls*)])
918 (let ([b (car h)] [h (cdr h)])
919 (combine (apply f a a*)
920 (if (pair? h)
921 (if (eq? h t)
922 (die who "circular" ls)
923 (let-values ([(c* h*) (cars+cdrs h* ls*)])
924 (let ([c (car h)] [h (cdr h)])
925 (combine (apply f b b*)
926 (loopn f c c* h h* (cdr t) ls ls*)))))
927 (if (and (null? h) (null*? h*))
928 (apply f b b*)
929 (combine (apply f b b*) (err* (cons ls ls*))))))))
930 (if (and (null? h) (null*? h*))
931 (apply f a a*)
932 (combine (apply f a a*) (err* (cons ls ls*))))))
933 (define name
934 (case-lambda
935 [(f ls)
936 (unless (procedure? f)
937 (die who "not a procedure" f))
938 (if (pair? ls)
939 (loop1 f (car ls) (cdr ls) (cdr ls) ls)
940 (if (null? ls)
941 (combine)
942 (die who "not a list" ls)))]
943 [(f ls . ls*)
944 (unless (procedure? f)
945 (die who "not a procedure" f))
946 (if (pair? ls)
947 (let-values ([(cars cdrs) (cars+cdrs ls* ls*)])
948 (loopn f (car ls) cars (cdr ls) cdrs (cdr ls) ls ls*))
949 (if (and (null? ls) (null*? ls*))
950 (combine)
951 (err* ls*)))])))]))
952
953 (define-iterator for-all and)
954 (define-iterator exists or)
955
956 (module (fold-left)
957 (define who 'fold-left)
958 (define (null*? ls)
959 (or (null? ls) (and (null? (car ls)) (null*? (cdr ls)))))
960 (define (err* ls*)
961 (if (null? ls*)
962 (die who "length mismatch")
963 (if (list? (car ls*))
964 (err* (cdr ls*))
965 (die who "not a proper list" (car ls*)))))
966 (define (cars+cdrs ls ls*)
967 (cond
968 [(null? ls) (values '() '())]
969 [else
970 (let ([a (car ls)])
971 (if (pair? a)
972 (let-values ([(cars cdrs) (cars+cdrs (cdr ls) (cdr ls*))])
973 (values (cons (car a) cars) (cons (cdr a) cdrs)))
974 (if (list? (car ls*))
975 (die who "length mismatch")
976 (die who "not a proper list" (car ls*)))))]))
977 (define (loop1 f nil h t ls)
978 (if (pair? h)
979 (let ([a (car h)] [h (cdr h)])
980 (if (pair? h)
981 (if (eq? h t)
982 (die who "circular" ls)
983 (let ([b (car h)] [h (cdr h)] [t (cdr t)])
984 (loop1 f (f (f nil a) b) h t ls)))
985 (if (null? h)
986 (f nil a)
987 (die who "not a proper list" ls))))
988 (if (null? h)
989 nil
990 (die who "not a proper list" ls))))
991 (define (loopn f nil h h* t ls ls*)
992 (if (pair? h)
993 (let-values ([(a* h*) (cars+cdrs h* ls*)])
994 (let ([a (car h)] [h (cdr h)])
995 (if (pair? h)
996 (if (eq? h t)
997 (die who "circular" ls)
998 (let-values ([(b* h*) (cars+cdrs h* ls*)])
999 (let ([b (car h)] [h (cdr h)] [t (cdr t)])
1000 (loopn f
1001 (apply f (apply f nil a a*) b b*)
1002 h h* t ls ls*))))
1003 (if (and (null? h) (null*? h*))
1004 (apply f nil a a*)
1005 (err* (cons ls ls*))))))
1006 (if (and (null? h) (null*? h*))
1007 nil
1008 (err* (cons ls ls*)))))
1009 (define fold-left
1010 (case-lambda
1011 [(f nil ls)
1012 (unless (procedure? f)
1013 (die who "not a procedure" f))
1014 (loop1 f nil ls ls ls)]
1015 [(f nil ls . ls*)
1016 (unless (procedure? f)
1017 (die who "not a procedure" f))
1018 (loopn f nil ls ls* ls ls ls*)])))
1019
1020 (module (fold-right)
1021 (define who 'fold-right)
1022 (define (null*? ls)
1023 (or (null? ls) (and (null? (car ls)) (null*? (cdr ls)))))
1024 (define (err* ls*)
1025 (if (null? ls*)
1026 (die who "length mismatch")
1027 (if (list? (car ls*))
1028 (err* (cdr ls*))
1029 (die who "not a proper list" (car ls*)))))
1030 (define (cars+cdrs ls ls*)
1031 (cond
1032 [(null? ls) (values '() '())]
1033 [else
1034 (let ([a (car ls)])
1035 (if (pair? a)
1036 (let-values ([(cars cdrs) (cars+cdrs (cdr ls) (cdr ls*))])
1037 (values (cons (car a) cars) (cons (cdr a) cdrs)))
1038 (if (list? (car ls*))
1039 (die who "length mismatch")
1040 (die who "not a proper list" (car ls*)))))]))
1041 (define (loop1 f nil h t ls)
1042 (if (pair? h)
1043 (let ([a (car h)] [h (cdr h)])
1044 (if (pair? h)
1045 (if (eq? h t)
1046 (die who "circular" ls)
1047 (let ([b (car h)] [h (cdr h)] [t (cdr t)])
1048 (f a (f b (loop1 f nil h t ls)))))
1049 (if (null? h)
1050 (f a nil)
1051 (die who "not a proper list" ls))))
1052 (if (null? h)
1053 nil
1054 (die who "not a proper list" ls))))
1055 (define (loopn f nil h h* t ls ls*)
1056 (if (pair? h)
1057 (let-values ([(a* h*) (cars+cdrs h* ls*)])
1058 (let ([a (car h)] [h (cdr h)])
1059 (if (pair? h)
1060 (if (eq? h t)
1061 (die who "circular" ls)
1062 (let-values ([(b* h*) (cars+cdrs h* ls*)])
1063 (let ([b (car h)] [h (cdr h)] [t (cdr t)])
1064 (apply f a
1065 (append a*
1066 (list
1067 (apply f
1068 b (append b*
1069 (list (loopn f nil h h* t ls ls*))))))))))
1070 (if (and (null? h) (null*? h*))
1071 (apply f a (append a* (list nil)))
1072 (err* (cons ls ls*))))))
1073 (if (and (null? h) (null*? h*))
1074 nil
1075 (err* (cons ls ls*)))))
1076 (define fold-right
1077 (case-lambda
1078 [(f nil ls)
1079 (unless (procedure? f)
1080 (die who "not a procedure" f))
1081 (loop1 f nil ls ls ls)]
1082 [(f nil ls . ls*)
1083 (unless (procedure? f)
1084 (die who "not a procedure" f))
1085 (loopn f nil ls ls* ls ls ls*)]
1086 )))
1087
1088 )
1089