forked from pkrumins/the-little-schemer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
10-value-of-all-of-this.ss
executable file
·388 lines (341 loc) · 8.49 KB
/
10-value-of-all-of-this.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
;
; Chapter 10 of The Little Schemer:
; What Is the Value of All This?
;
; 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
;
; We'll need atom?
;
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
; An entry is a pair of lists whose first list is a set. The two lists must be
; of equal length.
; Here are some entry examples.
;
'((appetizer entree bevarage)
(pate boeuf vin))
'((appetizer entree bevarage)
(beer beer beer))
'((bevarage dessert)
((food is) (number one with us)))
; Let's build entries with build from chapter 7 (07-friends-and-relations.ss)
;
(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))
(define new-entry build)
; Test it out and build the example entries above
;
(build '(appetizer entree bevarage)
'(pate boeuf vin))
(build '(appetizer entree bevarage)
'(beer beer beer))
(build '(bevarage dessert)
'((food is) (number one with us)))
; We'll need first and second functions from chapter 7
;
(define first
(lambda (p)
(car p)))
(define second
(lambda (p)
(car (cdr p))))
; And also third, later.
;
(define third
(lambda (l)
(car (cdr (cdr l)))))
; The lookup-in-entry function looks in an entry to find the value by name
;
(define lookup-in-entry
(lambda (name entry entry-f)
(lookup-in-entry-help
name
(first entry)
(second entry)
entry-f)))
; lookup-in-entry uses lookup-in-entry-help helper function
;
(define lookup-in-entry-help
(lambda (name names values entry-f)
(cond
((null? names) (entry-f name))
((eq? (car names) name) (car values))
(else
(lookup-in-entry-help
name
(cdr names)
(cdr values)
entry-f)))))
; Let's try out lookup-in-entry
;
(lookup-in-entry
'entree
'((appetizer entree bevarage) (pate boeuf vin))
(lambda (n) '()))
; ==> 'boeuf
(lookup-in-entry
'no-such-item
'((appetizer entree bevarage) (pate boeuf vin))
(lambda (n) '()))
; ==> '()
; A table (also called an environment) is a list of entries. Here are some
; examples.
;
'()
'(((appetizer entree beverage) (pate boeuf vin))
((beverage dessert) ((food is) (number one with us))))
; The extend-table function takes an entry and a table and adds entry to the
; table
;
(define extend-table cons)
; lookup-in-table finds an entry in a table
;
(define lookup-in-table
(lambda (name table table-f)
(cond
((null? table) (table-f name))
(else
(lookup-in-entry
name
(car table)
(lambda (name)
(lookup-in-table
name
(cdr table)
table-f)))))))
; Let's try lookup-in-table
;
(lookup-in-table
'beverage
'(((entree dessert) (spaghetti spumoni))
((appetizer entree beverage) (food tastes good)))
(lambda (n) '()))
; ==> 'good
; Expressions to actions
;
(define expression-to-action
(lambda (e)
(cond
((atom? e) (atom-to-action e))
(else
(list-to-action e)))))
; Atom to action
;
(define atom-to-action
(lambda (e)
(cond
((number? e) *const)
((eq? e #t) *const)
((eq? e #f) *const)
((eq? e 'cons) *const)
((eq? e 'car) *const)
((eq? e 'cdr) *const)
((eq? e 'null?) *const)
((eq? e 'eq?) *const)
((eq? e 'atom?) *const)
((eq? e 'zero?) *const)
((eq? e 'add1) *const)
((eq? e 'sub1) *const)
((eq? e 'number?) *const)
(else *identifier))))
; List to action
;
(define list-to-action
(lambda (e)
(cond
((atom? (car e))
(cond
((eq? (car e) 'quote) *quote)
((eq? (car e) 'lambda) *lambda)
((eq? (car e) 'cond) *cond)
(else *application)))
(else *application))))
; The value function takes an expression and evaulates it
;
(define value
(lambda (e)
(meaning e '())))
; The meaning function translates an expression to its meaning
;
(define meaning
(lambda (e table)
((expression-to-action e) e table)))
; Now the various actions. Let's start with *const
;
(define *const
(lambda (e table)
(cond
((number? e) e)
((eq? e #t) #t)
((eq? e #f) #f)
(else
(build 'primitive e)))))
; *quote: (quote text)
;
(define *quote
(lambda (e table)
(text-of e)))
; text-of
;
(define text-of second)
; *identifier
;
(define *identifier
(lambda (e table)
(lookup-in-table e table initial-table)))
; initial-table
;
(define initial-table
(lambda (name)
(car '()))) ; let's hope we don't take this path
; *lambda
;
(define *lambda
(lambda (e table)
(build 'non-primitive
(cons table (cdr e)))))
; Let's add helper functions
;
(define table-of first)
(define formals-of second)
(define body-of third)
; cond takes lines, and returns the value for the first true line
;
(define evcon
(lambda (lines table)
(cond
((else? (question-of (car lines)))
(meaning (answer-of (car lines)) table))
((meaning (question-of (car lines)) table)
(meaning (answer-of (car lines)) table))
(else
(evcon (cdr lines) table))))) ; we don't ask null?, better one of cond lines be true!
; evcon needs else?, question-of and answer-of
;
(define else?
(lambda (x)
(cond
((atom? x) (eq? x 'else))
(else #f))))
(define question-of first)
(define answer-of second)
; Now we can write the real *cond
;
(define *cond
(lambda (e table)
(evcon (cond-lines-of e) table)))
(define cond-lines-of cdr)
; evlis finds meaning of arguments
;
(define evlis
(lambda (args table)
(cond
((null? args) '())
(else
(cons (meaning (car args) table)
(evlis (cdr args) table))))))
; Finally the *application
;
(define *application
(lambda (e table)
(applyz
(meaning (function-of e) table)
(evlis (arguments-of e) table))))
(define function-of car)
(define arguments-of cdr)
; Is the function a primitive?
;
(define primitive?
(lambda (l)
(eq? (first l) 'primitive)))
; Is the function a non-primitive?
;
(define non-primitive?
(lambda (l)
(eq? (first l) 'non-primitive)))
; Apply!
;
(define applyz
(lambda (fun vals)
(cond
((primitive? fun)
(apply-primitive (second fun) vals))
((non-primitive? fun)
(apply-closure (second fun) vals)))))
; apply-primitive
;
(define apply-primitive
(lambda (name vals)
(cond
((eq? name 'cons)
(cons (first vals) (second vals)))
((eq? name 'car)
(car (first vals)))
((eq? name 'cdr)
(cdr (first vals)))
((eq? name 'null?)
(null? (first vals)))
((eq? name 'eq?)
(eq? (first vals) (second vals)))
((eq? name 'atom?)
(:atom? (first vals)))
((eq? name 'zero?)
(zero? (first vals)))
((eq? name 'add1)
(+ 1 (first vals)))
((eq? name 'sub1)
(- 1 (first vals)))
((eq? name 'number?)
(number? (first vals))))))
; :atom?
;
(define :atom?
(lambda (x)
(cond
((atom? x) #t)
((null? x) #f)
((eq? (car x) 'primitive) #t)
((eq? (car x) 'non-primitive) #t)
(else #f))))
; apply-closure
;
(define apply-closure
(lambda (closure vals)
(meaning
(body-of closure)
(extend-table (new-entry
(formals-of closure)
vals)
(table-of closure)))))
;
; Let's try out our brand new Scheme interpreter!
;
(value '(add1 6)) ; 7
(value '(quote (a b c))) ; '(a b c)
(value '(car (quote (a b c)))) ; 'a
(value '(cdr (quote (a b c)))) ; '(b c)
(value
'((lambda (x)
(cons x (quote ())))
(quote (foo bar baz)))) ; '((foo bar baz))
(value
'((lambda (x)
(cond
(x (quote true))
(else
(quote false))))
#t)) ; 'true
;
; 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
;