forked from prathyvsh/the-little-schemer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-oh-my-gawd-it's-full-of-stars.rkt
256 lines (205 loc) · 6.95 KB
/
05-oh-my-gawd-it's-full-of-stars.rkt
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
#lang racket
(define (atom? x) (or (symbol? x) (number? x)))
;; Helper function to check if two lists are equal
(define (listeq? a b)
(cond
((and (null? a) (null? b)) true)
((or (null? a) (null? b)) false)
(else (cond
((and (list? (first a)) (list? (first b)))
(and (listeq? (first a) (first b))
(listeq? (rest a) (rest b))))
((eq? (first a) (first b)) (listeq? (rest a) (rest b)))
(else false)))))
;; My definition
(define (rember* a l)
(cond
((null? l) '())
((atom? (first l)) (cond
((eq? (first l) a) (rember* a (rest l)))
(else (cons (first l) (rember* a (rest l))))))
(else (cons (rember* a (first l)) (rember* a (rest l))))))
(listeq? (rember* 'cup '((coffee) cup ((tea) cup) (and (hick)) cup))
'((coffee) ((tea)) (and (hick))))
(listeq? (rember* 'sauce '(((tomato sauce))
((bean) sauce)
(and ((flying)) sauce)))
(((tomato)) ((bean)) (and ((flying)))))
;; My definition
(define (insertR* new old l)
(cond
((null? l) '())
((atom? (first l)) (cond
((eq? (first l) old) (cons old (cons new (insertR* new old (rest l)))))
(else (cons (first l) (insertR* new old (rest l))))))
(else (cons (insertR* new old (first l))
(insertR* new old (rest l))))))
(insertR* 'roast 'chuck '((how much (wood))
could
((a (wood) chuck))
(((chuck)))
(if (a) ((wood chuck)))
could chuck wood))
;; => '((how much (wood)) could ((a (wood) chuck roast)) (((chuck roast))) (if (a) ((wood chuck roast))) could chuck roast wood))
;; My definition
(define (occur* a l)
(cond
((null? l) 0)
((atom? (first l)) (cond
((eq? (first l) a) (add1 (occur* a (rest l))))
(else (occur* a (rest l)))))
(else (+ (occur* a (first l))
(occur* a (rest l))))))
(occur* 'banana '((banana)
(split ((((banana ice)))
(cream (banana))
sherbet))
(banana)
(bread)
(banana brandy)))
;; => 5
;; My definition
(define (subst* new old l)
(cond
((null? l) '())
((atom? (first l)) (cond
((eq? (first l) old) (cons new (subst* new old (rest l))))
(else (cons (first l) (subst* new old (rest l))))))
(else (cons (subst* new old (first l))
(subst* new old (rest l))))))
(subst* 'orange 'banana '((banana)
(split ((((banana ice)))
(cream (banana))
sherbet))
(banana)
(bread)
(banana brandy)))
;; '((orange) (split ((((orange ice))) (cream (orange)) sherbet)) (orange) (bread) (orange brandy))
;; My definition
(define (insertL* new old l)
(cond
((null? l) '())
((atom? (first l)) (cond
((eq? (first l) old) (cons new (cons old (insertL* new old (rest l)))))
(else (cons (first l) (insertL* new old (rest l))))))
(else (cons (insertL* new old (first l))
(insertL* new old (rest l))))))
(insertL* 'pecker 'chuck '((how much (wood))
could
((a (wood) chuck))
(((chuck)))
(if (a) ((wood chuck)))
could chuck wood))
;; '((how much (wood)) could ((a (wood) pecker chuck)) (((pecker chuck))) (if (a) ((wood pecker chuck))) could pecker chuck wood)
;; My definition
(define (member* a l)
(cond
((null? l) #f)
((atom? (first l)) (or (eq? (first l) old) (member* a (rest l))))
(else (or (member* a (first l))
(member* a (rest l))))))
(member* 'chips '((potato) (chips ((with) fish) (chips))))
;; => #t
(define (leftmost l)
(cond
((atom? (first l)) (first l))
(else (leftmost (first l)))))
(leftmost '((potato) (chips ((with) fish) chips)))
;; => potato
(leftmost '(((hot) (tuna (and))) cheese))
;; => hot
(define x 'pizza)
(define l '(mozzarella pizza))
(and (atom? (first l))
(eq? (first l) x))
;; => #f
(define x 'pizza)
(define l '(pizza pizza))
(and (atom? (first l))
(eq? (first l) x))
;; => #t
;; My definition
(define (eqlist? l1 l2)
(cond
((and (null? l1) (null? l2)) #t)
((and (atom? l1) (atom? l2)) (eqan? l1 l2))
((or (atom? l1) (atom? l2)) false)
((and (atom? (first l1)) (atom? (first l2)))
(and (eqan? (first l1) (first l2)) (eqlist? (rest l1) (rest l2))))
(else (and (eqlist? (first l1) (first l2))
(eqlist? (rest l1) (rest l2))))))
(eqlist? '(strawberry ice cream)
'(strawberry ice cream))
;; => #t
(eqlist? '(strawberry ice cream)
'(strawberry cream ice))
;; => #f
(eqlist? '(banana ((split)))
'((banana) (split)))
;; => #f
(eqlist? '(beef ((sausage)) (and (soda)))
'(beef ((salami)) (and (soda))))
;; => #f
(eqlist? '(beef ((sausage)) (and (soda)))
'(beef ((sausage)) (and (soda))))
;; => #t
(eqlist? '(banana (()) () (split))
'(banana (()) () (split ())))
;; => #f
(eqlist? '(banana (()) () (split))
'(banana (()) () (split)))
;; => #t
;; An S-exp is either an atom or a list of S-expressions.
(define (equal? s1 s2)
(cond
((and (atom? s1)
(atom? s2))
(eq? s1 s2))
((or (atom? s1)
(atom? s2)) #f)
(else (eqlist? s1 s2))))
(define (eqlist? l1 l2)
(cond
((and (null? l1) (null? l2)) #t)
((or (null? l1) (null? l2)) #f)
(else (and (equal? (first l1) (first l2))
(eqlist? (rest l1)
(rest l2))))))
(eqlist? '(strawberry ice cream)
'(strawberry ice cream))
;; => #t
(eqlist? '(strawberry ice cream)
'(strawberry cream ice))
;; => #f
(eqlist? '(banana ((split)))
'((banana) (split)))
;; => #f
(eqlist? '(beef ((sausage)) (and (soda)))
'(beef ((salami)) (and (soda))))
;; => #f
(eqlist? '(beef ((sausage)) (and (soda)))
'(beef ((sausage)) (and (soda))))
;; => #t
(eqlist? '(banana (()) () (split))
'(banana (()) () (split ())))
;; => #f
(eqlist? '(banana (()) () (split))
'(banana (()) () (split)))
;; => #t
;; Rember with equal?
(define (rember s l)
(cond
((null? l) '())
((equal? s (first l)) (rest l))
(else (cons (first l) (rember s (rest l))))))
(rember 'potato '(potato salad))
;; => '(salad)
;; Design the functions correctly. Then think about
;; connecting them or using them together.
;; We can start using equal? for all function except
;; for the functions that made possible the construction
;; of equal? in the first place such as eqan?.
;; This was a pretty good chapter. This is probably one of the
;; the best ROIs for a beginner encountering recursion. But from
;; the perspective of someone who has finished HTDP before,
;; this is an exercise in re-inforcing the learnings.