forked from pkrumins/the-little-schemer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09-and-again.ss
301 lines (263 loc) · 6.03 KB
/
09-and-again.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
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
;
; Chapter 8 of The Little Schemer:
; ...and Again, and Again, and Again, ...
;
; Code examples assemled by Jinpu Hu ([email protected]).
; His blog is at http://hujinpu.com -- good coders code, great reuse.
;
; Get yourself this wonderful book at Amazon: http://bit.ly/4GjWdP
;
; The pick function returns the n-th element in a lat
;
(define pick
(lambda (n lat)
(cond
((zero? (sub1 n)) (car lat))
(else
(pick (sub1 n) (cdr lat))))))
; Functions like looking are called partial functions.
;
(define looking
(lambda (a lat)
(keep-looking a (pick 1 lat) lat)))
; Example of looking
;
(looking 'caviar '(6 2 4 caviar 5 7 3)) ; #t
(looking 'caviar '(6 2 grits caviar 5 7 3)) ; #f
; It does not recur on a part of lat.
; It is truly unnatural.
;
(define keep-looking
(lambda (a sorn lat)
(cond
((number? sorn)
(keep-looking a (pick sorn lat) lat))
(else (eq? sorn a )))))
; It is the most partial function.
;
(define eternity
(lambda (x)
(eternity x)))
; Helper functions for working with pairs
;
(define first
(lambda (p)
(car p)))
(define second
(lambda (p)
(car (cdr p))))
(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))
; The function shift takes a pair whose first component is a pair
; and builds a pair by shifting the second part of the first component
; into the second component
;
(define shift
(lambda (pair)
(build (first (first pair))
(build (second (first pair))
(second pair)))))
; Example of shift
;
(shift '((a b) c)) ; '(a (b c))
(shift '((a b) (c d))) ; '(a (b (c d)))
; The a-pair? function determines if it's a pair
;
(define a-pair?
(lambda (x)
(cond
((atom? x) #f)
((null? x) #f)
((null? (cdr x)) #f)
((null? (cdr (cdr x))) #t)
(else #f))))
; We first need to define atom? for Scheme as it's not a primitive
;
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
; align is not a partial function, because it yields a value for every argument.
;
(define align
(lambda (pora)
(cond
((atom? pora) pora)
((a-pair? (first pora))
(align (shift pora)))
(else (build (first pora)
(align (second pora)))))))
; counts the number of atoms in align's arguments
;
(define length*
(lambda (pora)
(cond
((atom? pora) 1)
(else
(+ (length* (first pora))
(length* (second pora)))))))
(define weight*
(lambda (pora)
(cond
((atom? pora) 1)
(else
(+ (* (weight* (first pora)) 2)
(weight* (second pora)))))))
; Example of weight*
;
(weight* '((a b) c)) ; 7
(weight* '(a (b c)) ; 5
; Let's simplify revrel by using inventing revpair that reverses a pair
;
(define revpair
(lambda (p)
(build (second p) (first p))))
(define shuffle
(lambda (pora)
(cond
((atom? pora) pora)
((a-pair? (first pora))
(shuffle (revpair pora)))
(else
(build (first pora)
(shuffle (second pora)))))))
; Example of shuffle
;
(shuffle '(a (b c))) ; '(a (b c))
(shuffle '(a b)) ; '(a b)
(shuffle '((a b) (c d))) ; infinite swap pora Ctrl + c to break and input q to exit
; The one? function is true when n=1
;
(define one?
(lambda (n) (= n 1)))
; not total function
(define C
(lambda (n)
(cond
((one? n) 1)
(else
(cond
((even? n) (C (/ n 2)))
(else
(C (add1 (* 3 n)))))))))
(define A
(lambda (n m)
(cond
((zero? n) (add1 m))
((zero? m) (A (sub1 n) 1))
(else
(A (sub1 n)
(A n (sub1 m)))))))
; Example of A
(A 1 0) ; 2
(A 1 1) ; 3
(A 2 2) ; 7
; length0
;
(lambda (l)
(cond
((null? l) 0)
(else
(add1 (eternity (cdr l))))))
; length<=1
;
(lambda (l)
(cond
((null? l) 0)
(else
(add1
((lambda(l)
(cond
((null? l) 0)
(else
(add1 (eternity (cdr l))))))
(cdr l))))))
; All these programs contain a function that looks like length.
; Perhaps we should abstract out this function.
; rewrite length0
;
((lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (length (cdr l)))))))
eternity)
; rewrite length<=1
;
((lambda (f)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (f (cdr l)))))))
((lambda (g)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (g (cdr l)))))))
eternity))
; make length
;
(lambda (mk-length)
(mk-length eternity))
; rewrite length<=1
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(lambda (l)
(cond
((null? l) 0)
(else
(add1
((mk-length eternity) (cdr l))))))))
; It's (length '(1 2 3 4 5))
;
(((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(lambda (l)
(cond
((null? l) 0)
(else
(add1
((mk-length mk-length) (cdr l))))))))
'(1 2 3 4 5))
; 5
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
((lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else
(add1 (length (cdr l)))))))
(lambda (x)
((mk-length mk-length) x)))))
; move out length function
;
((lambda (le)
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(le (lambda (x)
((mk-length mk-length) x))))))
(lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (length (cdr l))))))))
; Y
;
(lambda (le)
((lambda (mk-length)
(mk-length mk-length))
(lambda (mk-length)
(le (lambda (x)
((mk-length mk-length) x))))))
; it is called the applicative-order Y combinator.
;
(define Y
(lambda (le)
((lambda (f) (f f))
(lambda (f)
(le (lambda (x) ((f f) x)))))))