forked from pkrumins/the-seasoned-schemer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path17-we-change-therefore-we-are.ss
executable file
·265 lines (231 loc) · 6.09 KB
/
17-we-change-therefore-we-are.ss
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
;
; Chapter 17 of The Seasoned Schemer:
; We Change, Therefore We Are!
;
; Code examples assemled by Peteris Krumins ([email protected]).
; His blog is at http://www.catonmat.net -- good coders code, great reuse.
;
; Get yourself this wonderful book at Amazon: http://bit.ly/8cyjgw
;
; The atom? primitive
;
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
; The sub1 primitive
;
(define sub1
(lambda (n)
(- n 1)))
; The add1 primitive
;
(define add1
(lambda (n)
(+ n 1)))
; The deep function wraps pizza in m parenthesis
;
(define deep
(lambda (m)
(if (zero? m)
'pizza
(cons (deep (sub1 m)) '()))))
; Examples of deep
;
(deep 3) ; '(((pizza)))
(deep 0) ; 'pizza
; The deepM function remembers calls to deep
;
; The deepM function uses find function to find n in Ns and return
; the correct value from Rs
;
(define find
(lambda (n Ns Rs)
(letrec
((A (lambda (ns rs)
(cond
((null? ns) #f)
((= (car Ns) n) (car Rs))
(else
(A (cdr ns) (cdr rs)))))))
(A ns rs))))
(define deepM
(let ((Rs '())
(Ns '()))
(letrec
((D (lambda (m)
(if (zero? m)
'pizza
(cons (deepM (sub1 m)) '())))))
(lambda (n)
(let ((exists (find n Ns Rs)))
(if (atom? exists)
(let ((result (D n)))
(set! Rs (cons result Rs))
(set! Ns (cons n Ns))
result)
exists))))))
; Examples of deepM
;
(deepM 3) ; '(((pizza)))
(deepM 0) ; 'pizza
; No need to use letrec in deepM
;
(define deepM-letrec
(let ((Rs '())
(Ns '())
(D (lambda (m)
(if (zero? m)
'pizza
(cons (deepM-letrec (sub1 m)) '())))))
(lambda (n)
(let ((exists (find n Ns Rs)))
(if (atom? exists)
(let ((result (D n)))
(set! Rs (cons result Rs))
(set! Ns (cons n Ns))
result)
exists)))))
; Test of the new deepM
;
(deepM-letrec 3) ; '(((pizza)))
(deepM-letrec 0) ; 'pizza
; No need for D in deepM
;
(define deepM-D
(let ((Rs '())
(Ns '()))
(lambda (n)
(let ((exists (find n Ns Rs)))
(if (atom? exists)
(let ((result ((lambda (m)
(if (zero? m)
'pizza
(cons (deepM-D (sub1 m)) '()))) n)))
(set! Rs (cons result Rs))
(set! Ns (cons n Ns))
result)
exists)))))
; Test of the new deepM
;
(deepM-D 3) ; '(((pizza)))
(deepM-D 0) ; 'pizza
; No need for the 2nd lambda
;
(define deepM-2nd
(let ((Rs '())
(Ns '()))
(lambda (n)
(let ((exists (find n Ns Rs)))
(if (atom? exists)
(let ((result (if (zero? n)
'pizza
(cons (deepM-2nd (sub1 n)) '()))))
(set! Rs (cons result Rs))
(set! Ns (cons n Ns))
result)
exists)))))
; Test of the new deepM
;
(deepM-2nd 3) ; '(((pizza)))
(deepM-2nd 0) ; 'pizza
; The consC function counts number of conses needed to build
; n-deep pizza
;
(define consC
(let ((N 0))
(lambda (x y)
(set! N (add1 N))
(cons x y))))
; No way to get N out of this consC, so we need the counter
; function that will hold the count
;
(define counter 0)
; And a modification of consC
;
(define consC
(let ((N 0))
(set! counter (lambda() N))
(lambda (x y)
(set! N (add1 N))
(cons x y))))
; And we need to modify deep
;
(define deep
(lambda (m)
(if (zero? m)
'pizza
(consC (deep (sub1 m)) '()))))
(deep 5) ; '(((((pizza)))))
(counter) ; 5
(deep 7) ; '(((pizza))
(counter) ; 12 ;;; (5 + 7)
; Let's determine how many cons'es are necessary to find
; values of (deep 0) ... (deep 1000).
;
(define supercounter
(lambda (f)
(letrec
((S (lambda (n)
(if (zero? n)
(f n)
(let ()
(f n)
(S (sub1 n)))))))
(S 1000)
(counter))))
; Try out supercounter
;
(supercounter deep) ; 500512 ;;; not 500500 as expected
; Need to wipe out counter before using it again
;
(define set-counter 0)
(define consC
(let ((N 0))
(set! counter (lambda() N))
(set! set-counter
(lambda (x) (set! N x)))
(lambda (x y)
(set! N (add1 N))
(cons x y))))
(set-counter 0)
; Try out supercounter again
;
(supercounter deep)
; How many conses are used by (deepM 5)?
; Need to modify deepM to use consC first.
;
(define deepM-consC
(let ((Rs '())
(Ns '()))
(lambda (n)
(let ((exists (find n Ns Rs)))
(if (atom? exists)
(let ((result
(if (zero? n)
'pizza
(consC (deepM-consC (sub1 n)) '()))))
(set! Rs (cons result Rs))
(set! Ns (cons n Ns))
result)
exists)))))
; How many conses are used by (deepM 5)?
;
(deepM 5) ; '(((((pizza)))))
(counter) ; 500505 ;;; because we forgot to reset counter
(set-counter 0)
(deepM-consC 5) ; '(((((pizza)))))
(counter) ; 5
(deep 7) ;
(counter) ; ??? the book says it should be 0, I get 12, why?
(supercounter deepM-consC) ; ??? how did this work? it didn't take an argument?
; Book has also talks about conses in rember* function but I am not interested
; in it at the moment.
;
; Go get yourself this wonderful book and have fun with the Scheme language!
;
; Shortened URL to the book at Amazon.com: http://bit.ly/8cyjgw
;
; Sincerely,
; Peteris Krumins
; http://www.catonmat.net
;