forked from pkrumins/the-seasoned-schemer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadme.txt
executable file
·248 lines (170 loc) · 11.1 KB
/
readme.txt
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
This repository contains all the code examples from the book "The Seasoned
Schemer." "The Seasoned Schemer" is the continuation of "The Little Schemer"
(http://bit.ly/4GjWdP) book. It's written in the same style as "The Little
Schemer", i.e., as a dialogue between you and the authors of the book, but the
goal is different - it teaches you to think about the nature of computation.
If you're interested, get the book from Amazon: http://bit.ly/8cyjgw
The code examples were copied (and completed where necessary) from
"The Seasoned Schemer" by Peteris Krumins ([email protected]).
His blog is at http://www.catonmat.net -- good coders code, great reuse.
------------------------------------------------------------------------------
Table of contents:
[11] Chapter 11: Welcome Back to the Show
11-welcome-back-to-the-show.ss
[12] Chapter 12: Take Cover
12-take-cover.ss
[13] Chapter 13: Hop, Skip, and Jump
13-hop-skip-and-jump.ss
[14] Chapter 14: Let There Be Names
14-let-there-be-names.ss
[15] Chapter 15: The Difference Between Men and Boys...
15-men-and-boys.ss
[16] Chapter 16: Ready, Set, Bang!
16-ready-set-bang.ss
[17] Chapter 17: We Change, Therefore We Are!
17-we-change-therefore-we-are.ss
[18] Chapter 18: We Change, Therefore We Are The Same!
18-we-change-therefore-we-are-the-same.ss
[19] Chapter 19: Absconding with the Jewels
19-jewels.ss
[20] Chapter 20: What's in Store?
20-whats-in-store.ss
[11]-Chapter-11-Welcome-Back-to-the-Show--------------------------------------
See 11-welcome-back-to-the-show.ss file for code examples.
Chapter 11 welcomes you back to Scheme. It starts with a problem of
determining whether any atom occurs twice in a row. Next a problem of finding
the running sum of a list of numbers is solved.
From the solutions of these two problems the 11th commandment is postulated:
.----------------------------------------------------------------------------.
| The eleventh commandment: |
| |
| Use additional arguments when a function needs to know what the other |
| arguments to the function have been like so far. |
'----------------------------------------------------------------------------'
The chapter ends with the scramble problem. The problem is stated INCORRECTLY
and will confuse you.
It says "The result at each position is found by counting backward from the
current position to this index."
What it should say is this: "The result at each position is found by counting
backward from the NEXT position to this index." Or in other words:
a'_{i} = a{i+1 - a_{i}}.
[12]-Chapter-12-Take-Cover----------------------------------------------------
See 12-take-cover.ss file for code examples.
Chapter 12 introduces the letrec feature of Scheme that allows to remove
arguments that do not change for recursive applications and which hides and
protects functions (creates local scope).
Many examples are shows to make it clear how letrec works. Two commandments
are stated:
.----------------------------------------------------------------------------.
| The twelfth commandment |
| |
| Use (letrec ...) to remove arguments that do not change for recursive |
| applications. |
'----------------------------------------------------------------------------'
And
.----------------------------------------------------------------------------.
| The thirteenth commandment |
| |
| Use (letrec ...) to hide and to protect functions. |
'----------------------------------------------------------------------------'
Now it's time for dessert.
[13]-Chapter-13-Hop-Skip-and-Jump---------------------------------------------
See 13-hop-skip-and-jump.ss file for code examples.
Chapter 13 teaches how to return values from functions abruptly and promptly
via letcc (aka. call-with-current-continuation).
.----------------------------------------------------------------------------.
| The fourteenth commandment |
| |
| Use (letcc ...) to return values abruptly and prompty. |
'----------------------------------------------------------------------------'
This chapter revisits intersect and intersectall functions from The Little
Schemer and rewrites them by applying 12th, 13th and 14th commandments.
[14]-Chapter-14-Let-There-Be-Names--------------------------------------------
See 14-let-there-be-names.ss file for code examples.
Chapter 14 introduces the (let ...) expression that is used to give
expressions names and to avoid evaluating them twice.
The usage of let is first stated as a preliminary version of fifteenth
commandment:
.----------------------------------------------------------------------------.
| The fifteenth commandment (preliminary version) |
| |
| Use (let ...) to name the values of repeated expressions. |
'----------------------------------------------------------------------------'
And as more examples are worked through, as a revised edition:
.----------------------------------------------------------------------------.
| The fifteenth commandment (revised version) |
| |
| Use (let ...) to name the values of repeated expressions in a function |
| definition if they may be evaluated twice for one and the same use of the |
| function. |
'----------------------------------------------------------------------------'
The final version of the fifteenth commandment is yet to come.
[15]-Chapter-15-The-Difference-Between-Men-and-Boys---------------------------
See 15-men-and-boys.ss file for code examples.
Chapter 15 teaches you how to use the (set! ...) expression and how to let
functions remember values.
It presents three commandments:
.----------------------------------------------------------------------------.
| The sixteenth commandment |
| |
| Use (set! ...) only with names defined in (let ...)s. |
| |
'----------------------------------------------------------------------------'
.----------------------------------------------------------------------------.
| 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. |
'----------------------------------------------------------------------------'
[16]-Chapter-16-Ready-Set-Bang!-----------------------------------------------
See 16-ready-set-bang.ss file for code examples.
Chapter 16 plays a lot around creating state variables via set!. A state
variable remembers its value between two distinct function calls.
The nineteenth commandment follows:
.----------------------------------------------------------------------------.
| The nineteenth commandment |
| |
| Use (set! ...) to remember valuable things between two distinct uses of a |
| function. |
'----------------------------------------------------------------------------'
Then we return to Y-combinators again. The Y! (Y-bang) combinator is
introduced that's "the applicative-order, imperative Y combinator. It works
almost like the regular Y-combinator, but not quite.
The final version of the seventeenth commandment is presented:
.----------------------------------------------------------------------------.
| The seventeenth commandment (final version) |
| |
| Use (set! x ...) for (let ((x ...)) ...) only if there is at least one |
| (lambda ... between it and the (let ...), or if the new value for x is a |
| function that refers to x |
'----------------------------------------------------------------------------'
[17]-Chapter-17-We-Change-Therefore-We-Are------------------------------------
See 17-we-change-therefore-we-are.ss file for code examples.
Chapter 17 continues to improve the paranthesis pizza wrapping function and
still teaches how to use set! and let correctly.
It also looks at how to count the number of cons calls in a function by using
set!.
[18]-Chapter-18-We-Change-Therefore-We-Are-The-Same---------------------------
See 18-we-change-therefore-we-are-the-same.ss file for code examples.
Chapter 18 implements car, cdr and cons through lambdas. It also talks about
what it means for two lists to be equal and introduces set-cdr! function.
[19]-Chapter-19-Absconding-with-the-Jewels------------------------------------
See 19-jewels.ss file for code examples.
Chapter 19 talks more about continuations and teaches how to use them to
create a generator get-next that yields one value on each function call.
[20]-Chapter-20-What's-in-Store?----------------------------------------------
See 20-whats-in-store.ss file for code examples.
... Going to write about it after I finish assembling code examples from The
Little Schemer, as it's based on knowledge of Chapter 10 of The Little
Schemer.
------------------------------------------------------------------------------
That's it. I hope you find these examples useful when reading "The Seasoned
Schemer" yourself! Go get it at http://bit.ly/8cyjgw, if you haven't already!
Sincerely,
Peteris Krumins
http://www.catonmat.net