-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSICP-Solutions.tex
376 lines (341 loc) · 9.21 KB
/
SICP-Solutions.tex
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
\documentclass{scrreprt}
% Language-specific settings
\usepackage{polyglossia}
\setdefaultlanguage{american}
% Fonts
\usepackage{fontspec,microtype}
\setmainfont{Linux Libertine O}
\setsansfont{Linux Libertine O}
\setmonofont[Scale=MatchLowercase]{Fira Code}
% Hyperlinks
\usepackage{xcolor}
\definecolor{darkblue}{rgb}{0,0,0.5}
\definecolor{darkgreen}{rgb}{0,0.3,0}
\usepackage[unicode,
pdftitle={Solutions to Structure and Interpretation of Computer Programs},
pdfauthor={Fabienne Ducroquet},
urlcolor=darkblue,
linkcolor=darkgreen,
breaklinks,
colorlinks]{hyperref}
% Headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[RO,LE]{\thepage}
\fancyhead[LO]{\rightmark}
\fancyhead[RE]{\leftmark}
% Source code highlighting
\usepackage{minted}
\newminted[cscm]{scheme}{autogobble,style=lovelace}
\newmintedfile[scm]{scheme}{style=lovelace}
\newmintinline[vscm]{scheme}{style=bw}
% Show subsubsections in the table of contents.
\setcounter{tocdepth}{4}
% Show a less detailed table of contents first.
\usepackage{shorttoc}
% External links to parts of the text on sarabander.github.io.
\usepackage{xstring,xifthen}
% \secref{<sectionnumber>} links to the given section or subsection.
% \secref[<anchor>]{<sectionnumber>} links to the anchor in the given section,
% and still shows “section <sectionnumber>” as the link text.
\newcommand*\secref[2][]{%
\StrCut[2]{#2}{.}{\sectionpart}{\rest}%
\StrSubstitute{\sectionpart}{.}{_002e}[\pagepart]%
\ifthenelse{\isempty{#1}}%
{\StrSubstitute{#2}{.}{_002e}[\anchorpart]%
\href{https://sarabander.github.io/sicp/html/\pagepart.xhtml\#g_t\anchorpart}{section~#2}}%
{\href{https://sarabander.github.io/sicp/html/\pagepart.xhtml\##1}{section~#2}}
}
% Use letters in enumerate.
\usepackage{enumitem}
\setenumerate[0]{label=\alph*.}
% Theorem environments
\usepackage{amssymb,amsmath,amsthm}
\newtheoremstyle{example}{}{}{}{}{\itshape}{:}{ }
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}
\theoremstyle{example}
\newtheorem*{example}{Example}
\newtheoremstyle{remark}{}{}{}{}{\bfseries}{.}{ }
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}
\theoremstyle{remark}
\newtheorem*{remark}{Remark}
\newtheoremstyle{comp}{}{}{\small}{}{\itshape}{:}{\newline}
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}
\theoremstyle{comp}
\newtheorem*{comp}{Complement}
\newtheoremstyle{sicp-exe}{}{}{}{-12pt}{\bfseries}{}{\newline}
{\href{https://sarabander.github.io/sicp/html/\thechapter\_002e\arabic{section}.xhtml\#Exercise-\thechapter\_002e\arabic{exeaux}}
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}}
\theoremstyle{sicp-exe}
\newtheorem{exeaux}{Exercise}[chapter]
% For hyperref's autoref.
\newcommand\exeauxautorefname{exercise}
% Include the exercises in the table of contents.
\NewDocumentEnvironment{exe}{o}
{\IfNoValueTF{#1}
{\exeaux\addcontentsline{toc}{paragraph}%
{Exercise}}
{\exeaux\addcontentsline{toc}{paragraph}%
{Exercise #1}}%
\ignorespaces}
{\endexeaux}
% For inline fractions
\usepackage{xfrac}
% For Fibonacci numbers.
\DeclareMathOperator{\Fib}{Fib}
% For tables.
\usepackage{tabls, array, longtable}
% For exercise 2.24, and some exercises in chapter 3
\usepackage{tikz}
\usetikzlibrary{positioning, arrows.meta, calc, fit, matrix, backgrounds,
decorations.markings, shapes.misc, shapes.geometric}
\newcommand*\var[2]{%
\newlength{#1}
\setlength{#1}{#2}
}
% Definitions for the box-and-pointer diagrams.
\var{\boxsize}{2em}
\var{\round}{3pt}
\tikzset{
box/.style={
minimum size = \boxsize,
rectangle,
rounded corners=\round,
draw,
fill=brown!15,
},
struct name/.style={
minimum height=\boxsize,
},
car/.style={
minimum size = \boxsize,
append after command={
\pgfextra
\draw[sharp corners,fill=brown!15]
(\tikzlastnode.south)
-| (\tikzlastnode.east)
|- (\tikzlastnode.north)
[rounded corners=\round]
-| (\tikzlastnode.west)
|- (\tikzlastnode.south east);
\endpgfextra},
},
cdr/.style={
minimum size = \boxsize,
append after command={
\pgfextra
\draw[sharp corners,fill=brown!15] (\tikzlastnode.south)
-| (\tikzlastnode.west) |- (\tikzlastnode.north)
[rounded corners=\round] -| (\tikzlastnode.east)
|- (\tikzlastnode.south west);
\endpgfextra},
},
cell matrix/.style={
row sep = 1.2\boxsize,
column sep = {\boxsize,between origins}
},
box and pointer/.style={
very thick,
font = \ttfamily,
decoration = {markings,
mark = at position 0 with { \fill circle (3pt); }
},
},
pointer/.style = {
-Stealth
},
box pointer/.style = {
pointer,
postaction = decorate
},
nil/.style = {
Triangle Cap[]-,
shorten >=.8pt,
}
}
\newcommand*\nil[1]{
\draw[nil] (#1.south west) -- (#1.north east);
}
\newcommand*\link[2]{
\draw[box pointer] (#1.base) -- (#2);
}
% Box-and-pointer end.
% Tikz styles and commands for the environments of section 3.2.
\var{\nametoenv}{4mm}
\tikzset{
env/.style={
rectangle,
rounded corners=2pt,
% border
very thick,
draw=teal!80!black,
% filling
fill=teal!10,
inner sep = 2mm,
font=\ttfamily\small,
align=left,
},
global env/.style={
env,
align=left,
inner xsep=1ex,
text width=13cm-2*\pgfkeysvalueof{/pgf/inner xsep},
minimum width=13cm,
minimum height=1.5cm
},
code/.style={
align=left,
font=\ttfamily\small,
}
}
\var{\circleradius}{3mm}
\var{\smallcircleradius}{.8mm}
\newcommand*\pointer[1]{
\draw (#1) circle (\circleradius);
\filldraw (#1) circle (\smallcircleradius);
}
\newcommand*\procedure[3]{
\coordinate[left=\circleradius of #1] (#2);
\coordinate[right=\circleradius of #1] (#3);
\pointer{#2}
\pointer{#3}
}
% Environments end.
% Settings for timing diagrams in section 3.4.
\tikzset{
bank/.style={
circle,
draw,
minimum size=1.2cm,
fill=orange!10,
},
action/.style={
rounded rectangle,
draw,
minimum height=1.5em,
fill=blue!5,
},
time matrix/.style={
column sep=1em,
},
arrow/.style={
rounded corners=10pt,
thick,
->,
},
}
% Settings for the data-path and controller diagrams in section 5.1.
\var{\regheight}{1cm}
\var{\push}{1.5ex}
\tikzset{
% Node matrix
data matrix/.style={
column sep=1.5em,
row sep=2.5em,
nodes = { anchor = center, align = center },
},
controller matrix/.style={
data matrix,
column sep = 3em,
},
% Nodes
data/.style={
draw,
thick,
rounded corners=2pt,
minimum height=\regheight,
},
reg/.style={
data,
rectangle,
inner xsep=.5cm,
fill=gray!12!blue!5,
},
const/.style={
data,
regular polygon,
regular polygon sides=3,
fill=gray!12!green!5,
minimum height=1.7\regheight,
},
op/.style={
data,
trapezium,
trapezium left angle=120,
trapezium right angle=120,
fill=pink!22!yellow!20
},
test/.style={
data,
circle,
fill=gray!12!pink!15,
},
% Controller diagrams
cio/.style={
thick,
rounded corners=2pt,
},
ctest/.style={
cio,
draw,
inner xsep=.4cm,
diamond,
fill=gray!12!pink!15,
},
cbutton/.style={
cio,
draw,
inner xsep=.5cm,
rectangle,
fill=gray!12!blue!5,
minimum height=\regheight,
},
% Paths
path/.style={
thick,
rounded corners=3pt,
},
button/.style={
path,
decoration = {markings,
mark = at position #1 with {
\fill[white] circle (.7071\push);
\draw[-] ++(-.5\push, -.5\push) -- ++(\push, \push);
\draw[-] ++(-.5\push, .5\push) -- ++(\push, -\push);
\clip[draw] circle (.7071\push); }
},
postaction = decorate,
->,
font=\ttfamily,
},
arg/.style={
path,
->,
},
flow/.style={
arg,
},
}
\title{Solutions to Structure and Interpretation of Computer Programs}
\author{Fabienne \textsc{Ducroquet}}
\begin{document}
\maketitle
\pdfbookmark[1]{Short Contents}{shorttoc}
\shorttoc{Short Contents}{1}
\clearpage
\pdfbookmark[1]{\contentsname}{toc}
\tableofcontents
\chapter*{Introduction}
Solutions to most of the exercises of \emph{Structure and Interpretation of
Computer Programs}, second edition, by Harold \textsc{Abelson} and Gerald Jay
\textsc{Sussman} with Julie \textsc{Sussman}.
The answers to these exercises have been tested with the Scheme interpreter from
\href{http://gambitscheme.org}{Gambit Scheme}, at the exception of the exercises
from \secref{2.2.4} about the picture language, which have been written with
\href{http://racket-lang.org}{Racket}, using the \vscm{graphics.ss} library.
\include{ch1}
\include{ch2}
\include{ch3}
\include{ch4}
\include{ch5}
\end{document}