-
Notifications
You must be signed in to change notification settings - Fork 1
/
c35-tests.scm
311 lines (233 loc) · 7.85 KB
/
c35-tests.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(load "./c35.scm")
(load "./test.scm")
(load "./prime.scm")
(run-tests
'(
(=? 'ES '())
(=? '(s-null? ES) #t)
(=? '(s-car (s-cons 3 ES)) 3)
(=? '(s-cdr (s-cons 3 ES)) ES)
(=? '(s-car (s-cdr (s-cons 3 (s-cons 4 ES)))) 4)
(=? '(let ((s1 (s-cons 10 ES)))
(s-ref 0 s1))
10)
(=? '(let ((s1 (s-cons 10 (s-cons 20 ES))))
(s-ref 1 s1))
20)
(=?s '(let ((s1 (s-cons 10 (s-cons 20 ES))))
(s-map (lambda(x) (* x x)) s1))
(s-cons 100 (s-cons 400 ES)))
(=? '(let ((res (cons 'result '())))
(s-for-each (lambda(x) (set-cdr! res (cons x (cdr res))))
(s-cons 10 (s-cons 20 ES)))
res)
(list 'result 20 10))
(=?o '(s-display (s-cons 1 (s-cons 2 ES)))
"1\n2\n")
(=?s '(s-cons 1 (s-cons 2 (s-cons 3 ES)))
(list 1 2 3))
(=?s '(s-take 2 (s-cons 1 (s-cons 2 (s-cons 3 ES))))
(list 1 2))
(=?s '(s-enumerate-interval 5 8)
(list 5 6 7 8))
(=?s '(s-filter (lambda(el) (eq? el 3))
(s-cons 1 (s-cons 2 (s-cons 3 ES))))
(list 3))
(=?s '(s-take 2 (s-filter
prime?
(s-enumerate-interval 10000 1000000)))
(list 10007 10009))
; ex. 3.50 : general s-map
(=?s '(s-map (lambda(x y z) (+ x y z))
(s-cons 0 (s-cons 1 (s-cons 2 ES)))
(s-cons 10 (s-cons 11 (s-cons 12 ES)))
(s-cons 90 (s-cons 100 (s-cons 120 ES))))
(list 100 112 134))
; ex. 3.51 : lazy evaluation w/ memoization
(=?o '(let* ((show (lambda(x) (display x) x))
(s (s-map show (s-enumerate-interval 0 10))))
(s-ref 5 s)
(s-ref 7 s))
"01234567")
; ex. 3.52
(=?o '(begin
(define (d x) (display x) (display " "))
(define sum 0)
(define (accum x) (set! sum (+ x sum)) sum)
(define seq (s-map accum (s-enumerate-interval 1 20)))
(d sum)
(define y (s-filter even? seq))
(d sum)
(define z (s-filter (lambda(x) (= (remainder x 5) 0)) seq))
(d sum)
(s-ref 7 y)
(d sum)
(with-output-to-string (lambda() (s-display z))) ; don't print
(d sum))
"1 6 10 136 210 ")
(=?s '(s-take 117 integers)
(s-enumerate-interval 1 117))
(=? '(s-ref 100 no-sevens)
117)
(=? '(s-ref 5 fibs)
5)
; sieve of Eratosthenes
(=? '(s-ref 50 primes)
233)
(=? '(s-ref 5 fibs_)
5)
; lazy self-referential primes
(=? '(s-ref 50 primes_)
233)
; Ex. 3.53 will return powers of two
(=? '(begin
(define s (s-cons 1 (add-streams s s)))
(s-ref 6 s))
64)
; repeat
(=?s '(s-take 5 (s-repeat 1))
(list 1 1 1 1 1))
; Ex. 3.54
(=?s '(s-take 5 (mul-streams (integers-from 1)
(integers-from 2)))
(list 2 6 12 20 30))
(=?s '(s-take 8 factorials)
(list 1 1 2 6 24 120 720 5040))
; stream to list (for debugging)
(=? '(s-to-list 3 (s-cons 1 (s-cons 2 ES)))
(list 1 2))
; ex. 3.55
(=? '(s-to-list 5 (partial-sums integers))
(list 1 3 6 10 15))
(=? '(s-to-list 5 (partial-sums (s-repeat 1)))
(list 1 2 3 4 5))
; ex. 3.56: Hamming numbers
(=?s '(s-merge (s-cons 1 (s-cons 3 ES))
(s-cons 2 (s-cons 3 (s-cons 4 ES))))
(list 1 2 3 4))
(=? '(s-to-list 5 (s-scale 2 integers))
(list 2 4 6 8 10))
(=? '(s-to-list 20 hamming)
(list 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36))
; ex. 3.57 n times
; ex. 3.58
(=? '(s-to-list 7 (s-expand 1 7 10))
(list 1 4 2 8 5 7 1))
; exs. 3.59-3.62 -- TODO
(=? '(s-ref 1 (sqrt-s 2))
1.5)
(=?~ '(s-ref 10 (sqrt-s 2))
1.414213)
(=?~ '(s-ref 4 pi-s)
3.33968253968254)
(=?~ '(s-ref 4 (euler-t pi-s))
3.14271284271284)
(=?~ '(s-ref 4 (accel-seq euler-t pi-s))
3.1415927140337)
; ex. 3.63
; This implementation has to map the lambda(guess) function for all
; previous elelments of the stream every time stream-cdr is called.
; The performance hit would remain if the implementation of delay was not
; memoized.
; ex. 3.64
(=?~ '(s-limit 0.01 pi-s)
3.146)
(=?~ '(sqrt_ 16 4.0)
5.191)
(=?~ '(sqrt_ 16 0.01)
4.00)
; ex. 3.65
; lg(2) is ~ 0.69314718
(=?~ '(s-ref 4 naive-ln2)
0.78333)
(=?~ '(s-ref 4 (euler-t naive-ln2))
0.69358)
(=?~ '(s-ref 2 (accel-seq euler-t naive-ln2))
0.69314718)
(=? '(s-to-list 8 prime-sum-pairs-s)
'( (1 . 1) (1 . 2) (2 . 3) (1 . 4) (1 . 6) (3 . 4) (2 . 5) (1 . 10 )))
; ex. 3.66
(=? '(s-find (lambda(x) (= x -1)) (s-take 10 integers))
#f)
(=? '(s-find (lambda(x) (= x 1)) integers)
0)
(=? '(s-find (lambda(x) (= x 10)) integers)
9)
(=? '(s-find (lambda(x) (equal? x '(1 . 100))) (pairs-s integers integers))
197) ; takes quite some time, consider commenting out
; ex. 3.67
(=? '(s-to-list 5 (all-pairs-s integers integers))
'( (1 . 1) (1 . 2) (2 . 1) (1 . 3) (2 . 2)))
(=? '(s-find (lambda(x) (equal? x `(1 . 5))) (all-pairs-s integers integers))
7)
(=? '(s-find (lambda(x) (equal? x `(5 . 1))) (all-pairs-s integers integers))
14)
; ex. 3.69
(=? '(p-triple? (list 1 2 3)) #f)
(=? '(p-triple? (list 3 4 5)) #t)
(=? '(p-triple? (list 4 3 5)) #f)
(=? '(p-triple? (list 5 4 3)) #f)
(=? '(s-to-list 9 (interleave3 integers (s-repeat 99) integers))
'( 1 99 1 2 99 2 3 99 3))
(=? '(s-find (lambda(x) (equal? x (list 2 1 2)))
(s-take 100 (triples-s integers integers integers)))
#f)
(=? '(s-find (lambda(x) (equal? x (list 2 3 2)))
(s-take 100 (triples-s integers integers integers)))
#f)
(=? '(s-find (lambda(x) (equal? x (list 3 2 1)))
(s-take 100 (triples-s integers integers integers)))
#f)
(=? '(s-find (lambda(x) (equal? x (list 1 1 2)))
(s-take 100 (triples-s integers integers integers)))
1)
(=? '(s-find (lambda(x) (equal? x (list 1 2 3)))
(s-take 100 (triples-s integers integers integers)))
4)
(=? '(s-to-list 5 (triples-s integers integers integers))
'((1 1 1) (1 1 2) (1 2 2) (2 2 2) (1 2 3)))
(=? '(s-to-list 2 p-triples-s)
'((3 4 5) (5 12 13)))
;; it takes ages to get the third entry
;; (=? '(s-to-list 3 p-triples-s)
;; '((3 4 5) (5 12 13) (7 24 25))); (8 15 17)))
; ex. 3.70
; a
(=? '(s-monotonic? 10 integers)
#t)
(=? '(s-monotonic? 10 (s-cons 10 (s-cons 9 ES)))
#f)
(=? '(s-monotonic? 100 (s-map pair-sum (pairs-by pair-sum integers integers)))
#t)
(=? '(s-monotonic? 100 (s-map pair-prod (pairs-by pair-prod integers integers)))
#t)
; b
(=? '(s-monotonic? 50 (s-map pair-235 (pairs-by pair-235 no235s no235s)))
#t)
; ex. 3.71
(=? '(s-to-list 3 (s-conseq 2 equal? (s-repeat 'hello)))
'( (hello hello)
(hello hello)
(hello hello)))
(=? '(s-to-list 1 (s-conseq 3 equal? (s-repeat (cons 'hello 'hello))))
'( ((hello . hello) (hello . hello) (hello . hello))))
(=? '(s-to-list 5 ramanujan)
'(((9 . 10) (1 . 12))
((9 . 15) (2 . 16))
((18 . 20) (2 . 24))
((19 . 24) (10 . 27))
((18 . 30) (4 . 32))))
(=? '(s-to-list 6 (s-map (lambda(l) (sum-of-cubes (car l))) ramanujan))
'(1729 4104 13832 20683 32832 39312))
; ex. 3.72
(=? '(s-to-list 4 (s-zip integers integers))
'( (1 . 1) (2 . 2) (3 . 3) (4 . 4)))
(=? '(s-to-list
4
(s-zip (s-map (lambda(l) (sum-of-squares (car l))) square-triplets)
square-triplets))
'((325 (10 . 15) (6 . 17) (1 . 18))
(425 (13 . 16) (8 . 19) (5 . 20))
(650 (17 . 19) (11 . 23) (5 . 25))
(725 (14 . 23) (10 . 25) (7 . 26))))
))