-
Notifications
You must be signed in to change notification settings - Fork 141
/
03-cons-the-magnificent.ss
executable file
·259 lines (228 loc) · 8.2 KB
/
03-cons-the-magnificent.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
;
; Chapter 3 of The Little Schemer:
; Cons the Magnificent
;
; 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
;
; The rember function removes the first occurance of the given atom from the
; given list.
;
(define rember
(lambda (a lat)
(cond
((null? lat) '())
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat)))))))
; Examples of rember function
;
(rember 'mint '(lamb chops and mint flavored mint jelly)) ; '(lamb chops and flavored mint jelly)
(rember 'toast '(bacon lettuce and tomato)) ; '(bacon lettuce and tomato)
(rember 'cup '(coffee cup tea cup and hick cup)) ; '(coffee tea cup and hick cup)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The second commandment ;
; ;
; Use /cons/ to build lists. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The firsts function builds a list of first s-expressions
;
(define firsts
(lambda (l)
(cond
((null? l) '())
(else
(cons (car (car l)) (firsts (cdr l)))))))
; Examples of firsts
;
(firsts '((apple peach pumpkin)
(plum pear cherry)
(grape raisin pea)
(bean carrot eggplant))) ; '(apple plum grape bean)
(firsts '((a b) (c d) (e f))) ; '(a c e)
(firsts '((five plums) (four) (eleven green oranges))) ; '(five four eleven)
(firsts '(((five plums) four)
(eleven green oranges)
((no) more))) ; '((five plums) eleven (no))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The third commandment ;
; ;
; When building lists, describe the first typical element, and then /cons/ ;
; it onto the natural recursion. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The insertR function inserts the element new to the right of the first
; occurence of element old in the list lat
;
(define insertR
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons old (cons new (cdr lat))))
(else
(cons (car lat) (insertR new old (cdr lat)))))))
; Examples of insertR
;
(insertR
'topping 'fudge
'(ice cream with fudge for dessert)) ; '(ice cream with fudge topping for dessert)
(insertR
'jalapeno
'and
'(tacos tamales and salsa)) ; '(tacos tamales and jalapeno salsa)
(insertR
'e
'd
'(a b c d f g d h)) ; '(a b c d e f g d h)
; The insertL function inserts the element new to the left of the first
; occurrence of element old in the list lat
;
(define insertL
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons new (cons old (cdr lat))))
(else
(cons (car lat) (insertL new old (cdr lat)))))))
; Example of insertL
;
(insertL
'd
'e
'(a b c e g d h)) ; '(a b c d e g d h)
; The subst function substitutes the first occurence of element old with new
; in the list lat
;
(define subst
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons new (cdr lat)))
(else
(cons (car lat) (subst new old (cdr lat)))))))
; Example of subst
;
(subst
'topping
'fudge
'(ice cream with fudge for dessert)) ; '(ice cream with topping for dessert)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; Go cons a piece of cake onto your mouth. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The subst2 function substitutes the first occurence of elements o1 or o2
; with new in the list lat
;
(define subst2
(lambda (new o1 o2 lat)
(cond
((null? lat) '())
((or (eq? (car lat) o1) (eq? (car lat) o2))
(cons new (cdr lat)))
(else
(cons (car lat) (subst new o1 o2 (cdr lat)))))))
; Example of subst2
;
(subst2
'vanilla
'chocolate
'banana
'(banana ice cream with chocolate topping)) ; '(vanilla ice cream with chocolate topping)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; If you got the last function, go and repeat the cake-consing. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The multirember function removes all occurances of a from lat
;
(define multirember
(lambda (a lat)
(cond
((null? lat) '())
((eq? (car lat) a)
(multirember a (cdr lat)))
(else
(cons (car lat) (multirember a (cdr lat)))))))
; Example of multirember
;
(multirember
'cup
'(coffee cup tea cup and hick cup)) ; '(coffee tea and hick)
; The multiinsertR function inserts the element new to the right of all
; occurences of element old in the list lat
;
(define multiinsertR
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons old (cons new (multiinsertR new old (cdr lat)))))
(else
(cons (car lat) (multiinsertR new old (cdr lat)))))))
; Example of multiinsertR
;
(multiinsertR
'x
'a
'(a b c d e a a b)) ; (a x b c d e a x a x b)
; The multiinsertL function inserts the element new to the left of all
; occurences of element old in the list lat
;
(define multiinsertL
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons new (cons old (multiinsertL new old (cdr lat)))))
(else
(cons (car lat) (multiinsertL new old (cdr lat)))))))
; Example of multiinsertL
;
(multiinsertL
'x
'a
'(a b c d e a a b)) ; (x a b c d e x a x a b)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The fourth commandment (preliminary) ;
; ;
; Always change at least one argument while recurring. It must be changed to ;
; be closer to termination. The changing argument must be tested in the ;
; termination condition: when using cdr, test the termination with null?. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; The multisubst function substitutes all occurence of element old with new
; in the list lat
;
(define multisubst
(lambda (new old lat)
(cond
((null? lat) '())
((eq? (car lat) old)
(cons new (multisubst new old (cdr lat))))
(else
(cons (car lat) (multisubst new old (cdr lat)))))))
; Example of multisubst
;
(multisubst
'x
'a
'(a b c d e a a b)) ; (x b c d e x x b)
;
; 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
;