-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse03.tm
3053 lines (2413 loc) · 122 KB
/
course03.tm
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
<TeXmacs|2.1.2>
<style|<tuple|generic|no-page-numbers|british|preview-ref|smart-ref>>
<\body>
<chapter|Building Abstractions with Procedures>
We are about to study the idea of a <em|computational
process><index|computational process>. Computational processes are abstract
beings that inhabit computers. As they evolve, processes manipulate other
abstract things called <em|data><index|data>. The evolution of a process is
directed by a pattern of rules called a <em|program><index|program>. People
create programs to direct processes. In effect, we conjure the spirits of
the computer with our spells.
A computational process is indeed much like a sorcerer's idea of a spirit.
It cannot be seen or touched. It is not composed of matter at all. However,
it is very real. It can perform intellectual work. It can answer questions.
It can affect the world by disbursing money at a bank or by controlling a
robot arm in a factory. The programs we use to conjure processes are like a
sorcerer's spells. They are carefully composed from symbolic expressions in
arcane and esoteric <em|programming languages><index|programming languages>
that prescribe the tasks we want our processes to perform.
A computational process, in a correctly working computer, executes programs
precisely and accurately. Thus, like the sorcerer's apprentice, novice
programmers must learn to understand and to anticipate the consequences of
their conjuring. Even small errors (usually called <em|bugs><index|bugs> or
<em|glitches><index|glitches>) in programs can have complex and
unanticipated consequences.
Fortunately, learning to program is considerably less dangerous than
learning sorcery, because the spirits we deal with are conveniently
contained in a secure way. Real-world programming, however, requires care,
expertise, and wisdom. A small bug in a computer-aided design program, for
example, can lead to the catastrophic collapse of an airplane or a dam or
the self-destruction of an industrial robot.
Master software engineers have the ability to organize programs so that
they can be reasonably sure that the resulting processes will perform the
tasks intended. They can visualize the behavior of their systems in
advance. They know how to structure programs so that unanticipated problems
do not lead to catastrophic consequences, and when problems do arise, they
can <em|debug><index|debug> their programs. Well-designed computational
systems, like well-designed automobiles or nuclear reactors, are designed
in a modular manner, so that the parts can be constructed, replaced, and
debugged separately.
<paragraph|Programming in Lisp>
We need an appropriate language for describing processes, and we will use
for this purpose the programming language Lisp. Just as our everyday
thoughts are usually expressed in our natural language (such as English,
French, or Japanese), and descriptions of quantitative phenomena are
expressed with mathematical notations, our procedural thoughts will be
expressed in Lisp. Lisp was invented in the late 1950s as a formalism for
reasoning about the use of certain kinds of logical expressions, called
<em|recursion equations><index|recusion equations>, as a model for
computation. The language was conceived by John McCarthy and is based on
his paper ``Recursive Functions of Symbolic Expressions and Their
Computation by Machine'' (McCarthy 1960).
Despite its inception as a mathematical formalism, Lisp is a practical
programming language. A Lisp <em|interpreter><index|interpreter> is a
machine that carries out processes described in the Lisp language. The
first Lisp interpreter was implemented by McCarthy with the help of
colleagues and students in the Artificial Intelligence Group of the MIT
Research Laboratory of Electronics and in the MIT Computation
Center.<\footnote>
The <em|Lisp 1 Programmer's Manual> appeared in 1960, and the <em|Lisp
1.5 Programmer's Manual> (<cite|mccarthy1965lisp>) was published in 1962.
The early history of Lisp is described in McCarthy 1978.
</footnote> Lisp, whose name is an acronym for LISt Processing, was
designed to provide symbol-manipulating capabilities for attacking
programming problems such as the symbolic differentiation and integration
of algebraic expressions. It included for this purpose new data objects
known as atoms and lists, which most strikingly set it apart from all other
languages of the period.
Lisp was not the product of a concerted design effort. Instead, it evolved
informally in an experimental manner in response to users' needs and to
pragmatic implementation considerations. Lisp's informal evolution has
continued through the years, and the community of Lisp users has
traditionally resisted attempts to promulgate any ``official'' definition
of the language. This evolution, together with the flexibility and elegance
of the initial conception, has enabled Lisp, which is the second oldest
language in widespread use today (only Fortran is older), to continually
adapt to encompass the most modern ideas about program design. Thus, Lisp
is by now a family of dialects, which, while sharing most of the original
features, may differ from one another in significant ways. The dialect of
Lisp used in this book is called Scheme.<\footnote>
The two dialects in which most major Lisp programs of the 1970s were
written are <label|%_idx_48>MacLisp <label|%_idx_50>(Moon 1978;
<label|%_idx_52>Pitman 1983), developed at the MIT Project MAC, and
<label|%_idx_56><label|%_idx_58>Interlisp <label|%_idx_60>(Teitelman
1974), developed at <label|%_idx_62>Bolt Beranek and Newman Inc. and the
Xerox Palo Alto Research Center. <label|%_idx_66><label|%_idx_68>Portable
Standard Lisp <label|%_idx_70>(Hearn 1969; <label|%_idx_72>Griss 1981)
was a Lisp dialect designed to be easily portable between different
machines. MacLisp spawned a number of subdialects, such as
<label|%_idx_74><label|%_idx_76>Franz Lisp, which was developed at the
<label|%_idx_78>University of California at Berkeley, and
<label|%_idx_80><label|%_idx_82>Zetalisp (Moon 1981), which was based on
a special-purpose processor designed at the <label|%_idx_84>MIT
Artificial Intelligence Laboratory to run Lisp very efficiently. The Lisp
dialect used in this book, called <label|%_idx_86>Scheme (Steele 1975),
was invented in 1975 by <label|%_idx_88><label|%_idx_90>Guy Lewis Steele
Jr. and Gerald Jay Sussman of the MIT Artificial Intelligence Laboratory
and later reimplemented for instructional use at MIT. Scheme became an
IEEE standard in 1990 (IEEE 1990). The
<label|%_idx_92><label|%_idx_94>Common Lisp dialect (Steele 1982, Steele
1990) was developed by the Lisp community to combine features from the
earlier Lisp dialects to make an industrial standard for Lisp. Common
Lisp became an ANSI standard in 1994 (ANSI<nbsp>1994).
</footnote>
Because of its experimental character and its emphasis on symbol
manipulation, Lisp was at first very inefficient for numerical
computations, at least in comparison with Fortran. Over the years, however,
Lisp compilers have been developed that translate programs into machine
code that can perform numerical computations reasonably efficiently. And
for special applications, Lisp has been used with great
effectiveness.<\footnote>
One such special application was a breakthrough computation of scientific
importance -- an integration of the motion of the
<label|%_idx_102><label|%_idx_104>Solar System that extended previous
results by nearly two orders of magnitude, and demonstrated that the
dynamics of the Solar System is chaotic. This computation was made
possible by new integration algorithms, a special-purpose compiler, and a
special-purpose computer all implemented with the aid of software tools
written in Lisp (Abelson et al. 1992; Sussman and Wisdom 1992).
</footnote> Although Lisp has not yet overcome its old reputation as
hopelessly inefficient, Lisp is now used in many applications where
efficiency is not the central concern. For example, Lisp has become a
language of choice for operating-system shell languages and for extension
languages for editors and computer-aided design systems.
If Lisp is not a mainstream language, why are we using it as the framework
for our discussion of programming? Because the language possesses unique
features that make it an excellent medium for studying important
programming constructs and data structures and for relating them to the
linguistic features that support them. The most significant of these
features is the fact that Lisp descriptions of processes, called
<em|procedures><index|procedures>, can themselves be represented and
manipulated as Lisp data. The importance of this is that there are powerful
program-design techniques that rely on the ability to blur the traditional
distinction between ``passive'' data and ``active'' processes. As we shall
discover, Lisp's flexibility in handling procedures as data makes it one of
the most convenient languages in existence for exploring these techniques.
The ability to represent procedures as data also makes Lisp an excellent
language for writing programs that must manipulate other programs as data,
such as the interpreters and compilers that support computer languages.
Above and beyond these considerations, programming in Lisp is great fun.
<section|The Elements of Programming><label|1.1>
A powerful programming language is more than just a means for instructing a
computer to perform tasks. The language also serves as a framework within
which we organize our ideas about processes. Thus, when we describe a
language, we should pay particular attention to the means that the language
provides for combining simple ideas to form more complex ideas. Every
powerful language has three mechanisms for accomplishing this:
<\itemize>
<item><with|font-series|bold|primitive expressions>, which represent the
simplest entities the language is concerned with,
<item><with|font-series|bold|means of combination>, by which compound
elements are built from simpler ones, and
<item><with|font-series|bold|means of abstraction>, by which compound
elements can be named and manipulated as units.
</itemize>
In programming, we deal with two kinds of elements: procedures and data.
(Later we will discover that they are really not so distinct.) Informally,
data is \Pstuff\Q that we want to manipulate, and procedures are
descriptions of the rules for manipulating the data. Thus, any powerful
programming language should be able to describe primitive data and
primitive procedures and should have methods for combining and abstracting
procedures and data.
In this chapter we will deal only with simple numerical data so that we can
focus on the rules for building procedures.<\footnote>
The characterization of numbers as \Psimple data\Q is a barefaced bluff.
In fact, the treatment of numbers is one of the trickiest and most
confusing aspects of any programming language. Some typical issues
involved are these: Some computer systems distinguish
<em|integers><index|integers>, such as 2, from <em|real
numbers><index|real numbers>, such as 2.71. Is the real number 2.00
different from the integer 2? Are the arithmetic operations used for
integers the same as the operations used for real numbers? Does 6 divided
by 2 produce 3, or 3.0? How large a number can we represent? How many
decimal places of accuracy can we represent? Is the range of integers the
same as the range of real numbers? Above and beyond these questions, of
course, lies a collection of issues concerning roundoff and truncation
errors\Vthe entire science of numerical analysis. Since our focus in this
book is on large-scale program design rather than on numerical
techniques, we are going to ignore these problems. The numerical examples
in this chapter will exhibit the usual roundoff behavior that one
observes when using arithmetic operations that preserve a limited number
of decimal places of accuracy in noninteger operations.
</footnote> In later chapters we will see that these same rules allow us to
build procedures to manipulate compound data as well.
<subsection|Expressions>
One easy way to get started at programming is to examine some typical
interactions with an interpreter for the Scheme dialect of Lisp. Imagine
that you are sitting at a computer terminal. You type an
<em|expression><glossary-explain|expression|\<#8868\>\<#8FBE\>\<#5F0F\>\V\V\<#539F\>\<#8BED\>\<#6216\>\<#8005\>\<#7EC4\>\<#5408\>\<#5F0F\>>,
and the interpreter responds by displaying the result of its
<em|evaluating><index|evaluating> that expression.
One kind of primitive expression you might type is a number. (More
precisely, the expression that you type consists of the numerals that
represent the number in base 10.) If you present Lisp with a number
<code|486>
the interpreter will respond by printing<\footnote>
Throughout this book, when we wish to emphasize the distinction between
the input typed by the user and the response printed by the interpreter,
we will show the latter in slanted characters.
</footnote>
<code|<with|font-shape|italic|486>>
Expressions representing numbers may be combined with an expression
representing a primitive procedure (such as <scm|+> or <scm|*>) to form a
compound expression that represents the application of the procedure to
those numbers. For example:
<\session|scheme|default>
<\unfolded-io|Scheme] >
(+ 137 349)
<|unfolded-io>
486
</unfolded-io>
<\unfolded-io|Scheme] >
(- 1000 334)
<|unfolded-io>
666
</unfolded-io>
<\unfolded-io|Scheme] >
(- 1000 334)
<|unfolded-io>
666
</unfolded-io>
<\unfolded-io|Scheme] >
(* 5 99)
<|unfolded-io>
495
</unfolded-io>
<\unfolded-io|Scheme] >
(/ 10 5)
<|unfolded-io>
2
</unfolded-io>
<\unfolded-io|Scheme] >
(+ 2.7 10)
<|unfolded-io>
12.7
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
Expressions such as these, formed by delimiting a list of expressions
within parentheses in order to denote procedure application, are called
<em|combinations><glossary-explain|combinations|\<#7EC4\>\<#5408\>\<#5F0F\>>.
The leftmost element in the list is called the
<em|operator><glossary-explain|operator|\<#64CD\>\<#4F5C\>\<#7B26\>>, and
the other elements are called <em|operands><glossary-explain|operands|\<#64CD\>\<#4F5C\>\<#6570\>>.
The value of a combination is obtained by applying the procedure specified
by the operator to the <em|arguments><glossary-explain|arguments|\<#53C2\>\<#6570\>>
that are the values of the operands.
The convention of placing the operator to the left of the operands is known
as <em|prefix notation><glossary-explain|prefix
notation|\<#524D\>\<#7F00\>\<#8868\>\<#793A\>\<#6CD5\>>, and it may be
somewhat confusing at first because it departs significantly from the
customary mathematical convention. Prefix notation has several advantages,
however. One of them is that it can accommodate procedures that may take an
arbitrary number of arguments, as in the following examples:
<\session|scheme|default>
<\unfolded-io|Scheme] >
(+ 21 35 12 7)
<|unfolded-io>
75
</unfolded-io>
<\unfolded-io|Scheme] >
(* 25 4 12)
<|unfolded-io>
1200
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
No ambiguity can arise, because the operator is always the leftmost element
and the entire combination is delimited by the parentheses.
A second advantage of prefix notation is that it extends in a
straightforward way to allow combinations to be
<with|font-shape|italic|nested>, that is, to have combinations whose
elements are themselves combinations:
<\session|scheme|default>
<\unfolded-io|Scheme] >
(+ (* 3 5) (- 10 6))
<|unfolded-io>
19
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
There is no limit (in principle) to the depth of such nesting and to the
overall complexity of the expressions that the Lisp interpreter can
evaluate. It is we humans who get confused by still relatively simple
expressions such as
<\scm-code>
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
</scm-code>
which the interpreter would readily evaluate to be 57. We can help
ourselves by writing such an expression in the form
<\scm-code>
(+ (* 3
\ \ \ \ \ \ (+ (* 2 4)
\ \ \ \ \ \ \ \ \ (+ 3 5)))
\ \ \ (+ (- 10 7)
\ \ \ \ \ \ 6))
</scm-code>
following a formatting convention known as
<em|pretty-printing><index|pretty-printing>, in which each long combination
is written so that the operands are aligned vertically. The resulting
indentations display clearly the structure of the expression.<\footnote>
Lisp systems typically provide features to aid the user in formatting
expressions. Two especially useful features are one that automatically
indents to the proper pretty-print position whenever a new line is
started and one that highlights the matching left parenthesis whenever a
right parenthesis is typed.
</footnote>
Even with complex expressions, the interpreter always operates in the same
basic cycle: It reads an expression from the terminal, evaluates the
expression, and prints the result. This mode of operation is often
expressed by saying that the interpreter runs in a <em|read-eval-print
loop><index|read-eval-print loop>. Observe in particular that it is not
necessary to explicitly instruct the interpreter to print the value of the
expression.<\footnote>
Lisp obeys the convention that every expression has a value. This
convention, together with the old reputation of Lisp as an inefficient
language, is the source of the quip by <name|Alan Perlis><index|Alan
Perlis> (paraphrasing Oscar Wilde) that \PLisp programmers know the value
of everything but the cost of nothing.\Q
</footnote>
<subsection|Naming and the Environment>
A critical aspect of a programming language is the means it provides for
using names to refer to computational objects. We say that the name
identifies a <em|variable><glossary-explain|variable|\<#53D8\>\<#91CF\>>
whose <em|value><glossary-explain|value|\<#503C\>> is the object.
In the Scheme dialect of Lisp, we name things with <scm|define>. Typing
<\session|scheme|default>
<\input|Scheme] >
(define size 2)
</input>
<\input|Scheme] >
\;
</input>
</session>
causes the interpreter to associate the value 2 with the name
<scm|size>.<\footnote>
In this book, we do not show the interpreter's response to evaluating
definitions, since this is highly implementation-dependent.
</footnote> Once the name <scm|size> has been associated with the number 2,
we can refer to the value 2 by name:
<\session|scheme|default>
<\unfolded-io|Scheme] >
size
<|unfolded-io>
2
</unfolded-io>
<\unfolded-io|Scheme] >
(* 5 size)
<|unfolded-io>
10
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
Here are further examples of the use of <code*|define>:
<\session|scheme|default>
<\input|Scheme] >
(define pi 3.14159)
</input>
<\input|Scheme] >
(define radius 10)
</input>
<\unfolded-io|Scheme] >
(* pi (* radius radius))
<|unfolded-io>
314.159
</unfolded-io>
<\input|Scheme] >
(define circumference (* 2 pi radius))
</input>
<\unfolded-io|Scheme] >
circumference
<|unfolded-io>
62.8318
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
<scm|Define> is our language's simplest means of abstraction, for it allows
us to use simple names to refer to the results of compound operations, such
as the <scm|circumference> computed above. In general, computational
objects may have very complex structures, and it would be extremely
inconvenient to have to remember and repeat their details each time we want
to use them. Indeed, complex programs are constructed by building, step by
step, computational objects of increasing complexity. The interpreter makes
this step-by-step program construction particularly convenient because
name-object associations can be created incrementally in successive
interactions. This feature encourages the incremental development and
testing of programs and is largely responsible for the fact that a Lisp
program usually consists of a large number of relatively simple procedures.
It should be clear that the possibility of associating values with symbols
and later retrieving them means that the interpreter must maintain some
sort of memory that keeps track of the name-object pairs. This memory is
called the <em|environment><index|environment> (more precisely the
<em|global environment><index|global environment>, since we will see later
that a computation may involve a number of different
environments).<\footnote>
Chapter 3 will show that this notion of environment is crucial, both for
understanding how the interpreter works and for implementing
interpreters.
</footnote>
<subsection|Evaluating Combinations><label|sec:1.1.3>
One of our goals in this chapter is to isolate issues about thinking
procedurally. As a case in point, let us consider that, in evaluating
combinations, the interpreter is itself following a procedure.
<\itemize-dot>
<item>To evaluate a combination, do the following:
<\enumerate>
<item>Evaluate the subexpressions of the combination.
<item>Apply the procedure that is the value of the leftmost
subexpression (the operator) to the arguments that are the values of
the other subexpressions (the operands).
</enumerate>
</itemize-dot>
Even this simple rule illustrates some important points about processes in
general. First, observe that the first step dictates that in order to
accomplish the evaluation process for a combination we must first perform
the evaluation process on each element of the combination. Thus, the
evaluation rule is <em|recursive><index|recursive> in nature; that is, it
includes, as one of its steps, the need to invoke the rule
itself.<\footnote>
It may seem strange that the evaluation rule says, as part of the first
step, that we should evaluate the leftmost element of a combination,
since at this point that can only be an operator such as <code*|+> or
<code*|*> representing a built-in primitive procedure such as addition or
multiplication. We will see later that it is useful to be able to work
with combinations whose operators are themselves compound expressions.
</footnote>
Notice how succinctly the idea of recursion can be used to express what, in
the case of a deeply nested combination, would otherwise be viewed as a
rather complicated process. For example, evaluating
<\scm-code>
(* (+ 2 (* 4 6))
\ \ \ (+ 3 5 7))
</scm-code>
requires that the evaluation rule be applied to four different
combinations. We can obtain a picture of this process by representing the
combination in the form of a tree, as shown in <smart-ref|fig:1.1>. Each
combination is represented by a node with branches corresponding to the
operator and the operands of the combination stemming from it. The terminal
nodes (that is, nodes with no branches stemming from them) represent either
operators or numbers. Viewing evaluation in terms of the tree, we can
imagine that the values of the operands percolate upward, starting from the
terminal nodes and then combining at higher and higher levels. In general,
we shall see that recursion is a very powerful technique for dealing with
hierarchical, treelike objects. In fact, the \Ppercolate values upward\Q
form of the evaluation rule is an example of a general kind of process
known as <em|tree accumulation><index|tree accumulation>.
<\big-figure|<tree|390|*|<tree|26|+|2|<tree|24|*|4|6>>|<tree|15|+|3|5|7>>>
<label|fig:1.1>Tree representation, showing the value of each
subcombination.
</big-figure>
Next, observe that the repeated application of the first step brings us to
the point where we need to evaluate, not combinations, but primitive
expressions such as numerals, built-in operators, or other names. We take
care of the primitive cases by stipulating that
<\itemize>
<item>the values of numerals are the numbers that they name,
<item>the values of built-in operators are the machine instruction
sequences that carry out the corresponding operations, and
<item>the values of other names are the objects associated with those
names in the environment.
</itemize>
We may regard the second rule as a special case of the third one by
stipulating that symbols such as <code*|+> and <code*|*> are also included
in the global environment, and are associated with the sequences of machine
instructions that are their \Pvalues.\Q The key point to notice is the role
of the environment in determining the meaning of the symbols in
expressions. In an interactive language such as Lisp, it is meaningless to
speak of the value of an expression such as <scm|(+ x 1)> without
specifying any information about the environment that would provide a
meaning for the symbol <code*|x> (or even for the symbol <code*|+>). As we
shall see in Chapter 3, the general notion of the environment as providing
a context in which evaluation takes place will play an important role in
our understanding of program execution.
Notice that the evaluation rule given above does not handle definitions.
For instance, evaluating <scm|(define x 3)> does not apply <code*|define>
to two arguments, one of which is the value of the symbol <code*|x> and the
other of which is 3, since the purpose of the <code*|define> is precisely
to associate <code*|x> with a value. (That is, <scm|(define x 3)> is not a
combination.)
Such exceptions to the general evaluation rule are called <em|special
forms><index|special forms>. <scm|Define> is the only example of a special
form that we have seen so far, but we will meet others shortly. Each
special form has its own evaluation rule. The various kinds of expressions
(each with its associated evaluation rule) constitute the syntax of the
programming language. In comparison with most other programming languages,
Lisp has a very simple syntax; that is, the evaluation rule for expressions
can be described by a simple general rule together with specialized rules
for a small number of special forms.<\footnote>
Special syntactic forms that are simply convenient alternative surface
structures for things that can be written in more uniform ways are
sometimes called <em|syntactic sugar><glossary-explain|syntactic
sugar|\<#8BED\>\<#6CD5\>\<#7CD6\>>, to use a phrase coined by Peter
Landin. In comparison with users of other languages, Lisp programmers, as
a rule, are less concerned with matters of syntax. (By contrast, examine
any Pascal manual and notice how much of it is devoted to descriptions of
syntax.) This disdain for syntax is due partly to the flexibility of
Lisp, which makes it easy to change surface syntax, and partly to the
observation that many \Pconvenient\Q syntactic constructs, which make the
language less uniform, end up causing more trouble than they are worth
when programs become large and complex. In the words of <name|Alan
Perlis>, \PSyntactic sugar causes cancer of the semicolon.\Q
</footnote>
<subsection|Compound Procedures><label|sec:1.1.4>
We have identified in Lisp some of the elements that must appear in any
powerful programming language:
<\itemize>
<item>Numbers and arithmetic operations are primitive data and
procedures.
<item>Nesting of combinations provides a means of combining operations.
<item>Definitions that associate names with values provide a limited
means of abstraction.
</itemize>
Now we will learn about <em|procedure definitions><index|procedure
definitions>, a much more powerful abstraction technique by which a
compound operation can be given a name and then referred to as a unit.
We begin by examining how to express the idea of \Psquaring.\Q We might
say, \PTo square something, multiply it by itself.\Q This is expressed in
our language as
<\session|scheme|default>
<\input|Scheme] >
<label|define_square>(define (square x) (* x x))
</input>
<\input|Scheme] >
\;
</input>
</session>
We can understand this in the following way:
<\code>
(define (square x) \ \ \ (* \ \ \ \ \ \ x \ \ \ \ \ \ x))
\ \ \| \ \ \ \ \ \| \ \ \ \ \ \| \ \ \ \ \ \| \ \ \ \ \ \ \|
\ \ \ \ \ \ \|
\ To square something, multiply it by itself.
</code>
We have here a <em|compound procedure><glossary-explain|compound
procedure|\<#590D\>\<#5408\>\<#51FD\>\<#6570\>>, which has been given the
name <code*|square>. The procedure represents the operation of multiplying
something by itself. The thing to be multiplied is given a local name,
<code*|x>, which plays the same role that a pronoun plays in natural
language. Evaluating the definition creates this compound procedure and
associates it with the name <code*|square>.<\footnote>
Observe that there are two different operations being combined here: we
are creating the procedure, and we are giving it the name <code*|square>.
It is possible, indeed important, to be able to separate these two
notions\Vto create procedures without naming them, and to give names to
procedures that have already been created. We will see how to do this in
Section 1.3.2.
</footnote>
The general form of a procedure definition is
<\scm-code>
(define (\<langle\><var|name>\<rangle\> \<langle\><var|formal
parameters>\<rangle\>) \<langle\><var|body>\<rangle\>)
</scm-code>
The \<langle\><var|name>\<rangle\> is a symbol to be associated with the
procedure definition in the environment.<\footnote>
Throughout this book, we will describe the general syntax of expressions
by using italic symbols delimited by angle brackets\Ve.g.,
\<langle\><var|name>\<rangle\>\Vto denote the \Pslots\Q in the expression
to be filled in when such an expression is actually used.
</footnote> The \<langle\><var|formal parameters>\<rangle\> are the names
used within the body of the procedure to refer to the corresponding
arguments of the procedure. The \<langle\><var|body>\<rangle\> is an
expression that will yield the value of the procedure application when the
formal parameters are replaced by the actual arguments to which the
procedure is applied.<\footnote>
More generally, the body of the procedure can be a sequence of
expressions. In this case, the interpreter evaluates each expression in
the sequence in turn and returns the value of the final expression as the
value of the procedure application.
</footnote> The \<langle\><var|name>\<rangle\> and the
\<langle\><var|formal parameters>\<rangle\> are grouped within parentheses,
just as they would be in an actual call to the procedure being defined.
Having defined <code*|square>, we can now use it:
<\session|scheme|default>
<\unfolded-io|Scheme] >
(square 21)
<|unfolded-io>
441
</unfolded-io>
<\unfolded-io|Scheme] >
(square (+ 2 5))
<|unfolded-io>
49
</unfolded-io>
<\unfolded-io|Scheme] >
(square (square 3))
<|unfolded-io>
81
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
We can also use <code*|square> as a building block in defining other
procedures. For example, <math|x<rsup|2>+y<rsup|2>> can be expressed as
<\scm-code>
(+ (square x) (square y))
</scm-code>
We can easily define a procedure <code*|sum-of-squares> that, given any two
numbers as arguments, produces the sum of their squares:
<\session|scheme|default>
<\input|Scheme] >
(define (sum-of-squares x y)
\ \ (+ (square x) (square y)))
</input>
<\unfolded-io|Scheme] >
(sum-of-squares 3 4)
<|unfolded-io>
25
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
Now we can use <code*|sum-of-squares> as a building block in constructing
further procedures:
<\session|scheme|default>
<\input|Scheme] >
(define (f a)
\ \ (sum-of-squares (+ a 1) (* a 2)))
</input>
<\unfolded-io|Scheme] >
(f 5)
<|unfolded-io>
136
</unfolded-io>
<\input|Scheme] >
\;
</input>
</session>
Compound procedures are used in exactly the same way as primitive
procedures. Indeed, one could not tell by looking at the definition of
<code*|sum-of-squares> given above whether <code*|square> was built into
the interpreter, like <code*|+> and <code*|*>, or defined as a compound
procedure.
<subsection|The Substitution Model for Procedure
Application><label|sec:1.1.5>
To evaluate a combination whose operator names a compound procedure, the
interpreter follows much the same process as for combinations whose
operators name primitive procedures, which we described in
<smart-ref|sec:1.1.3>. That is, the interpreter evaluates the elements of
the combination and applies the procedure (which is the value of the
operator of the combination) to the arguments (which are the values of the
operands of the combination).
We can assume that the mechanism for applying primitive procedures to
arguments is built into the interpreter. For compound procedures, the
application process is as follows:
<\itemize>
<item>To apply a compound procedure to arguments, evaluate the body of
the procedure with each formal parameter replaced by the corresponding
argument.
</itemize>
To illustrate this process, let's evaluate the combination
<\scm-code>
(f 5)
</scm-code>
where <code*|f> is the procedure defined in <smart-ref|sec:1.1.4>. We begin
by retrieving the body of <code*|f>:
<\scm-code>
(sum-of-squares (+ a 1) (* a 2))
</scm-code>
Then we replace the formal parameter <code*|a> by the argument 5:
<\scm-code>
(sum-of-squares (+ 5 1) (* 5 2))
</scm-code>
Thus the problem reduces to the evaluation of a combination with two
operands and an operator <code*|sum-of-squares>. Evaluating this
combination involves three subproblems. We must evaluate the operator to
get the procedure to be applied, and we must evaluate the operands to get
the arguments. Now <scm|(+ 5 1)> produces 6 and <scm|(* 5 2)> produces 10,
so we must apply the <code*|sum-of-squares> procedure to 6 and 10. These
values are substituted for the formal parameters <code*|x> and <code*|y> in
the body of <scm|sum-of-squares>, reducing the expression to
<\scm-code>
(+ (square 6) (square 10))
</scm-code>
If we use the definition of <code*|square>, this reduces to
<\scm-code>
(+ (* 6 6) (* 10 10))
</scm-code>
which reduces by multiplication to
<\scm-code>
(+ 36 100)
</scm-code>
and finally to
<\scm-code>
136
</scm-code>
The process we have just described is called the <em|substitution
model><glossary-explain|substitution model|\<#4EE3\>\<#6362\>\<#6A21\>\<#578B\>>
for procedure application. It can be taken as a model that determines the
\Pmeaning\Q of procedure application, insofar as the procedures in this
chapter are concerned. However, there are two points that should be
stressed:
<\itemize>
<item>The purpose of the substitution is to help us think about procedure
application, not to provide a description of how the interpreter really
works. Typical interpreters do not evaluate procedure applications by
manipulating the text of a procedure to substitute values for the formal
parameters. In practice, the \Psubstitution\Q is accomplished by using a
local environment for the formal parameters. We will discuss this more
fully in Chapter 3 and Chapter 4 when we examine the implementation of an
interpreter in detail.
<item>Over the course of this book, we will present a sequence of
increasingly elaborate models of how interpreters work, culminating with
a complete implementation of an interpreter and compiler in Chapter 5.
The substitution model is only the first of these models\Va way to get
started thinking formally about the evaluation process. In general, when
modeling phenomena in science and engineering, we begin with simplified,
incomplete models. As we examine things in greater detail, these simple
models become inadequate and must be replaced by more refined models. The
substitution model is no exception. In particular, when we address in
Chapter 3 the use of procedures with \Pmutable data,\Q we will see that
the substitution model breaks down and must be replaced by a more
complicated model of procedure application.<\footnote>
Despite the simplicity of the substitution idea, it turns out to be
surprisingly complicated to give a rigorous mathematical definition of
the substitution process. The problem arises from the possibility of
confusion between the names used for the formal parameters of a
procedure and the (possibly identical) names used in the expressions to
which the procedure may be applied. Indeed, there is a long history of
erroneous definitions of <em|substitution><index|substitution> in the
literature of logic and programming semantics. See Stoy 1977 for a
careful discussion of substitution.
</footnote>
</itemize>
<paragraph*|Applicative order versus normal order>
According to the description of evaluation given in <smart-ref|sec:1.1.3>,
the interpreter first evaluates the operator and operands and then applies
the resulting procedure to the resulting arguments. This is not the only
way to perform evaluation. An alternative evaluation model would not
evaluate the operands until their values were needed. Instead it would
first substitute operand expressions for parameters until it obtained an
expression involving only primitive operators, and would then perform the
evaluation. If we used this method, the evaluation of <scm|(f 5)> would
proceed according to the sequence of expansions
<\scm-code>
(sum-of-squares (+ 5 1) (* 5 2))
\;
(+ (square (+ 5 1)) \ \ \ (square (* 5 2)))
\;
(+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2)))
</scm-code>
followed by the reductions
<\scm-code>
(+ (* 6 6) \ \ \ \ \ \ \ \ \ \ \ \ \ (* 10 10))
\;
(+ 36 \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 100)
136
</scm-code>
This gives the same answer as our previous evaluation model, but the
process is different. In particular, the evaluations of <scm|(+ 5 1)> and
<scm|(* 5 2)> are each performed twice here, corresponding to the reduction
of the expression <code*|(* x x)> with <code*|x> replaced respectively by
<code*|(+ 5 1)> and <code*|(* 5 2)>.
This alternative \Pfully expand and then reduce\Q evaluation method is
known as <em|normal-order evaluation><glossary-explain|normal-order
evaluation|\<#6B63\>\<#5219\>\<#5E8F\>\<#6C42\>\<#503C\>>, in contrast to
the \Pevaluate the arguments and then apply\Q method that the interpreter
actually uses, which is called <em|applicative-order
evaluation><glossary-explain|applicative-order
evaluation|\<#5E94\>\<#7528\>\<#5E8F\>\<#6C42\>\<#503C\>>. It can be shown
that, for procedure applications that can be modeled using substitution
(including all the procedures in the first two chapters of this book) and
that yield legitimate values, normal-order and applicative-order evaluation
produce the same value. (See Exercise <reference|ex1.5> for an instance of
an \Pillegitimate\Q value where normal-order and applicative-order
evaluation do not give the same result.)
Lisp uses applicative-order evaluation, partly because of the additional
efficiency obtained from avoiding multiple evaluations of expressions such
as those illustrated with <code*|(+ 5 1)> and <code*|(* 5 2)> above and,
more significantly, because normal-order evaluation becomes much more
complicated to deal with when we leave the realm of procedures that can be
modeled by substitution. On the other hand, normal-order evaluation can be
an extremely valuable tool, and we will investigate some of its
implications in Chapter 3 and Chapter 4.<\footnote>
In Chapter 3 we will introduce <em|stream processing><index|stream
processing>, which is a way of handling apparently \Pinfinite\Q data
structures by incorporating a limited form of normal-order evaluation. In
4.2 we will modify the Scheme interpreter to produce a normal-order
variant of Scheme.
</footnote>
<subsection|Conditional Expressions and Predicates>
The expressive power of the class of procedures that we can define at this
point is very limited, because we have no way to make tests and to perform
different operations depending on the result of a test. For instance, we
cannot define a procedure that computes the absolute value of a number by
testing whether the number is positive, negative, or zero and taking
different actions in the different cases according to the rule
<\equation*>
<around|\||x|\|>=<around*|{|<tabular|<tformat|<cwith|1|-1|1|1|cell-halign|r>|<cwith|1|-1|2|2|cell-halign|l>|<cwith|1|-1|3|3|cell-halign|l>|<table|<row|<cell|x>|<cell|<with|mode|text|if>>|<cell|x\<gtr\>0,>>|<row|<cell|0>|<cell|<with|mode|text|if>>|<cell|x=0,>>|<row|<cell|-x>|<cell|<with|mode|text|if>>|<cell|x\<less\>0.>>>>>|\<nobracket\>>
</equation*>
\
This construct is called a <em|case analysis><index|case analysis>, and
there is a special form in Lisp for notating such a case analysis. It is
called <code*|cond> (which stands for \Pconditional\Q), and it is used as
follows:
<\scm-code>
(define (abs x)
\ \ (cond ((\<gtr\> x 0) x)
\ \ \ \ \ \ \ \ ((= x 0) 0)
\ \ \ \ \ \ \ \ ((\<less\> x 0) (- x))))
</scm-code>
The general form of a conditional expression is
\;
<\scm-code>
(cond (\<langle\><var|p<rsub|1>>\<rangle\>
\<langle\><var|e<rsub|1>>\<rangle\>)
\ \ \ \ \ \ (\<langle\><var|p<rsub|2>>\<rangle\>
\<langle\><var|e<rsub|2>>\<rangle\>)
\ \ \ \ \ \ ...
\ \ \ \ \ \ (\<langle\><var|p<rsub| >>\<rangle\> \<langle\><var|e<rsub|
>>\<rangle\>))
</scm-code>
consisting of the symbol <code*|cond> followed by parenthesized pairs of