forked from pkrumins/the-seasoned-schemer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12-take-cover.ss
executable file
·335 lines (298 loc) · 9.03 KB
/
12-take-cover.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
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
;
; Chapter 12 of The Seasoned Schemer:
; Take Cover
;
; 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
;
; add1 primitive
;
(define add1
(lambda (x) (+ x 1)))
; The Y-combinator
;
(define Y
(lambda (le)
((lambda (f) (f f))
(lambda (f)
(le (lambda (x) ((f f) x)))))))
; length function written via Y-combinator and applied to '(a b c)
;
((Y (lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (length (cdr l)))))))) '(a b c)) ; produces output 3
; No need to pass 'a' around in multirember
; Use Y-combinator not to pass it around
;
(define multirember
(lambda (a lat)
((Y (lambda (mr)
(lambda (lat)
(cond
((null? lat) '())
((eq? a (car lat)) (mr (cdr lat)))
(else
(cons (car lat) (mr (cdr lat))))))))
lat)))
; Example of multirember
;
(multirember 'a '(a b c a a a x)) ; '(b c x)
; multirember via letrec
;
(define multirember-letrec
(lambda (a lat)
((letrec
((mr (lambda (lat)
(cond
((null? lat) '())
((eq? a (car lat)) (mr (cdr lat)))
(else
(cons (car lat) (mr (cdr lat))))))))
mr)
lat)))
; Example of multirember-letrec
;
(multirember-letrec 'a '(a b c a a a x)) ; '(b c x)
; Structure of letrec
;
; ((letrec ((mr ...)) mr) values)
; Another way to write multirember via letrec
;
(define multirember-letrec-2
(lambda (a lat)
(letrec
((mr (lambda (lat)
(cond
((null? lat) '())
((eq? a (car lat)) (mr (cdr lat)))
(else
(cons (car lat) (mr (cdr lat))))))))
(mr lat))))
; Another test of applying multirember-letrec-2
;
(multirember-letrec-2 'a '(a b c a a a x)) ; '(b c x)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The twelfth commandment ;
; ;
; Use (letrec ...) to remove arguments that do not change for recursive ;
; applications. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Yet another way to write multirember via letrec
;
(define multirember-letrec-3
(letrec
((mr (lambda (a lat)
(cond
((null? lat) '())
((eq? (car lat) a) (mr a (cdr lat)))
(else
(cons (car lat) (mr a (cdr lat))))))))
mr))
; Test multirember-letrec-3
;
(multirember-letrec-3 'a '(a b c a a a x)) ; '(b c x)
; The member? function determines if the given element is in the list
;
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
((eq? (car lat) a) #t)
(else (member? a (cdr lat))))))
; Test member?
;
(member? 'x '(a b c x d e f)) ; #t
(member? 'x '(a b c d e f)) ; #f
; member? via letrec
;
(define member-letrec?
(lambda (a l)
((letrec
((yes? (lambda (l)
(cond
((null? l) #f)
((eq? (car l) a) #t)
(else (yes? (cdr l)))))))
yes?)
l)))
; Test member-letrec?
;
(member-letrec? 'x '(a b c x d e f)) ; #t
(member-letrec? 'x '(a b c d e f)) ; #f
; Another member? via letrec
;
(define member-letrec-2?
(lambda (a l)
(letrec
((yes? (lambda (l)
(cond
((null? l) #f)
((eq? (car l) a) #t)
(else (yes? (cdr l)))))))
(yes? l))))
; Test member-letrec-2?
;
(member-letrec-2? 'x '(a b c x d e f)) ; #t
(member-letrec-2? 'x '(a b c d e f)) ; #f
; The union function takes two sets and merges them
;
(define union
(lambda (set1 set2)
(cond
((null? set1) set2)
((member? (car set1) set2)
(union (cdr set1) set2))
(else
(cons (car set1) (union (cdr set1) set2))))))
; Example of union
;
(union
'(tomatoes and macaroni casserole)
'(macaroni and cheese)) ; '(tomatoes and macaroni casserole cheese)
; union via letrec
;
(define union-letrec
(lambda (set1 set2)
(letrec
((U (lambda (set)
(cond
((null? set) set2)
((member? (car set) set2)
(U (cdr set)))
(else
(cons (car set) (U (cdr set))))))))
(U set1))))
; Test of union-letrec
;
(union-letrec
'(tomatoes and macaroni casserole)
'(macaroni and cheese)) ; '(tomatoes and macaroni casserole cheese)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The thirteenth commandment ;
; ;
; Use (letrec ...) to hide and to protect functions. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define union-letrec-protected
(lambda (set1 set2)
(letrec
((U (lambda (set)
(cond
((null? set) set2)
((M? (car set) set2)
(U (cdr set)))
(else
(cons (car set) (U (cdr set)))))))
(M?
(lambda (a lat)
(cond
((null? lat) #f)
((eq? (car lat) a) #t)
(else
(M? a (cdr lat)))))))
(U set1))))
; Test of union-letrec-protected
;
(union-letrec-protected
'(tomatoes and macaroni casserole)
'(macaroni and cheese)) ; '(tomatoes and macaroni casserole cheese)
; Fixing M? to follow 12th commandment
;
(define union-letrec-protected-12
(lambda (set1 set2)
(letrec
((U (lambda (set)
(cond
((null? set) set2)
((M? (car set) set2)
(U (cdr set)))
(else
(cons (car set) (U (cdr set)))))))
(M?
(lambda (a lat)
(letrec
((N? (lambda (lat)
(cond
((null? lat) #f)
((eq? (car lat) a) #t)
(else
(N? (cdr lat)))))))
(N? lat)))))
(U set1))))
; Test of union-letrec-protected-12
;
(union-letrec-protected-12
'(tomatoes and macaroni casserole)
'(macaroni and cheese)) ; '(tomatoes and macaroni casserole cheese)
; The two-in-a-row? checks if a lat contains two equal elements
;
(define two-in-a-row?
(lambda (lat)
(letrec
((W (lambda (a lat)
(cond
((null? lat) #f)
((eq? a (car lat)) #t)
(else
(W (car lat) (cdr lat)))))))
(cond
((null? lat) #f)
(else (W (car lat) (cdr lat)))))))
; Test two-in-a-row?
;
(two-in-a-row? '(Italian sardines spaghetti parsley)) ; #f
(two-in-a-row? '(Italian sardines sardines spaghetti parsley)) ; #t
(two-in-a-row? '(Italian sardines more sardines spaghetti)) ; #f
; Another version of two-in-a-row?
;
(define two-in-a-row-2?
(letrec
((W (lambda (a lat)
(cond
((null? lat) #f)
((eq? a (car lat)) #t)
(else
(W (car lat) (cdr lat)))))))
(lambda (lat)
(cond
((null? lat) #f)
(else (W (car lat) (cdr lat)))))))
; Test two-in-a-row-2?
;
(two-in-a-row-2? '(Italian sardines spaghetti parsley)) ; #f
(two-in-a-row-2? '(Italian sardines sardines spaghetti parsley)) ; #t
(two-in-a-row-2? '(Italian sardines more sardines spaghetti)) ; #f
; The sum-of-prefixes finds the running sum of a list of numbers
;
(define sum-of-prefixes
(lambda (tup)
(letrec
((S (lambda (sss tup)
(cond
((null? tup) '())
(else
(cons (+ sss (car tup))
(S (+ sss (car tup)) (cdr tup))))))))
(S 0 tup))))
; Examples of sum-of-prefixes
;
(sum-of-prefixes '(2 1 9 17 0)) ; '(2 3 12 29 29)
(sum-of-prefixes '(1 1 1 1 1)) ; '(1 2 3 4 5)
(sum-of-prefixes '(1 1 1)) ; '(1 2 3)
; TODO: scramble function (i am not interested in it atm)
;
; 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
;