-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathzip.scm
49 lines (40 loc) · 1.28 KB
/
zip.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(define (zip x y)
(if (or (null? x)
(null? y))
'()
(cons (list (car x) (car y))
(zip (cdr x) (cdr y)))))
(define (zip-with fn x y)
(if (or (null? x)
(null? y))
'()
(cons (fn (car x) (car y))
(zip-with fn (cdr x) (cdr y)))))
; zip е частен случай на zip-with
(define (zip x y)
(zip-with list x y))
(define (any? p l)
(and (not (null? l))
(or (p (car l))
(any? p (cdr l)))))
(define (zip-with* fn . ls)
(if (or (null? ls)
(any? null? ls))
'()
(cons (apply fn (map car ls))
(apply zip-with* fn (map cdr ls)))))
(load "../testing/check.scm")
(check (zip '() '()) => '())
(check (zip '(42) '()) => '())
(check (zip '(1 3 5) '(2 4 6)) => '((1 2) (3 4) (5 6)))
(check (zip '(1 3 5) '(2 4 6 8)) => '((1 2) (3 4) (5 6)))
(check (zip-with + '() '()) => '())
(check (zip-with + '(42) '()) => '())
(check (zip-with + '(1 3 5) '(2 4 6)) => '(3 7 11))
(check (zip-with + '(1 3 5) '(2 4 6 8)) => '(3 7 11))
(check (zip-with* + '() '() '()) => '())
(check (zip-with* + '(42) '() '(1 2 3)) => '())
(check (zip-with* + '(1 2 3) '(4 5 6) '(7 8 9)) => '(12 15 18))
(check (zip-with* cons '(1 3 5) '(2 4 6 8 10)) => '((1 . 2) (3 . 4) (5 . 6)))
(check-report)
(check-reset!)