-
Notifications
You must be signed in to change notification settings - Fork 141
/
07-friends-and-relations.ss
executable file
·407 lines (348 loc) · 9.2 KB
/
07-friends-and-relations.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
;
; Chapter 7 of The Little Schemer:
; Friends and Relations
;
; Code examples assemled by Peteris Krumins (peter@catonmat.net).
; His blog is at http://www.catonmat.net -- good coders code, great reuse.
;
; Get yourself this wonderful book at Amazon: http://bit.ly/4GjWdP
;
; member function from Chapter 2 (02-do-it-again.ss)
;
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
(else (or (eq? (car lat) a)
(member? a (cdr lat)))))))
; atom? function from Chapter 1 (01-toys.ss)
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
; Example of a set
;
'(apples peaches pears plums)
; Example of not a set
;
'(apple peaches apple plum) ; because 'apple appears twice
; The set? function determines if a given lat is a set
;
(define set?
(lambda (lat)
(cond
((null? lat) #t)
((member? (car lat) (cdr lat)) #f)
(else
(set? (cdr lat))))))
; Examples of set?
;
(set? '(apples peaches pears plums)) ; #t
(set? '(apple peaches apple plum)) ; #f
(set? '(apple 3 pear 4 9 apple 3 4)) ; #f
; The makeset funciton takes a lat and produces a set
;
(define makeset
(lambda (lat)
(cond
((null? lat) '())
((member? (car lat) (cdr lat)) (makeset (cdr lat)))
(else
(cons (car lat) (makeset (cdr lat)))))))
; Example of makeset
;
(makeset '(apple peach pear peach plum apple lemon peach))
; ==> '(pear plum apple lemon peach)
; makeset via multirember from Chapter 3 (03-cons-the-magnificent.ss)
;
(define multirember
(lambda (a lat)
(cond
((null? lat) '())
((eq? (car lat) a)
(multirember a (cdr lat)))
(else
(cons (car lat) (multirember a (cdr lat)))))))
(define makeset
(lambda (lat)
(cond
((null? lat) '())
(else
(cons (car lat)
(makeset (multirember (car lat) (cdr lat))))))))
; Test makeset
;
(makeset '(apple peach pear peach plum apple lemon peach))
; ==> '(apple peach pear plum lemon)
(makeset '(apple 3 pear 4 9 apple 3 4))
; ==> '(apple 3 pear 4 9)
; The subset? function determines if set1 is a subset of set2
;
(define subset?
(lambda (set1 set2)
(cond
((null? set1) #t)
((member? (car set1) set2)
(subset? (cdr set1) set2))
(else #f))))
; Examples of subset?
;
(subset? '(5 chicken wings)
'(5 hamburgers 2 pieces fried chicken and light duckling wings))
; ==> #t
(subset? '(4 pounds of horseradish)
'(four pounds of chicken and 5 ounces of horseradish))
; ==> #f
; A shorter version of subset?
;
(define subset?
(lambda (set1 set2)
(cond
((null? set1) #t)
(else (and (member? (car set1) set2)
(subset? (cdr set1) set2))))))
; Tests of the new subset?
;
(subset? '(5 chicken wings)
'(5 hamburgers 2 pieces fried chicken and light duckling wings))
; ==> #t
(subset? '(4 pounds of horseradish)
'(four pounds of chicken and 5 ounces of horseradish))
; ==> #f
; The eqset? function determines if two sets are equal
;
(define eqset?
(lambda (set1 set2)
(and (subset? set1 set2)
(subset? set2 set1))))
; Examples of eqset?
;
(eqset? '(a b c) '(c b a)) ; #t
(eqset? '() '()) ; #t
(eqset? '(a b c) '(a b)) ; #f
; The intersect? function finds if two sets intersect
;
(define intersect?
(lambda (set1 set2)
(cond
((null? set1) #f)
((member? (car set1) set2) #t)
(else
(intersect? (cdr set1) set2)))))
; Examples of intersect?
;
(intersect?
'(stewed tomatoes and macaroni)
'(macaroni and cheese))
; ==> #t
(intersect?
'(a b c)
'(d e f))
; ==> #f
; A shorter version of intersect?
;
(define intersect?
(lambda (set1 set2)
(cond
((null? set1) #f)
(else (or (member? (car set1) set2)
(intersect? (cdr set1) set2))))))
; Tests of intersect?
;
(intersect?
'(stewed tomatoes and macaroni)
'(macaroni and cheese))
; ==> #t
(intersect?
'(a b c)
'(d e f))
; ==> #f
; The intersect function finds the intersect between two sets
;
(define intersect
(lambda (set1 set2)
(cond
((null? set1) '())
((member? (car set1) set2)
(cons (car set1) (intersect (cdr set1) set2)))
(else
(intersect (cdr set1) set2)))))
; Example of intersect
;
(intersect
'(stewed tomatoes and macaroni)
'(macaroni and cheese))
; ==> '(and macaroni)
; The union function finds union of two sets
;
(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
'(stewed tomatoes and macaroni casserole)
'(macaroni and cheese))
; ==> '(stewed tomatoes casserole macaroni and cheese)
; The xxx function is the set difference function
;
(define xxx
(lambda (set1 set2)
(cond
((null? set1) '())
((member? (car set1) set2)
(xxx (cdr set1) set2))
(else
(cons (car set1) (xxx (cdr set1) set2))))))
; Example of set difference
;
(xxx '(a b c) '(a b d e f)) ; '(c)
; The intersectall function finds intersect between multitude of sets
;
(define intersectall
(lambda (l-set)
(cond
((null? (cdr l-set)) (car l-set))
(else
(intersect (car l-set) (intersectall (cdr l-set)))))))
; Examples of intersectall
;
(intersectall '((a b c) (c a d e) (e f g h a b))) ; '(a)
(intersectall
'((6 pears and)
(3 peaches and 6 peppers)
(8 pears and 6 plums)
(and 6 prunes with some apples))) ; '(6 and)
; 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))))
; Examples of pairs
;
(a-pair? '(pear pear)) ; #t
(a-pair? '(3 7)) ; #t
(a-pair? '((2) (pair))) ; #t
(a-pair? '(full (house))) ; #t
; Examples of not-pairs
(a-pair? '()) ; #f
(a-pair? '(a b c)) ; #f
; 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 '()))))
; Just an example of how you'd write third
;
(define third
(lambda (l)
(car (cdr (cdr l)))))
; Example of a not-relations
;
'(apples peaches pumpkins pie)
'((apples peaches) (pumpkin pie) (apples peaches))
; Examples of relations
;
'((apples peaches) (pumpkin pie))
'((4 3) (4 2) (7 6) (6 2) (3 4))
; The fun? function determines if rel is a function
;
(define fun?
(lambda (rel)
(set? (firsts rel))))
; It uses firsts function from Chapter 3 (03-cons-the-magnificent.ss)
(define firsts
(lambda (l)
(cond
((null? l) '())
(else
(cons (car (car l)) (firsts (cdr l)))))))
; Examples of fun?
;
(fun? '((4 3) (4 2) (7 6) (6 2) (3 4))) ; #f
(fun? '((8 3) (4 2) (7 6) (6 2) (3 4))) ; #t
(fun? '((d 4) (b 0) (b 9) (e 5) (g 4))) ; #f
; The revrel function reverses a relation
;
(define revrel
(lambda (rel)
(cond
((null? rel) '())
(else (cons (build (second (car rel))
(first (car rel)))
(revrel (cdr rel)))))))
; Example of revrel
;
(revrel '((8 a) (pumpkin pie) (got sick)))
; ==> '((a 8) (pie pumpkin) (sick got))
; Let's simplify revrel by using inventing revpair that reverses a pair
;
(define revpair
(lambda (p)
(build (second p) (first p))))
; Simplified revrel
;
(define revrel
(lambda (rel)
(cond
((null? rel) '())
(else (cons (revpair (car rel)) (revrel (cdr rel)))))))
; Test of simplified revrel
;
(revrel '((8 a) (pumpkin pie) (got sick)))
; ==> '((a 8) (pie pumpkin) (sick got))
; The fullfun? function determines if the given function is full
;
(define fullfun?
(lambda (fun)
(set? (seconds fun))))
; It uses seconds helper function
;
(define seconds
(lambda (l)
(cond
((null? l) '())
(else
(cons (second (car l)) (seconds (cdr l)))))))
; Examples of fullfun?
;
(fullfun? '((8 3) (4 2) (7 6) (6 2) (3 4))) ; #f
(fullfun? '((8 3) (4 8) (7 6) (6 2) (3 4))) ; #t
(fullfun? '((grape raisin)
(plum prune)
(stewed prune))) ; #f
; one-to-one? is the same fullfun?
;
(define one-to-one?
(lambda (fun)
(fun? (revrel fun))))
(one-to-one? '((8 3) (4 2) (7 6) (6 2) (3 4))) ; #f
(one-to-one? '((8 3) (4 8) (7 6) (6 2) (3 4))) ; #t
(one-to-one? '((grape raisin)
(plum prune)
(stewed prune))) ; #f
(one-to-one? '((chocolate chip) (doughy cookie)))
; ==> #t and you deserve one now!
;
; Go get yourself this wonderful book and have fun with these examples!
;
; Shortened URL to the book at Amazon.com: http://bit.ly/4GjWdP
;
; Sincerely,
; Peteris Krumins
; http://www.catonmat.net
;