-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path15-men-and-boys.ss
executable file
·136 lines (105 loc) · 3.75 KB
/
15-men-and-boys.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
;
; Chapter 15 of The Seasoned Schemer:
; The Difference Between Men and Boys...
;
; 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
;
(define x
(cons 'chicago
(cons 'pizza '())))
(set! x 'gone)
(set! x 'skins)
(define gourmet
(lambda (food)
(cons food
(cons x '()))))
(gourmet 'onion) ; '(onion skins)
(set! x 'rings)
(gourmet 'onion) ; '(onion rings)
(define gourmand
(lambda (food)
(set! x food)
(cons food
(cons x '()))))
(gourmand 'potato) ; '(potato potato)
(gourmand 'rice) ; '(rice rice)
(define dinerR
(lambda (food)
(set! x food)
(cons 'milkshake
(cons food '()))))
(dinerR 'onion) ; '(milkshake onion)
(dinerR 'pecanpie) ; '(milkshake pecanpie)
(define omnivore
(let ((x 'minestrone))
(lambda (food)
(set! x food)
(cons food (cons x '())))))
(omnivore 'bouillabaisse) ; '(bouillabaisse bouillabaisse)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The sixteenth commandment ;
; ;
; Use (set! ...) only with names defined in (let ...)s. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define gobbler
(let ((x 'minestrone))
(lambda (food)
(set! x food)
(cons food (cons x '())))))
(gobbler 'gumbo) ; '(gumbo gumbo)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The seventeenth commandment (preliminary version) ;
; ;
; Use (set! ...) for (let ((x ...)) ...) only if there is at least one ;
; (lambda ... between it and the (let ((x ...)) ...). ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
; The eighteenth commandment ;
; ;
; Use (set! x ...) only when the value that x refers to is no longer needed. ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define chez-nous
(lambda ()
(let ((a food))
(set! food x)
(set! x a))))
(define food 'none)
(define glutton
(lambda (x)
(set! food x)
(cons 'more
(cons x
(cons 'more
(cons x
'()))))))
(dinerR 'milk)
(glutton 'cream)
; x is milk
; food is cream
x
food
(chez-nous) ; x is now cream, food is now milk
x
food
(chez-nous) ; x is now milk, food is now cream
x
food
; There is more stuff in the book
;
; 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
;