-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 3.67 all-pairs.rkt
372 lines (327 loc) · 7.28 KB
/
Exercise 3.67 all-pairs.rkt
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#lang racket
; Exercise 3.67. Modify the pairs procedure so that (pairs integers integers) will
; produce the stream of all pairs of integers (i,j) (without the condition i < j).
; Hint: You will need to mix in an additional stream.
; S O L U T I O N
(define (all-pairs s t)
(stream-cons
(list (stream-first s) (stream-first t))
(interleave
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest t))
(interleave
(stream-map (lambda (x) (list x (stream-first t))) (stream-rest s))
(all-pairs (stream-rest s) (stream-rest t))
)
)
)
)
(define (pairs s t)
(stream-cons
(list (stream-first s) (stream-first t))
(interleave
(stream-map (lambda (x) (list (stream-first s) x)) (stream-rest t))
(pairs (stream-rest s) (stream-rest t))
)
)
)
(define (interleave s1 s2)
(if (stream-empty? s1)
s2
(stream-cons
(stream-first s1)
(interleave s2 (stream-rest s1))
)
)
)
(define (partial-sums S)
(stream-cons (stream-first S) (add-streams (partial-sums S) (stream-rest S)))
)
(define (ln2-summands n)
(stream-cons
(/ 1.0 n)
(stream-map - (ln2-summands (+ n 1)))
)
)
(define ln2-stream
(partial-sums (ln2-summands 1))
)
(define (sqrt x tolerance)
(stream-limit (sqrt-stream x) tolerance)
)
(define (stream-limit s t)
(let ((s0 (stream-ref s 0)) (s1 (stream-ref s 1)))
(if (< (abs (- s1 s0)) t)
s1
(stream-limit (stream-rest s) t)
)
)
)
(define (sqrt-stream x)
(define guesses
(stream-cons
1.0
(stream-map
(lambda (guess) (sqrt-improve guess x))
guesses
)
)
)
guesses
)
(define (sqrt-improve guess x)
(average guess (/ x guess))
)
(define (average x y) (/ (+ x y) 2))
(define (square x) (* x x))
(define (euler-transform s)
(let ((s0 (stream-ref s 0)) ; Sn-1
(s1 (stream-ref s 1)) ; Sn
(s2 (stream-ref s 2))) ; Sn+1
(stream-cons
(-
s2
(/ (square (- s2 s1)) (+ s0 (* -2 s1) s2))
)
(euler-transform (stream-rest s))
)
)
)
(define (make-tableau transform s)
(stream-cons
s
(make-tableau transform (transform s))
)
)
(define (accelerated-sequence transform s)
(stream-map stream-first (make-tableau transform s))
)
(define (scale-stream stream factor)
(stream-map (lambda (x) (* x factor)) stream)
)
(define (stream-map proc . argstreams)
; (displayln "Entered stream-map")
(if (stream-empty? (car argstreams))
empty-stream
(stream-cons
(apply proc (map stream-first argstreams))
(apply stream-map (cons proc (map stream-rest argstreams)))
)
)
)
(define (stream-ref s n)
; (display "Entered stream-ref with n = ")
; (display n)
; (newline)
(if (= n 0)
(stream-first s)
(stream-ref (stream-rest s) (- n 1))
)
)
; This procedure produces a stream with a maximum number of count elements from
; the supplied stream
(define (truncate-stream s count)
(if (<= count 0)
empty-stream
(stream-cons
(stream-first s)
(truncate-stream (stream-rest s) (- count 1))
)
)
)
; This procedure displays a finite number of elements from the supplied stream
; as specified by 'count'
(define (display-stream-elements count s)
(if (= 0 count)
(begin
(newline)
'done
)
(begin
(newline)
(display (stream-first s))
(display-stream-elements (- count 1) (stream-rest s))
)
)
)
(define (display-stream s)
(stream-for-each display-line s)
)
(define (display-line x)
(newline)
(display x)
)
(define (indent-and-display pair)
(display-spaces (car pair))
(display pair)
(newline)
)
(define (display-spaces n)
(if (<= n 0)
(void)
(begin
(display " ")
(display-spaces (- n 1))
)
)
)
(define (div-series dividend-series divisor-series)
(cond
((= 0 (stream-first divisor-series))
(error "Denominator should not have a zero constant: " (stream-first divisor-series))
)
(else
(mul-series
dividend-series
(invert-unit-series (scale-stream divisor-series (/ 1 (stream-first divisor-series))))
)
)
)
)
(define (invert-unit-series s)
(stream-cons
1
(mul-series
(scale-stream (stream-rest s) -1)
(invert-unit-series s)
)
)
)
(define (mul-series s1 s2)
(stream-cons
; (a0 * b0) (The following is the constant term of the series resulting from
; the multiplication)
(* (stream-first s1) (stream-first s2))
; The following is the rest of the series starting with the x^1 term
(add-streams
; {a0 * (b1x + b2x^2 + b3x^3 + ...)} +
; {b0 * (a1x + a2x^2 + a3x^3 + ...)} +
(add-streams
(scale-stream (stream-rest s2) (stream-first s1))
(scale-stream (stream-rest s1) (stream-first s2))
)
; {(a1x + a2x^2 + a3x^3 + ...) * (b1x + b2x^2 + b3x^3 + ...)}
(stream-cons
; 0 needs to be prepended to this stream so that the first term of the stream
; is the x^1 term. Only then the outer add-streams will add like terms in the
; two series supplied to it
0
(mul-series (stream-rest s1) (stream-rest s2))
)
)
)
)
(define exp-series
(stream-cons 1 (integrate-series exp-series))
)
; the integral of negative sine is cosine
(define cosine-series
(stream-cons 1 (integrate-series (scale-stream sine-series -1)))
)
; the integral of cosine is sine
(define sine-series
(stream-cons 0 (integrate-series cosine-series))
)
(define tan-series
(div-series sine-series cosine-series)
)
(define (integrate-series s)
(div-streams s integers)
)
(define (add-streams s1 s2)
(stream-map + s1 s2)
)
(define (div-streams s1 s2)
(stream-map / s1 s2)
)
(define ones (stream-cons 1 ones))
(define integers (stream-cons 1 (add-streams ones integers)))
; Test Driver
(define (run-test return-type proc . args)
(define (print-item-list items first-time?)
(cond
((not (pair? items)) (void))
(else
(if (not first-time?)
(display ", ")
(void)
)
(print (car items))
(print-item-list (cdr items) false)
)
)
)
(display "Applying ")
(display proc)
(if (not (null? args))
(begin
(display " on: ")
(print-item-list args true)
)
(void)
)
(newline)
(let ((result (apply proc args)))
(if (not (eq? return-type 'none))
(display "Result: ")
(void)
)
(cond
((procedure? result) ((result 'print)))
; ((eq? return-type 'deque) (print-deque result))
((eq? return-type 'none) (void))
(else
(print result)
(newline)
)
)
)
(newline)
)
(define (execution-time proc . args)
(define start-time (current-milliseconds))
; (display start-time)
; (display " ")
(apply proc args)
(define end-time (current-milliseconds))
; (display end-time)
(display "Execution time of ")
(display proc)
(display ": ")
(- end-time start-time)
)
; Tests
; Test Results
Welcome to DrRacket, version 6.11 [3m].
Language: racket, with debugging; memory limit: 1024 MB.
> (stream-for-each indent-and-display (truncate-stream (all-pairs integers integers) 30))
(1 1)
(1 2)
(2 1)
(1 3)
(2 2)
(1 4)
(3 1)
(1 5)
(2 3)
(1 6)
(4 1)
(1 7)
(3 2)
(1 8)
(5 1)
(1 9)
(2 4)
(1 10)
(6 1)
(1 11)
(3 3)
(1 12)
(7 1)
(1 13)
(2 5)
(1 14)
(8 1)
(1 15)
(4 2)
(1 16)
>