Blob


1 (define (memo-proc proc)
2 (let ((already-run? false) (result false))
3 (lambda ()
4 (if already-run?
5 result
6 (begin (set! already-run? true)
7 (set! result (proc))
8 result)))))
10 (define-syntax mydelay
11 (rsc-macro-transformer
12 (let ((xfmr
13 (lambda (exp)
14 `(memo-proc (lambda () ,exp)))))
15 (lambda (e r)
16 (apply xfmr (cdr e))))))
18 (define (myforce delayed-object)
19 (delayed-object))
21 (define-syntax cons-stream
22 (rsc-macro-transformer
23 (let ((xfmr (lambda (x y) `(cons ,x (mydelay ,y)))))
24 (lambda (e r)
25 (apply xfmr (cdr e))))))
27 (define (stream-car s)
28 (car s))
29 (define (stream-cdr s)
30 (myforce (cdr s)))
31 (define stream-null? null?)
32 (define the-empty-stream '())
34 (define (integers-starting-from n)
35 (cons-stream n (integers-starting-from (+ n 1))))
37 (define (stream-ref s n)
38 (if (= n 0)
39 (stream-car s)
40 (stream-ref (stream-cdr s) (- n 1))))
41 (define (stream-map proc s)
42 (if (stream-null? s)
43 the-empty-stream
44 (cons-stream (proc (stream-car s))
45 (stream-map proc (stream-cdr s)))))
46 (define (stream-for-each proc s)
47 (if (stream-null? s)
48 'done
49 (begin (proc (stream-car s))
50 (stream-for-each proc (stream-cdr s)))))
52 (define (stream-enumerate-interval low high)
53 (if (> low high)
54 the-empty-stream
55 (cons-stream
56 low
57 (stream-enumerate-interval (+ low 1) high))))
58 (define (stream-filter pred s)
59 (if (stream-null? s)
60 the-empty-stream
61 (let ((scar (stream-car s)))
62 (if (pred scar)
63 (cons-stream scar (stream-filter pred (stream-cdr s)))
64 (stream-filter pred (stream-cdr s))))))
66 (define (display-stream s)
67 (stream-for-each display-line s))
68 (define (display-line x)
69 (newline)
70 (display x))
72 (define (test-case actual expected)
73 (newline)
74 (display "Actual: ")
75 (display actual)
76 (newline)
77 (display "Expected: ")
78 (display expected)
79 (newline))
81 ;; Exercise 3.52. Consider the sequence of expressions
83 (define sum 0)
84 (define (accum x)
85 (set! sum (+ x sum))
86 sum)
87 (define seq (stream-map accum (stream-enumerate-interval 1 20)))
88 (test-case sum 1)
89 (define y (stream-filter even? seq))
90 (test-case sum 6)
91 (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
92 seq))
93 (test-case sum 10)
94 (stream-ref y 7)
95 ;; 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136
96 (test-case sum 136)
97 (display-stream z)
98 ;; 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210
99 (newline)
100 (display '(10 15 45 55 105 120 190 210))
102 ;; What is the value of sum after each of the above expressions is evaluated? What is the printed response to evaluating the stream-ref and display-stream expressions? Would these responses differ if we had implemented (delay <exp>) simply as (lambda () <exp>) without using the optimization provided by memo-proc ? Explain.
104 (define (memo-proc proc)
105 proc) ;; get rid of the optimization
106 (define sum 0)
107 (define (accum x)
108 (set! sum (+ x sum))
109 sum)
110 (define seq (stream-map accum (stream-enumerate-interval 1 20)))
111 (test-case sum 1)
112 (define y (stream-filter even? seq))
113 (test-case sum 6)
114 (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
115 seq))
116 ;; 1 8 11 15
117 (test-case sum 15)
118 (stream-ref y 7)
119 ;; y
120 ;; *6 19 *24 *30 37 45 *54 *64 75 87 *100 *114 129 145 *162
121 (test-case sum 162)
122 (display-stream z)
123 ;; *15 167 173 *180 188 197 207 218 *230 243 257 272 288 *305 323 342 362
124 (newline)
125 (display '(15 180 230 305))