-
Notifications
You must be signed in to change notification settings - Fork 0
/
capstone.tex
5710 lines (4155 loc) · 451 KB
/
capstone.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[letterpaper]{geometry}
\usepackage{setspace}
\newcommand{\packageGraphicx}{\usepackage{graphicx}}
\newcommand{\packageHyperref}{\usepackage{hyperref}}
\newcommand{\renewrmdefault}{\renewcommand{\rmdefault}{ptm}}
\newcommand{\packageRelsize}{\usepackage{relsize}}
\newcommand{\packageMathabx}{\usepackage{mathabx}}
% Avoid conflicts between "mathabx" and "wasysym":
\newcommand{\packageWasysym}{
\let\leftmoon\relax \let\rightmoon\relax \let\fullmoon\relax \let\newmoon\relax \let\diameter\relax
\usepackage{wasysym}}
\newcommand{\packageTextcomp}{\usepackage{textcomp}}
\newcommand{\packageFramed}{\usepackage{framed}}
\newcommand{\packageHyphenat}{\usepackage[htt]{hyphenat}}
\newcommand{\packageColor}{\usepackage[usenames,dvipsnames]{color}}
\newcommand{\doHypersetup}{\hypersetup{bookmarks=true,bookmarksopen=true,bookmarksnumbered=true}}
\newcommand{\packageTocstyle}{\IfFileExists{tocstyle.sty}{\usepackage{tocstyle}\usetocstyle{standard}}{}}
\newcommand{\packageCJK}{\IfFileExists{CJK.sty}{\usepackage{CJK}}{}}
% this bit intentionally left blank
% This is the default style configuration for Scribble-generated Latex
\packageGraphicx
\packageHyperref
\renewrmdefault
\packageRelsize
\packageMathabx
\packageWasysym
\packageTextcomp
\packageFramed
\packageHyphenat
\packageColor
\doHypersetup
\packageTocstyle
\packageCJK
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Configuration that is especially meant to be overridden:
% Inserted before every ``chapter'', useful for starting each one on a new page:
\newcommand{\sectionNewpage}{}
% Inserted before every book ``part''
\newcommand{\partNewpage}{\sectionNewpage}
% Hooks for actions within the `document' environment:
\newcommand{\preDoc}{}
\newcommand{\postDoc}{}
% Generated by `secref'; first arg is section number, second is section title:
\newcommand{\BookRef}[2]{\emph{#2}}
\newcommand{\ChapRef}[2]{\SecRef{#1}{#2}}
\newcommand{\SecRef}[2]{section~#1}
\newcommand{\PartRef}[2]{part~#1}
% Generated by `Secref':
\newcommand{\BookRefUC}[2]{\BookRef{#1}{#2}}
\newcommand{\ChapRefUC}[2]{\SecRefUC{#1}{#2}}
\newcommand{\SecRefUC}[2]{Section~#1}
\newcommand{\PartRefUC}[2]{Part~#1}
% Variants of the above with a label for an internal reference:
\newcommand{\BookRefLocal}[3]{\hyperref[#1]{\BookRef{#2}{#3}}}
\newcommand{\ChapRefLocal}[3]{\hyperref[#1]{\ChapRef{#2}{#3}}}
\newcommand{\SecRefLocal}[3]{\hyperref[#1]{\SecRef{#2}{#3}}}
\newcommand{\PartRefLocal}[3]{\hyperref[#1]{\PartRef{#2}{#3}}}
\newcommand{\BookRefLocalUC}[3]{\hyperref[#1]{\BookRefUC{#2}{#3}}}
\newcommand{\ChapRefLocalUC}[3]{\hyperref[#1]{\ChapRefUC{#2}{#3}}}
\newcommand{\SecRefLocalUC}[3]{\hyperref[#1]{\SecRefUC{#2}{#3}}}
\newcommand{\PartRefLocalUC}[3]{\hyperref[#1]{\PartRefUC{#2}{#3}}}
% Variants of the above with a section number is empty (i.e., UnNumbered):
\newcommand{\BookRefUN}[1]{\BookRef{}{#1}}
\newcommand{\ChapRefUN}[1]{\SecRefUN{#1}}
\newcommand{\SecRefUN}[1]{``#1''}
\newcommand{\PartRefUN}[1]{\SecRefUN{#1}}
\newcommand{\BookRefUCUN}[1]{\BookRefUN{#1}}
\newcommand{\ChapRefUCUN}[1]{\ChapRefUN{#1}}
\newcommand{\SecRefUCUN}[1]{\SecRefUN{#1}}
\newcommand{\PartRefUCUN}[1]{\PartRefUN{#1}}
\newcommand{\BookRefLocalUN}[2]{\hyperref[#1]{\BookRefUN{#2}}}
\newcommand{\ChapRefLocalUN}[2]{\SecRefLocalUN{#1}{#2}}
\newcommand{\SecRefLocalUN}[2]{\hyperref[#1]{\SecRefUN{#2}}}
\newcommand{\PartRefLocalUN}[2]{\SecRefLocalUN{#1}{#2}}
\newcommand{\BookRefLocalUCUN}[2]{\BookRefLocalUN{#1}{#2}}
\newcommand{\ChapRefLocalUCUN}[2]{\ChapRefLocalUN{#1}{#2}}
\newcommand{\SecRefLocalUCUN}[2]{\SecRefLocalUN{#1}{#2}}
\newcommand{\PartRefLocalUCUN}[2]{\PartRefLocalUN{#1}{#2}}
\newcommand{\SectionNumberLink}[2]{\hyperref[#1]{#2}}
% Enabled with a 'enable-index-merge part style property. This default
% implementation isn't good enough, because the argument is a
% comma-separated sequence of labels:
\newcommand{\Smanypageref}[1]{\pageref{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fonts
% Font commands used by generated text:
\newcommand{\Scribtexttt}[1]{{\texttt{#1}}}
\newcommand{\textsub}[1]{$_{\hbox{\textsmaller{#1}}}$}
\newcommand{\textsuper}[1]{$^{\hbox{\textsmaller{#1}}}$}
\newcommand{\intextcolor}[2]{\textcolor{#1}{#2}}
\newcommand{\intextrgbcolor}[2]{\textcolor[rgb]{#1}{#2}}
\newcommand{\incolorbox}[2]{{\fboxrule=0pt\fboxsep=0pt\colorbox{#1}{#2}}}
\newcommand{\inrgbcolorbox}[2]{{\fboxrule=0pt\fboxsep=0pt\colorbox[rgb]{#1}{#2}}}
\newcommand{\plainlink}[1]{#1}
\newcommand{\techoutside}[1]{#1}
\newcommand{\techinside}[1]{#1}
\newcommand{\badlink}[1]{#1}
\newcommand{\indexlink}[1]{#1}
\newcommand{\noborder}[1]{#1}
\newcommand{\Smaller}[1]{\textsmaller{#1}}
\newcommand{\Larger}[1]{\textlarger{#1}}
\newcommand{\planetName}[1]{PLane\hspace{-0.1ex}T}
\newcommand{\slant}[1]{{\textsl{#1}}}
% Used for <, >, and | in tt mode. For some fonts and installations,
% there seems to be an encoding issue, so pick T1 explicitly:
\newcommand{\Stttextmore}{{\fontencoding{T1}\selectfont>}}
\newcommand{\Stttextless}{{\fontencoding{T1}\selectfont<}}
\newcommand{\Stttextbar}{{\fontencoding{T1}\selectfont|}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tables
% The `stabular' environment seems to be the lesser of evils among
% page-breaking table environments (and we've made a copy as ``pltstabular'
% to make sure that it doesn't change).
\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\message{pltstabular is a modification of stabular}
%% A renamed vsetion of:
%% stabular.sty
%% Copyright 1998 Sigitas Tolu\v sis
%% VTeX Ltd., Akademijos 4, Vilnius, Lithuania
%% e-mail [email protected]
%% http://www.vtex.lt/tex/download/macros/
%%
% This program can redistributed and/or modified under the terms
% of the LaTeX Project Public License Distributed from CTAN
% archives in directory macros/latex/base/lppl.txt; either
% version 1 of the License, or (at your option) any later version.
%
% PURPOSE: Improve tabular environment.
%
% SHORT DESCRIPTION:
%
% Changed internal commands: \@mkpream, \@addamp, \@xhline
%
% Provides new commands in tabular (used after command \\):
% \emptyrow[#1]
% -------------
% Adds empty row, #1 - height of the row
%
% \tabrow{#1}[#2]
% ---------------
% Adds row of natural height: #1\\[#2]
%
% Provides new environments: pltstabular and pltstabular*
% -------- ---------
% One more multi-page version of tabular
%
%
\def\empty@finalstrut#1{%
\unskip\ifhmode\nobreak\fi\vrule\@width\z@\@height\z@\@depth\z@}
\def\no@strut{\global\setbox\@arstrutbox\hbox{%
\vrule \@height\z@
\@depth\z@
\@width\z@}%
\gdef\@endpbox{\empty@finalstrut\@arstrutbox\par\egroup\hfil}%
}%
\def\yes@strut{\global\setbox\@arstrutbox\hbox{%
\vrule \@height\arraystretch \ht\strutbox
\@depth\arraystretch \dp\strutbox
\@width\z@}%
\gdef\@endpbox{\@finalstrut\@arstrutbox\par\egroup\hfil}%
}%
\def\@mkpream#1{\@firstamptrue\@lastchclass6
\let\@preamble\@empty\def\empty@preamble{\add@ins}%
\let\protect\@unexpandable@protect
\let\@sharp\relax\let\add@ins\relax
\let\@startpbox\relax\let\@endpbox\relax
\@expast{#1}%
\expandafter\@tfor \expandafter
\@nextchar \expandafter:\expandafter=\reserved@a\do
{\@testpach\@nextchar
\ifcase \@chclass \@classz \or \@classi \or \@classii \or \@classiii
\or \@classiv \or\@classv \fi\@lastchclass\@chclass}%
\ifcase \@lastchclass \@acol
\or \or \@preamerr \@ne\or \@preamerr \tw@\or \or \@acol \fi}
\def\@addamp{%
\if@firstamp
\@firstampfalse
\edef\empty@preamble{\add@ins}%
\else
\edef\@preamble{\@preamble &}%
\edef\empty@preamble{\expandafter\noexpand\empty@preamble &\add@ins}%
\fi}
\newif\iftw@hlines \tw@hlinesfalse
\def\@xhline{\ifx\reserved@a\hline
\tw@hlinestrue
\else\ifx\reserved@a\Hline
\tw@hlinestrue
\else
\tw@hlinesfalse
\fi\fi
\iftw@hlines
\aftergroup\do@after
\fi
\ifnum0=`{\fi}%
}
\def\do@after{\emptyrow[\the\doublerulesep]}
\def\emptyrow{\noalign\bgroup\@ifnextchar[\@emptyrow{\@emptyrow[\z@]}}
\def\@emptyrow[#1]{\no@strut\gdef\add@ins{\vrule \@height\z@ \@depth#1 \@width\z@}\egroup%
\empty@preamble\\
\noalign{\yes@strut\gdef\add@ins{\vrule \@height\z@ \@depth\z@ \@width\z@}}%
}
\def\tabrow#1{\noalign\bgroup\@ifnextchar[{\@tabrow{#1}}{\@tabrow{#1}[]}}
\def\@tabrow#1[#2]{\no@strut\egroup#1\ifx.#2.\\\else\\[#2]\fi\noalign{\yes@strut}}
%
\def\endpltstabular{\crcr\egroup\egroup \egroup}
\expandafter \let \csname endpltstabular*\endcsname = \endpltstabular
\def\pltstabular{\let\@halignto\@empty\@pltstabular}
\@namedef{pltstabular*}#1{\def\@halignto{to#1}\@pltstabular}
\def\@pltstabular{\leavevmode \bgroup \let\@acol\@tabacol
\let\@classz\@tabclassz
\let\@classiv\@tabclassiv \let\\\@tabularcr\@stabarray}
\def\@stabarray{\m@th\@ifnextchar[\@sarray{\@sarray[c]}}
\def\@sarray[#1]#2{%
\bgroup
\setbox\@arstrutbox\hbox{%
\vrule \@height\arraystretch\ht\strutbox
\@depth\arraystretch \dp\strutbox
\@width\z@}%
\@mkpream{#2}%
\edef\@preamble{%
\ialign \noexpand\@halignto
\bgroup \@arstrut \@preamble \tabskip\z@skip \cr}%
\let\@startpbox\@@startpbox \let\@endpbox\@@endpbox
\let\tabularnewline\\%
% \let\par\@empty
\let\@sharp##%
\set@typeset@protect
\lineskip\z@skip\baselineskip\z@skip
\@preamble}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatother
\newenvironment{bigtabular}{\begin{pltstabular}}{\end{pltstabular}}
% For the 'boxed table style:
\newcommand{\SBoxedLeft}{\textcolor[rgb]{0.6,0.6,1.0}{\vrule width 3pt\hspace{3pt}}}
% Formerly used to keep the horizontal line for a definition on the same page:
\newcommand{\SEndFirstHead}[0]{ \nopagebreak \\ }
% Corrects weirdness when a table is the first thing in
% an itemization:
\newcommand{\bigtableinlinecorrect}[0]{~
\vspace{-\baselineskip}\vspace{\parskip}}
% Used to indent the table correctly in an itemization, since that's
% one of the things stabular gets wrong:
\newlength{\stabLeft}
\newcommand{\bigtableleftpad}{\hspace{\stabLeft}}
\newcommand{\atItemizeStart}[0]{\addtolength{\stabLeft}{\labelsep}
\addtolength{\stabLeft}{\labelwidth}}
% For a single-column table in simple environments, it's better to
% use the `list' environment instead of `stabular'.
\newenvironment{SingleColumn}{\begin{list}{}{\topsep=0pt\partopsep=0pt%
\listparindent=0pt\itemindent=0pt\labelwidth=0pt\leftmargin=0pt\rightmargin=0pt%
\itemsep=0pt\parsep=0pt}\item}{\end{list}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Etc.
% ._ and .__
\newcommand{\Sendabbrev}[1]{#1\@}
\newcommand{\Sendsentence}[1]{\@#1}
% Default style for a nested flow:
\newenvironment{Subflow}{\begin{list}{}{\topsep=0pt\partopsep=0pt%
\listparindent=0pt\itemindent=0pt\labelwidth=0pt\leftmargin=0pt\rightmargin=0pt%
\itemsep=0pt}\item}{\end{list}}
% For the 'inset nested-flow style:
\newenvironment{SInsetFlow}{\begin{quote}}{\end{quote}}
% Indent a 'code-inset nested flow:
\newcommand{\SCodePreSkip}{\vskip\abovedisplayskip}
\newcommand{\SCodePostSkip}{\vskip\belowdisplayskip}
\newenvironment{SCodeFlow}{\SCodePreSkip\begin{list}{}{\topsep=0pt\partopsep=0pt%
\listparindent=0pt\itemindent=0pt\labelwidth=0pt\leftmargin=2ex\rightmargin=2ex%
\itemsep=0pt\parsep=0pt\onehalfspacing}\item}{\end{list}\SCodePostSkip}
\newcommand{\SCodeInsetBox}[1]{\setbox1=\hbox{\hbox{\hspace{2ex}#1\hspace{2ex}}}\vbox{\SCodePreSkip\vtop{\box1\SCodePostSkip}}}
% Inset a 'vertical-inset nested flow:
\newcommand{\SVInsetPreSkip}{\vskip\abovedisplayskip}
\newcommand{\SVInsetPostSkip}{\vskip\belowdisplayskip}
\newenvironment{SVInsetFlow}{\SVInsetPreSkip\begin{list}{}{\topsep=0pt\partopsep=0pt%
\listparindent=0pt\itemindent=0pt\labelwidth=0pt\leftmargin=0pt\rightmargin=0pt%
\itemsep=0pt\parsep=0pt}\item}{\end{list}\SVInsetPostSkip}
\newcommand{\SVInsetBox}[1]{\setbox1=\hbox{\hbox{#1}}\vbox{\SCodePreSkip\vtop{\box1\SCodePostSkip}}}
% The 'compact itemization style:
\newenvironment{compact}{\begin{itemize}}{\end{itemize}}
\newcommand{\compactItem}[1]{\item #1}
% The nested-flow style for `centerline':
\newenvironment{SCentered}{\begin{trivlist}\item \centering}{\end{trivlist}}
% The \refpara command corresponds to `margin-note'. The
% refcolumn and refcontent environments also wrap the note,
% because they simplify the CSS side.
\newcommand{\refpara}[1]{\normalmarginpar\marginpar{\raggedright \footnotesize #1}}
\newcommand{\refelem}[1]{\refpara{#1}}
\newenvironment{refcolumn}{}{}
\newenvironment{refcontent}{}{}
\newcommand{\refparaleft}[1]{\reversemarginpar\marginpar{\raggedright \footnotesize #1}}
\newcommand{\refelemleft}[1]{\refparaleft{#1}}
\newenvironment{refcolumnleft}{}{}
% Macros used by `title' and `author':
\newcommand{\titleAndVersionAndAuthors}[3]{\title{#1\\{\normalsize \SVersionBefore{}#2}}\author{#3}\maketitle}
\newcommand{\titleAndVersionAndEmptyAuthors}[3]{\title{#1\\{\normalsize \SVersionBefore{}#2}}#3\maketitle}
\newcommand{\titleAndEmptyVersionAndAuthors}[3]{\title{#1}\author{#3}\maketitle}
\newcommand{\titleAndEmptyVersionAndEmptyAuthors}[3]{\title{#1}\maketitle}
\newcommand{\titleAndVersionAndAuthorsAndShort}[4]{\title[#4]{#1\\{\normalsize \SVersionBefore{}#2}}\author{#3}\maketitle}
\newcommand{\titleAndVersionAndEmptyAuthorsAndShort}[4]{\title[#4]{#1\\{\normalsize \SVersionBefore{}#2}}#3\maketitle}
\newcommand{\titleAndEmptyVersionAndAuthorsAndShort}[4]{\title[#4]{#1}\author{#3}\maketitle}
\newcommand{\titleAndEmptyVersionAndEmptyAuthorsAndShort}[4]{\title[#4]{#1}\maketitle}
\newcommand{\SAuthor}[1]{#1}
\newcommand{\SAuthorSep}[1]{\qquad}
\newcommand{\SVersionBefore}[1]{Version }
% Useful for some styles, such as sigalternate:
\newcommand{\SNumberOfAuthors}[1]{}
\let\SOriginalthesubsection\thesubsection
\let\SOriginalthesubsubsection\thesubsubsection
% sections
\newcommand{\Spart}[2]{\part[#1]{#2}}
\newcommand{\Ssection}[2]{\section[#1]{#2}\let\thesubsection\SOriginalthesubsection}
\newcommand{\Ssubsection}[2]{\subsection[#1]{#2}\let\thesubsubsection\SOriginalthesubsubsection}
\newcommand{\Ssubsubsection}[2]{\subsubsection[#1]{#2}}
\newcommand{\Ssubsubsubsection}[2]{{\bf #2}}
\newcommand{\Ssubsubsubsubsection}[2]{\Ssubsubsubsection{#1}{#2}}
% "star" means unnumbered and not in ToC:
\newcommand{\Spartstar}[1]{\part*{#1}}
\newcommand{\Ssectionstar}[1]{\section*{#1}\renewcommand*\thesubsection{\arabic{subsection}}\setcounter{subsection}{0}}
\newcommand{\Ssubsectionstar}[1]{\subsection*{#1}\renewcommand*\thesubsubsection{\arabic{section}.\arabic{subsubsection}}\setcounter{subsubsection}{0}}
\newcommand{\Ssubsubsectionstar}[1]{\subsubsection*{#1}}
\newcommand{\Ssubsubsubsectionstar}[1]{{\bf #1}}
\newcommand{\Ssubsubsubsubsectionstar}[1]{\Ssubsubsubsectionstar{#1}}
% "starx" means unnumbered but in ToC:
\newcommand{\Spartstarx}[2]{\Spartstar{#2}\addcontentsline{toc}{part}{#1}}
\newcommand{\Ssectionstarx}[2]{\Ssectionstar{#2}\addcontentsline{toc}{section}{#1}}
\newcommand{\Ssubsectionstarx}[2]{\Ssubsectionstar{#2}\addcontentsline{toc}{subsection}{#1}}
\newcommand{\Ssubsubsectionstarx}[2]{\Ssubsubsectionstar{#2}\addcontentsline{toc}{subsubsection}{#1}}
\newcommand{\Ssubsubsubsectionstarx}[2]{\Ssubsubsubsectionstar{#2}}
\newcommand{\Ssubsubsubsubsectionstarx}[2]{\Ssubsubsubsubsectionstar{#2}}
% "grouper" is for the 'grouper style variant --- on subsections and lower,
% because \Spart is used for grouper at the section level. Grouper implies
% unnumbered.
\newcounter{GrouperTemp}
\newcommand{\Ssubsectiongrouper}[2]{\setcounter{GrouperTemp}{\value{subsection}}\Ssubsectionstarx{#1}{#2}\setcounter{subsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsectiongrouper}[2]{\setcounter{GrouperTemp}{\value{subsubsection}}\Ssubsubsectionstarx{#1}{#2}\setcounter{subsubsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsubsectiongrouper}[2]{\Ssubsubsubsectionstarx{#1}{#2}}
\newcommand{\Ssubsubsubsubsectiongrouper}[2]{\Ssubsubsubsubsectionstarx{#1}{#2}}
\newcommand{\Ssubsectiongrouperstar}[1]{\setcounter{GrouperTemp}{\value{subsection}}\Ssubsectionstar{#1}\setcounter{subsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsectiongrouperstar}[1]{\setcounter{GrouperTemp}{\value{subsubsection}}\Ssubsubsectionstar{#1}\setcounter{subsubsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsubsectiongrouperstar}[1]{\Ssubsubsubsectionstar{#1}}
\newcommand{\Ssubsubsubsubsectiongrouperstar}[1]{\Ssubsubsubsubsectionstar{#1}}
\newcommand{\Ssubsectiongrouperstarx}[2]{\setcounter{GrouperTemp}{\value{subsection}}\Ssubsectionstarx{#1}{#2}\setcounter{subsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsectiongrouperstarx}[2]{\setcounter{GrouperTemp}{\value{subsubsection}}\Ssubsubsectionstarx{#1}{#2}\setcounter{subsubsection}{\value{GrouperTemp}}}
\newcommand{\Ssubsubsubsectiongrouperstarx}[2]{\Ssubsubsubsectionstarx{#1}{#2}}
\newcommand{\Ssubsubsubsubsectiongrouperstarx}[2]{\Ssubsubsubsubsectionstarx{#1}{#2}}
% Generated by `subsubsub*section':
\newcommand{\SSubSubSubSection}[1]{\Ssubsubsubsubsectionstar{#1}}
% For hidden parts with an empty title:
\newcommand{\notitlesection}{\vspace{2ex}\phantomsection\noindent}
% To increments section numbers:
\newcommand{\Sincpart}{\stepcounter{part}}
\newcommand{\Sincsection}{\stepcounter{section}}
\newcommand{\Sincsubsection}{\stepcounter{subsection}}
\newcommand{\Sincsubsubsection}{\stepcounter{subsubsection}}
\newcommand{\Sincsubsubsubsection}{}
\newcommand{\Sincsubsubsubsubsection}{}
% When brackets appear in section titles:
\newcommand{\SOpenSq}{[}
\newcommand{\SCloseSq}{]}
% Helper for box-mode macros:
\newcommand{\Svcenter}[1]{$\vcenter{#1}$}
% Helper to work around a problem with "#"s for URLs within \href
% within other macros:
\newcommand{\Shref}[3]{\href{#1\##2}{#3}}
% For URLs:
\newcommand{\Snolinkurl}[1]{\nolinkurl{#1}}
% History note:
\newcommand{\SHistory}[1]{\begin{smaller}#1\end{smaller}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Scribble then generates the following:
%
% \begin{document}
% \preDoc
% \titleAndVersion{...}{...}
% ... document content ...
% \postDoc
% \end{document}
\newcommand{\NoteBox}[1]{\footnote{#1}}
\newcommand{\NoteContent}[1]{#1}
\newcommand{\Footnote}[1]{\footnote{#1}}
\newcommand{\FootnoteRef}[1]{}
\newcommand{\FootnoteTarget}[1]{}
\newcommand{\FootnoteContent}[1]{#1}
% Redefine \noindent to avoid generating any output at all:
\newenvironment{FootnoteBlock}{\renewcommand{\noindent}{}}{}
\newcommand{\FootnoteBlockContent}[1]{}
% Redefine \SColorize to produce B&W Scheme text
\newcommand{\SColorize}[2]{\color{#1}{#2}}
% Redefine SHyphen to allow identifiers to be hyphenated
\newcommand{\SHyphen}[1]{#1}
\newcommand{\inColor}[2]{{\SHyphen{\Scribtexttt{\SColorize{#1}{#2}}}}}
\definecolor{PaleBlue}{rgb}{0.90,0.90,1.0}
\definecolor{LightGray}{rgb}{0.90,0.90,0.90}
\definecolor{CommentColor}{rgb}{0.76,0.45,0.12}
\definecolor{ParenColor}{rgb}{0.52,0.24,0.14}
\definecolor{IdentifierColor}{rgb}{0.15,0.15,0.50}
\definecolor{ResultColor}{rgb}{0.0,0.0,0.69}
\definecolor{ValueColor}{rgb}{0.13,0.55,0.13}
\definecolor{OutputColor}{rgb}{0.59,0.00,0.59}
\newcommand{\RktPlain}[1]{\inColor{black}{#1}}
\newcommand{\RktKw}[1]{{\SColorize{black}{\Scribtexttt{#1}}}} % no \textbf anymore
\newcommand{\RktStxLink}[1]{\RktKw{#1}}
\newcommand{\RktStxDef}[1]{\RktStxLink{#1}}
\newcommand{\RktCmt}[1]{\inColor{CommentColor}{#1}}
\newcommand{\RktPn}[1]{\inColor{ParenColor}{#1}}
\newcommand{\RktInBG}[1]{\inColor{ParenColor}{#1}}
\newcommand{\RktSym}[1]{\inColor{IdentifierColor}{#1}}
\newcommand{\RktSymDef}[1]{\RktSym{#1}}
\newcommand{\RktVal}[1]{\inColor{ValueColor}{#1}}
\newcommand{\RktValLink}[1]{\inColor{blue}{#1}}
\newcommand{\RktValDef}[1]{\RktValLink{#1}}
\newcommand{\RktModLink}[1]{\inColor{blue}{#1}}
\newcommand{\RktRes}[1]{\inColor{ResultColor}{#1}}
\newcommand{\RktOut}[1]{\inColor{OutputColor}{#1}}
\newcommand{\RktMeta}[1]{\inColor{IdentifierColor}{#1}}
\newcommand{\RktMod}[1]{\inColor{black}{#1}}
\newcommand{\RktRdr}[1]{\inColor{black}{#1}}
\newcommand{\RktVarCol}[1]{\inColor{IdentifierColor}{#1}}
\newcommand{\RktVar}[1]{{\RktVarCol{\textsl{#1}}}}
\newcommand{\RktErrCol}[1]{\inColor{red}{#1}}
\newcommand{\RktErr}[1]{{\RktErrCol{\textrm{\textit{#1}}}}}
\newcommand{\RktOpt}[1]{#1}
\newcommand{\RktIn}[1]{\incolorbox{LightGray}{\RktInBG{#1}}}
\newcommand{\highlighted}[1]{\colorbox{PaleBlue}{\hspace{-0.5ex}\RktInBG{#1}\hspace{-0.5ex}}}
\newenvironment{RktBlk}{}{}
\newenvironment{defmodule}{}{}
\newenvironment{prototype}{}{}
\newenvironment{argcontract}{}{}
\newenvironment{together}{}{}
\newenvironment{specgrammar}{}{}
\newenvironment{RBibliography}{}{}
\newcommand{\bibentry}[1]{\parbox[t]{0.8\linewidth}{#1}}
\newenvironment{leftindent}{\begin{quote}}{\end{quote}}
\newenvironment{insetpara}{\begin{quote}}{\end{quote}}
\newcommand{\Rfilebox}[2]{\begin{list}{}{\topsep=0pt\partopsep=0pt%
\listparindent=0pt\itemindent=0pt\labelwidth=0pt\leftmargin=0ex\rightmargin=0ex%
\itemsep=0pt\parsep=0pt}\item #1
#2\end{list}}
\newcommand{\RfileboxBox}[3]{#3{\halign{\hfil##\cr #1 \cr #2 \cr}}}
\newcommand{\RfileboxBoxT}[2]{\RfileboxBox{#1}{#2}{\vtop}}
\newcommand{\RfileboxBoxC}[2]{\RfileboxBox{#1}{#2}{\Svcenter}}
\newcommand{\RfileboxBoxB}[2]{\RfileboxBox{#1}{#2}{\vbox}}
\newcommand{\Rfiletitle}[1]{\hfill \fbox{#1}}
\newcommand{\RfiletitleBox}[1]{\fbox{#1}}
\newcommand{\Rfilename}[1]{#1}
\newenvironment{Rfilecontent}{}{}
\newcommand{\RfilecontentBox}[1]{#1}
\newcommand{\RBackgroundLabel}[1]{}
\newenvironment{RBackgroundLabelInner}{}{}
\newcommand{\RpackageSpec}[1]{\hspace{5ex} #1}
\usepackage{ccaption}
% \legend relies on \belowcaptionskip, which is not defined
% by the JFP class file:
\makeatletter
\@ifundefined{belowcaptionskip}{\newlength{\belowcaptionskip}}{}
\makeatother
\newcommand{\Legend}[1]{~
\hrule width \hsize height .33pt
\vspace{4pt}
\legend{#1}}
\newcommand{\LegendContinued}[1]{\Legend{#1}}
\newcommand{\FigureTarget}[2]{#1}
\newcommand{\FigureRef}[2]{#1}
\newlength{\FigOrigskip}
\FigOrigskip=\parskip
% Put this before the figure content, so that a hyperref goes to
% the start of the content:
\newcommand{\FigureSetRef}{\refstepcounter{figure}}
\newenvironment{Figure}{\begin{figure}\FigureSetRef}{\end{figure}}
\newenvironment{FigureMulti}{\begin{figure*}[t!p]\FigureSetRef}{\end{figure*}}
\newenvironment{FigureMultiWide}{\begin{FigureMulti}\FigureSetRef}{\end{FigureMulti}}
\newenvironment{Herefigure}{\begin{figure}[ht!]\FigureSetRef\centering}{\end{figure}}
\newenvironment{Centerfigure}{\begin{Xfigure}\centering\item}{\end{Xfigure}}
\newenvironment{Leftfigure}{\begin{Xfigure}\item}{\end{Xfigure}}
\newenvironment{Rightfigure}{\begin{Xfigure}\item}{\end{Xfigure}}
\newenvironment{Xfigure}{\begin{list}{}{\leftmargin=0pt\topsep=0pt\parsep=\FigOrigskip\partopsep=0pt}}{\end{list}}
\newenvironment{FigureInside}{}{}
\newcommand{\Centertext}[1]{\begin{center}#1\end{center}}
\def\texMath#1{#1}
\def\texMathInline#1{\ensuremath{#1}}
\def\texMathDisplay#1{\ifmmode #1\else\[#1\]\fi}% This style file is used both for `book' and `report' modes
\renewcommand{\ChapRef}[2]{chapter~#1}
\renewcommand{\ChapRefUC}[2]{Chapter~#1}
% sections
\renewcommand{\Spart}[2]{\part[#1]{#2}}
\renewcommand{\Ssection}[2]{\chapter[#1]{#2}}
\renewcommand{\Ssubsection}[2]{\section[#1]{#2}}
\renewcommand{\Ssubsubsection}[2]{\subsection[#1]{#2}}
\renewcommand{\Ssubsubsubsection}[2]{\subsubsection[#1]{#2}}
\renewcommand{\Ssubsubsubsubsection}[2]{{\bf #2}}
% "star" means unnumbered and not in ToC:
\renewcommand{\Spartstar}[1]{\part*{#1}}
\renewcommand{\Ssectionstar}[1]{\chapter*{#1}}
\renewcommand{\Ssubsectionstar}[1]{\section*{#1}}
\renewcommand{\Ssubsubsectionstar}[1]{\subsection*{#1}}
\renewcommand{\Ssubsubsubsectionstar}[1]{\subsubsection*{#1}}
\renewcommand{\Ssubsubsubsubsectionstar}[1]{\Ssubsubsubsubsection{#1}}
% "starx" means unnumbered but in ToC:
\renewcommand{\Spartstarx}[2]{\Spartstar{#2}\addcontentsline{toc}{part}{#1}}
\renewcommand{\Ssectionstarx}[2]{\Ssectionstar{#2}\addcontentsline{toc}{chapter}{#1}}
\renewcommand{\Ssubsectionstarx}[2]{\Ssubsectionstar{#2}\addcontentsline{toc}{section}{#1}}
\renewcommand{\Ssubsubsectionstarx}[2]{\Ssubsubsectionstar{#2}\addcontentsline{toc}{subsection}{#1}}
\renewcommand{\Ssubsubsubsectionstarx}[2]{\Ssubsubsubsectionstar{#2}\addcontentsline{toc}{subsubsection}{#1}}
\renewcommand{\Ssubsubsubsubsectionstarx}[2]{\Ssubsubsubsubsectionstar{#2}}
% "grouper" is for the 'grouper style variant:
\renewcommand{\Ssubsectiongrouper}[2]{\setcounter{GrouperTemp}{\value{section}}\Ssubsectionstarx{#1}{#2}\setcounter{section}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsectiongrouper}[2]{\setcounter{GrouperTemp}{\value{subsection}}\Ssubsubsectionstarx{#1}{#2}\setcounter{subsection}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsubsectiongrouper}[2]{\setcounter{GrouperTemp}{\value{subsubsection}}\Ssubsubsectionstarx{#1}{#2}\setcounter{subsubsection}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsubsubsectiongrouper}[2]{\Ssubsubsubsubsectionstarx{#1}{#2}}
\renewcommand{\Ssubsectiongrouperstar}[1]{\setcounter{GrouperTemp}{\value{section}}\Ssubsectionstar{#1}\setcounter{section}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsectiongrouperstar}[1]{\setcounter{GrouperTemp}{\value{subsection}}\Ssubsubsectionstar{#1}\setcounter{subsection}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsubsectiongrouperstar}[1]{\setcounter{GrouperTemp}{\value{subsubsection}}\Ssubsubsectionstar{#1}\setcounter{subsubsection}{\value{GrouperTemp}}}
\renewcommand{\Ssubsubsubsubsectiongrouperstar}[1]{\Ssubsubsubsubsectionstar{#1}}
% To increments section numbers:
\renewcommand{\Sincpart}{\stepcounter{part}}
\renewcommand{\Sincsection}{\stepcounter{chapter}}
\renewcommand{\Sincsubsection}{\stepcounter{section}}
\renewcommand{\Sincsubsubsection}{\stepcounter{subsection}}
\renewcommand{\Sincsubsubsubsection}{\stepcounter{subsubsection}}
\geometry{top=1.0in,bottom=1.0in,left=1.0in,right=1.0in}
\linespread{1.6}
\rmfamily
\fontsize{12}{2}
\begin{document}
\preDoc
\titleAndEmptyVersionAndAuthors{Programming Language Implementation}{}{\SNumberOfAuthors{1}\SAuthor{Charles Saternos}}
\label{t:x28part_x22Programmingx5fLanguagex5fImplementationx22x29}
\newpage \tableofcontents \newpage
\sectionNewpage
\Ssection{Introduction}{Introduction}\label{t:x28part_x22Introductionx22x29}
From the outside, programming languages seem like mystical entities that
somehow translate arcane instructions to tangible outputs. The inner
workings of these magical beasts are best left to those who have years of
research under their belt and advanced knowledge of computers.
While programming language implementations do draw on many areas of
computer science knowledge, simple programming language can easily be
developed by any hacker with moderate interest and some free time. In
fact, a simple meta{-}circular interpreter developed in Racket
(the language used for implementations in this book), could be written in
one sitting.
This book is meant to be an introduction to some of the concepts and
techniques used to build programming languages. We{'}ll be looking at simple
implementations, with runnable example code throughout the book. Most code
examples can be run on their own, and the reader is encouraged to run
them, particularly when they are confused. Tinkering with example code is
one of the best ways to learn new programming techniques.
For larger code examples that require multiple chunks of code, the book is
written using literate programming. Literate programming provides
a textual version of the code, meant for reading, as well as the option to
stitch the code chunks together into a runnable source file. This allows
the reader to check out the exact code they read in the chapter, change
it, and run it for themselves.
\Ssubsection{Book source}{Book source}\label{t:x28part_x22Bookx5fsourcex22x29}
The source for this whole book is available on GitHub
\href{https://github.com/charles-l/capstone}{\Snolinkurl{https://github.com/charles-l/capstone}}.
The reader is encouraged to download the source and run it for themself.
\Ssubsection{Why bother with programming language implementation?}{Why bother with programming language implementation?}\label{t:x28part_x22Whyx5fbotherx5fwithx5fprogrammingx5flanguagex5fimplementationx5fx22x29}
There are many reasons for learning how programming languages work under
the hood, but some of the most useful are gaining a deeper understanding
of what programming languages do, how to implement your own domain
specific languages, and building your own general purpose programming
languages for fun.
Gaining a comprehensive knowledge of compilers and interpreters gives you
the ability to reason about error messages, and tune the performance of
programs written in your your favorite programming language. For instance,
knowledge of how the C compiler works, gives a programmer the ability to
determine the difference between compile time and link time errors. When
they get a linker error, they{'}ll realize they forgot to include a library
or object file in their compilation command.
Performance tuning is also easier once a programmer knows how a programing
language is optimizing. For instance, in Lua, strings are interned
{-} they{'}re inserted into a large hash{-}table, so string comparisons are
cheap. This means the programmer doesn{'}t have to intern the strings in
a hash table by hand. Or in Python, knowing that the bytecode compiler
generates an opcode to create a dictionary directly for an inline
declaration (i.e. \Scribtexttt{{\char`\{}{\char`\}}}), and the \Scribtexttt{dict()} function does a function
call generates the opcode, meaning an inline \Scribtexttt{{\char`\{}{\char`\}}} is faster than
calling \Scribtexttt{dict()}.
\sectionNewpage
\Ssection{Background}{Background}\label{t:x28part_x22Backgroundx22x29}
This chapter will introduce the basics of the Racket programming language,
and will discuss some theory that will be used later during
implementation.
\Ssubsection{Racket}{Racket}\label{t:x28part_x22Racketx22x29}
\includegraphics[scale=0.5]{rkttmp98239c067a6552b13196269e9adf471433a234a1.png}
We{'}ll use the Racket programming language for the implementations
throughout the book. Racket is a descendent of Lisp, the second high{-}level
programming language created (after Fortran). Despite its age, Lisp
dialects are still popular today.
Racket is designed to aid programming language developers in prototyping
and experimenting with new programming language paradigms. It is a test
bed, with many tools and libraries that make implementations simple and
concise. Instead of tracking memory usage in C, or writing dozens of
classes in a complicated hierarchy in Java, we will focus on developing
compilers and interpreters using domain{-}specific languages provided by
Racket.
Even without the libraries and extra resources, Racket is still an
excellent choice for developing programming languages. With built{-}in
functional pattern matching, idiomatic use of recursion, which is useful
for tree{-}navigation, and a simple, easy{-}to{-}implement kernel
(which makes building Lisp interpreters trivial), Racket is a great choice
for language development.
Racket is a simple language that you can learned quickly. You{'}re
encouraged to enter the simple programs throughout this book to see the
results yourself. Fidget with them in a REPL
(the interactive prompt for Racket programs). Tweak
them and observe what changes in the output. You{'}re encouraged to copy the
code throughout the book, but it{'}s especially vital to understand this
chapter since concepts we introduce here are referenced throughout the book.
Lisp{'}s\NoteBox{\NoteContent{We{'}ll talk generally about Lisp and Scheme since the features
are relevant to Racket}} syntax is extremely minimal. Expressions are
written in prefix notation, with parentheses to denote function calls.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{println}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{"Hello}\mbox{\hphantom{\Scribtexttt{x}}}\RktVal{world"}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktOut{"Hello world"
}\end{SingleColumn}\end{SCodeFlow}
Literal values (i.e. strings, numbers, or booleans) and variables are
written without parentheses.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktVal{"reeeee"}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{"reeeee"}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktVal{2}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{2}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{define}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{the{-}number{-}three}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{3}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktSym{the{-}number{-}three}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{3}\end{SingleColumn}\end{SCodeFlow}
The number of parentheses is significant in Lisp, unlike other languages
where they{'}re used for redefining order of operations. It{'}s nonsense to
write extra parentheses in many contexts.
For instance, the variable we defined in the previous example in another
pair of parentheses will attempt to execute the bound value (\Scribtexttt{3}) as
a function. This doesn{'}t make sense, so it{'}s important to match
parentheses carefully (a good editor is invaluable!).
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{the{-}number{-}three}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktErr{the{-}number{-}three: undefined;
cannot reference an identifier before its definition
in module: {\textquotesingle}anonymous{-}module
}\end{SingleColumn}\end{SCodeFlow}
The DrRacet IDE that ships with Racket [\hyperref[t:x28cite_x22DrSchemex22x29]{DrScheme}] is a common choice
for newcomers to the language. DrRacket features a REPL
(read{-}eval{-}print{-}loop {-} a prompt for interactive program development),
a graphical debugger, and macro expander. It highlights and indents Racket
code properly, and generally makes life easier for users who aren{'}t
familiar with Lisp syntax and notation.
Lisp doesn{'}t (by default) have infix notation. Standard numeric operations
are written in prefix form. For example, 3 * (4 + 2) is written:
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{*}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{3}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{+}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{4}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{2}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{18}\end{SingleColumn}\end{SCodeFlow}
This format seems awkward at first, but has the advantage of simple syntax
that is straightforward to parse which makes user defined macros possible
(which we{'}ll look at later).
Racket has standard data types (strings, numbers, and characters):
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktVal{"reeeee"}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{"reeeee"}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktVal{42}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{42}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktVal{\#{\char`\\}z}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{\#{\char`\\}z}\end{SingleColumn}\end{SCodeFlow}
And it has built{-}in data{-}types that are typically seen in Lisps. Namely
symbols, \Scribtexttt{cons} cells, lists, and vectors.
Symbols are similar to strings. The only distinction is the symbol{'}s value
is "interned" (i.e. inserted into an internal hash table), to make lookups
fast. This means symbols are cheap to compare, so they can be used in
contexts where enums would be used in other languages.
Symbols are created with the quote keyword:
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{quote}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{symbols{-}are{-}cooler{-}than{-}strings}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}symbols{-}are{-}cooler{-}than{-}strings}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{println}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{quote}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{just{-}a{-}symbol}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktOut{{\textquotesingle}just{-}a{-}symbol
}\end{SingleColumn}\end{SCodeFlow}
Since it{'}s awkward to keep write \Scribtexttt{quote} every time we create a symbol,
we can use the shorthand \Scribtexttt{}\Scribtexttt{{'}}\Scribtexttt{}.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{symbols{-}are{-}cooler{-}than{-}strings}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}symbols{-}are{-}cooler{-}than{-}strings}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{println}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{just{-}a{-}symbol}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktOut{{\textquotesingle}just{-}a{-}symbol
}\end{SingleColumn}\end{SCodeFlow}
Admittedly, the quote syntax is shocking when seen by programmers who use
languages that use single quotes to denote characters or strings. But
quote shorthand is terser than the \Scribtexttt{quote} keyword, so we will use it.
Lisp{'}s primary container structure is the cons cell. A cons cell is a pair
of two values. The first and second elements of the pair are accessed with
the \Scribtexttt{car} and \Scribtexttt{cdr} functions respectively.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{b}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a {\hbox{\texttt{.}}} b)}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{2}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{"cool"}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(2 {\hbox{\texttt{.}}} "cool")}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{\#{\char`\\}m}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{\#{\char`\\}e}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(\#{\char`\\}m {\hbox{\texttt{.}}} \#{\char`\\}e)}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{car}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{b}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}a}
\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cdr}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{my{-}other{-}car{-}is}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a{-}cdr}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}a{-}cdr}\end{SingleColumn}\end{SCodeFlow}
Cons cells can hold other cons cells in them, to become a linked list of
values.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{little}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{dotted}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{list}\RktPn{)}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a little dotted {\hbox{\texttt{.}}} list)}\end{SingleColumn}\end{SCodeFlow}
Rather than storing a value in the last cons cell, it{'}s standard to store
the null list, \Scribtexttt{}\Scribtexttt{{'}}\Scribtexttt{()}, which helps simplify list navigation.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{proper}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{cons}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{list}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktPn{(}\RktPn{)}\RktPn{)}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a proper list)}\end{SingleColumn}\end{SCodeFlow}
Since \Scribtexttt{cons}ing a bunch of \Scribtexttt{cons} cells together is tedious, the
helper function, \Scribtexttt{list} will create a linked{-}list of cons cells
(terminated by the empty list).
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{list}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{b}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{{\textquotesingle}}\RktSym{c}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a b c)}\end{SingleColumn}\end{SCodeFlow}
Furthermore, quoting each individual element can become an annoyance, so
we can use the quote keyword to build a list of symbols.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktPn{(}\RktSym{quote}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktPn{(}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{b}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{c}\RktPn{)}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a b c)}\end{SingleColumn}\end{SCodeFlow}
Or using the shorthand \Scribtexttt{}\Scribtexttt{{'}}\Scribtexttt{}:
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktSym{{\textquotesingle}}\RktPn{(}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{b}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{c}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}(a b c)}\end{SingleColumn}\end{SCodeFlow}
The quote symbol can construct lists, sublists, and literal values.
\begin{SCodeFlow}\begin{SingleColumn}\begin{bigtabular}{@{\bigtableleftpad}l@{}l@{}l@{}}
\hbox{\Scribtexttt{{\Stttextmore}}} &
\hbox{\mbox{\hphantom{\Scribtexttt{x}}}} &
\begin{RktBlk}\begin{tabular}[t]{@{}l@{}}
\hbox{\RktMeta{}\RktSym{{\textquotesingle}}\RktPn{(}\RktPn{(}\RktSym{a}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{b}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{c}\RktPn{)}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{d}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{e}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktSym{f}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{1}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{2}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{3}\RktMeta{}\mbox{\hphantom{\Scribtexttt{x}}}\RktMeta{}\RktVal{"strings}\mbox{\hphantom{\Scribtexttt{x}}}\RktVal{too{\hbox{\texttt{!}}}"}\RktPn{)}\RktMeta{}}\end{tabular}\end{RktBlk}\end{bigtabular}
\RktRes{{\textquotesingle}((a b c) d e f 1 2 3 "strings too{\hbox{\texttt{!}}}")}