Skip to content

Commit 3a32e7d

Browse files
authored
Merge pull request #26 from source-academy/revamp-chapter-1
chapter 1 in ok shape
2 parents 231d63b + 2ad0632 commit 3a32e7d

File tree

12 files changed

+1385
-1060
lines changed

12 files changed

+1385
-1060
lines changed

xml/chapter1/section1/subsection4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ square(square(3));
342342
<JAVASCRIPT>
343343
<TEXT>
344344
We can also use <JAVASCRIPTINLINE>square</JAVASCRIPTINLINE>
345-
as a building block in defining other
345+
as a building block in declaring other
346346
functions. For example, <LATEXINLINE>$x^2 +y^2$</LATEXINLINE>
347347
can be expressed as
348348
<SNIPPET EVAL="no">

xml/chapter1/section1/subsection6.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@
9393
</SCHEME>
9494
<JAVASCRIPT>
9595
<TEXT>
96-
The expressive power of the class of functions that we can define at
96+
The expressive power of the class of functions that we can declare at
9797
this point is very limited, because we have no way to make tests and
9898
to perform different operations depending on the result of a test.
99-
For instance, we cannot define a function that computes the
99+
For instance, we cannot declare a function that computes the
100100
<INDEX>absolute value</INDEX>
101101
absolute
102102
value of a number by testing whether the number is negative or not,
@@ -600,7 +600,7 @@ x &gt; 5 &amp;&amp; x &lt; 10
600600
<JAVASCRIPT>
601601
<TEXT>
602602
<CHANGE>
603-
As another example, we can define a predicate to test whether one
603+
As another example, we can declare a predicate to test whether one
604604
number is not equal to another number.
605605
<COMMENT>&gt;= already introduced above, so not_equal used here</COMMENT>
606606
</CHANGE>

xml/chapter1/section1/subsection7.xml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,25 @@ function sqrt(x) {
368368
</SPLIT>
369369

370370
<TEXT>
371-
If we type these definitions to the interpreter, we can use <SCHEMEINLINE>sqrt</SCHEMEINLINE>
371+
If we type these
372+
<SPLITINLINE>
373+
<SCHEME>
374+
definitions
375+
</SCHEME>
376+
<JAVASCRIPT>
377+
declarations
378+
</JAVASCRIPT>
379+
</SPLITINLINE>
380+
to the interpreter, we can use <SCHEMEINLINE>sqrt</SCHEMEINLINE>
372381
just as we can use any
373382
<SPLITINLINE>
374383
<SCHEME>
375-
procedure
384+
procedure:
376385
</SCHEME>
377386
<JAVASCRIPT>
378-
function
387+
function:
379388
</JAVASCRIPT>
380-
</SPLITINLINE>:
389+
</SPLITINLINE>
381390

382391
<SNIPPET PAGE="24">
383392
<NAME>sqrt_example</NAME>
@@ -560,8 +569,8 @@ square(sqrt(1000));
560569
ordinary conditional function whose application
561570
works just like conditional expressions?</QUOTE> she asks.
562571
Alyssa<APOS/>s friend Eva Lu Ator claims this can indeed be done, and
563-
she defines a <SCHEMEINLINE>conditional</SCHEMEINLINE> function
564-
as follows:
572+
she declares a <JAVASCRIPTINLINE>conditional</JAVASCRIPTINLINE>
573+
function as follows:
565574
<SNIPPET>
566575
<NAME>conditional</NAME>
567576
<JAVASCRIPT>

xml/chapter1/section1/subsection8.xml

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<SPLITINLINE><SCHEME><SCHEMEINLINE>Sqrt</SCHEMEINLINE></SCHEME><JAVASCRIPT>The function <JAVASCRIPTINLINE>sqrt</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> is our first example of a process defined by a set of
1717
mutually
1818
<SPLITINLINE><SCHEME>defined procedures</SCHEME>
19-
<JAVASCRIPT>declared functions</JAVASCRIPT></SPLITINLINE>.
19+
<JAVASCRIPT>defined functions</JAVASCRIPT></SPLITINLINE>.
2020
Notice that the
2121
<SPLITINLINE><SCHEME>definition of <SCHEMEINLINE>sqrt-iter</SCHEMEINLINE></SCHEME><JAVASCRIPT>declaration of <JAVASCRIPTINLINE>sqrt_iter</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> is
2222
<INDEX>recursive procedure<SUBINDEX>recursive procedure definition</SUBINDEX></INDEX>
@@ -385,13 +385,14 @@ function square(y) {
385385
</JAVASCRIPT>
386386
</SPLITINLINE>
387387
For example, we used
388-
<SCHEMEINLINE>square</SCHEMEINLINE> in
389-
the definition of
388+
<SCHEMEINLINE>square</SCHEMEINLINE>
390389
<SPLITINLINE>
391390
<SCHEME>
391+
in the definition of
392392
<SCHEMEINLINE>good-enough?</SCHEMEINLINE>
393393
</SCHEME>
394394
<JAVASCRIPT>
395+
in the declaration of
395396
<JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE>
396397
</JAVASCRIPT>
397398
</SPLITINLINE>
@@ -528,13 +529,13 @@ function good_enough(guess, x) {
528529
has a very special role in the
529530
<SPLITINLINE>
530531
<SCHEME>
531-
procedure
532+
procedure definition,
532533
</SCHEME>
533534
<JAVASCRIPT>
534-
function
535+
function declaration,
535536
</JAVASCRIPT>
536537
</SPLITINLINE>
537-
definition, in that it doesn<APOS/>t matter what name the
538+
in that it doesn<APOS/>t matter what name the
538539
<SPLITINLINE>
539540
<SCHEME>
540541
formal
@@ -549,14 +550,13 @@ function good_enough(guess, x) {
549550
<SPLITINLINE>
550551
<SCHEME>
551552
a <EM>bound variable</EM>, and we
552-
say that the procedure
553+
say that the procedure definition
553554
</SCHEME>
554555
<JAVASCRIPT>
555556
<EM>bound</EM>, and we
556-
say that the function
557+
say that the function declaration
557558
</JAVASCRIPT>
558559
</SPLITINLINE>
559-
definition
560560
<INDEX>bind</INDEX>
561561
<EM>binds</EM> its
562562
<SPLITINLINE>
@@ -573,18 +573,22 @@ function good_enough(guess, x) {
573573
procedure definition is unchanged if a bound variable
574574
</SCHEME>
575575
<JAVASCRIPT>
576-
function definition is unchanged if a bound name
576+
function declaration is unchanged if a bound name
577577
</JAVASCRIPT>
578578
</SPLITINLINE>
579-
is consistently renamed throughout the definition.<FOOTNOTE>The
579+
is consistently renamed throughout the
580+
<SPLITINLINE><SCHEME>definition</SCHEME><JAVASCRIPT>declaration</JAVASCRIPT></SPLITINLINE>.<FOOTNOTE>The
580581
concept of consistent renaming is actually subtle and difficult to
581582
define formally. Famous logicians have made embarrassing errors
582583
here.</FOOTNOTE>
583584
If a <SPLITINLINE><SCHEME>variable</SCHEME><JAVASCRIPT>name</JAVASCRIPT></SPLITINLINE>
584585
is not bound, we say that it is
585586
<INDEX>free variable</INDEX><INDEX>variable<SUBINDEX>free</SUBINDEX></INDEX>
586587
<EM>free</EM>. The
587-
set of expressions for which a binding defines a name is called the
588+
set of expressions for which a binding
589+
<SPLITINLINE><SCHEME>defines</SCHEME><JAVASCRIPT>declares</JAVASCRIPT></SPLITINLINE>
590+
a name
591+
is called the
588592
<INDEX>scope of a variable</INDEX><INDEX>variable<SUBINDEX>scope of</SUBINDEX></INDEX>
589593
<EM>scope</EM> of that name.
590594
In a
@@ -593,7 +597,7 @@ function good_enough(guess, x) {
593597
procedure definition, the bound variables
594598
</SCHEME>
595599
<JAVASCRIPT>
596-
function definition, the bound names
600+
function declaration, the bound names
597601
</JAVASCRIPT>
598602
</SPLITINLINE>
599603
declared as the
@@ -662,7 +666,7 @@ function good_enough(guess, x) {
662666
</SCHEME>
663667
<JAVASCRIPT>
664668
<TEXT>
665-
In the definition of <JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE> above,
669+
In the declaration of <JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE> above,
666670
<JAVASCRIPTINLINE>guess</JAVASCRIPTINLINE> and <JAVASCRIPTINLINE>x</JAVASCRIPTINLINE> are
667671
bound names but <JAVASCRIPTINLINE>abs</JAVASCRIPTINLINE>, and
668672
<JAVASCRIPTINLINE>square</JAVASCRIPTINLINE> are free.
@@ -683,19 +687,21 @@ function good_enough(guess, x) {
683687
meaning of <JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE> is
684688
not independent of the choice of its
685689
free names, however. It surely depends upon the fact (external to
686-
this definition) that the symbol <JAVASCRIPTINLINE>abs</JAVASCRIPTINLINE> names a
690+
this declaration) that the symbol <JAVASCRIPTINLINE>abs</JAVASCRIPTINLINE> names a
687691
function for computing the absolute value of a number.
688692
The JavaScript function <JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE> will
689693
compute a different mathematical function if we substitute
690694
<JAVASCRIPTINLINE>math_cos</JAVASCRIPTINLINE> (JavaScript<APOS/>s cosine function)
691-
for <JAVASCRIPTINLINE>abs</JAVASCRIPTINLINE> in its definition.
695+
for <JAVASCRIPTINLINE>abs</JAVASCRIPTINLINE> in its declaration.
692696
<INDEX>local name|)</INDEX>
693697
</TEXT>
694698
</JAVASCRIPT>
695699
</SPLIT>
696700

697701
<SUBHEADING>
698-
<NAME>Internal definitions and block structure</NAME>
702+
<NAME>Internal
703+
<SPLITINLINE><SCHEME>definitions</SCHEME><JAVASCRIPT>declarations</JAVASCRIPT></SPLITINLINE>
704+
and block structure</NAME>
699705
</SUBHEADING>
700706
<LABEL NAME="sec:block-structure"/>
701707

@@ -903,9 +909,14 @@ function sqrt(x) {
903909
This <EM>block structure</EM>
904910
is basically the right solution to the simplest
905911
name-packaging problem. But there is a better idea lurking here. In
906-
addition to internalizing the definitions of the auxiliary
907-
<SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>,
908-
we can simplify them. Since <SCHEMEINLINE>x</SCHEMEINLINE> is bound in the definition of
912+
addition to internalizing the
913+
<SPLITINLINE><SCHEME>definitions of the auxiliary procedures</SCHEME>
914+
<JAVASCRIPT>declarations of the auxiliary functions</JAVASCRIPT></SPLITINLINE>,
915+
we can simplify them. Since <SCHEMEINLINE>x</SCHEMEINLINE>
916+
is bound in the
917+
<SPLITINLINE><SCHEME>definition</SCHEME>
918+
<JAVASCRIPT>declaration</JAVASCRIPT></SPLITINLINE>
919+
of
909920
<SCHEMEINLINE>sqrt</SCHEMEINLINE>, the
910921
<SPLITINLINE>
911922
<SCHEME>
@@ -917,8 +928,12 @@ function sqrt(x) {
917928
</SPLITINLINE>
918929
<SPLITINLINE><SCHEME><SCHEMEINLINE>good-enough?</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>good_enough</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>,
919930
<SCHEMEINLINE>improve</SCHEMEINLINE>, and
920-
<SPLITINLINE><SCHEME><SCHEMEINLINE>sqrt-iter</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>sqrt_iter</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>,
921-
which are defined internally to
931+
<SPLITINLINE>
932+
<SCHEME><SCHEMEINLINE>sqrt-iter</SCHEMEINLINE>,
933+
which are defined internally to</SCHEME>
934+
<JAVASCRIPT><JAVASCRIPTINLINE>sqrt_iter</JAVASCRIPTINLINE>,
935+
which are defined internally to</JAVASCRIPT>
936+
</SPLITINLINE>
922937
<SCHEMEINLINE>sqrt</SCHEMEINLINE>, are in the
923938
scope of <SCHEMEINLINE>x</SCHEMEINLINE>.
924939
Thus, it is not necessary to pass
@@ -930,7 +945,10 @@ function sqrt(x) {
930945
<INDEX>free variable<SUBINDEX>internal@in internal definition</SUBINDEX></INDEX>
931946
free
932947
<SPLITINLINE><SCHEME>variable</SCHEME><JAVASCRIPT>name</JAVASCRIPT></SPLITINLINE>
933-
in the internal definitions, as shown below.
948+
in the internal
949+
<SPLITINLINE><SCHEME>definitions,</SCHEME>
950+
<JAVASCRIPT>declarations,</JAVASCRIPT></SPLITINLINE>
951+
as shown below.
934952
Then <SCHEMEINLINE>x</SCHEMEINLINE>
935953
gets its value from the argument with which the enclosing
936954
<SPLITINLINE>
@@ -960,25 +978,24 @@ function sqrt(x) {
960978
bindings made by enclosing
961979
<SPLITINLINE>
962980
<SCHEME>
963-
procedure
981+
procedure definitions;
964982
</SCHEME>
965983
<JAVASCRIPT>
966-
function
984+
function declarations;
967985
</JAVASCRIPT>
968986
</SPLITINLINE>
969-
definitions;
970987
that is, they are looked up in
971988
<INDEX>environment<SUBINDEX>lexical scoping and</SUBINDEX></INDEX>
972989
the environment in which the
973990
<SPLITINLINE>
974991
<SCHEME>
975-
procedure
992+
procedure was defined.
976993
</SCHEME>
977994
<JAVASCRIPT>
978-
function
995+
function was declared.
979996
</JAVASCRIPT>
980997
</SPLITINLINE>
981-
was defined. We will see how
998+
We will see how
982999
this works in detail in chapter<SPACE/>3 when we
9831000
study environments and the
9841001
detailed behavior of the interpreter.</FOOTNOTE>
@@ -1022,19 +1039,23 @@ function sqrt(x) {
10221039

10231040
<TEXT>
10241041
We will use block structure extensively to help us break
1025-
up large programs into tractable pieces.<FOOTNOTE>Embedded definitions
1026-
<INDEX>internal definition<SUBINDEX>position of</SUBINDEX></INDEX>
1027-
must come first in a
1042+
up large programs into tractable pieces.<FOOTNOTE>Embedded
10281043
<SPLITINLINE>
10291044
<SCHEME>
1030-
procedure
1045+
definitions must come first in a procedure
10311046
</SCHEME>
10321047
<JAVASCRIPT>
1033-
function
1048+
declarations must come first in a function
10341049
</JAVASCRIPT>
10351050
</SPLITINLINE>
1036-
body. The management is not responsible
1037-
for the consequences of running programs that intertwine definition
1051+
body.
1052+
<INDEX>internal definition<SUBINDEX>position of</SUBINDEX></INDEX>
1053+
The management is not responsible
1054+
for the consequences of running programs that intertwine
1055+
<SPLITINLINE>
1056+
<SCHEME>definition</SCHEME>
1057+
<JAVASCRIPT>declaration</JAVASCRIPT>
1058+
</SPLITINLINE>
10381059
and use.<LABEL NAME="foot:management"/></FOOTNOTE>
10391060
The idea of block structure originated with the
10401061
programming language

xml/chapter1/section2/subsection1.xml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ application<SUBINDEX>shape of process|(</SUBINDEX></INDEX>
116116
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
117117
for computing
118118
factorials:<FOOTNOTE>In a real program we would probably use the
119-
block structure introduced in the last section to hide the definition
120-
of
121-
<SPLITINLINE><SCHEME><SCHEMEINLINE>fact-iter</SCHEMEINLINE></SCHEME>
122-
<JAVASCRIPT><JAVASCRIPTINLINE>fact_iter</JAVASCRIPTINLINE></JAVASCRIPT>
123-
</SPLITINLINE>:
119+
block structure introduced in the last section to hide the
120+
<SPLITINLINE>
121+
<SCHEME>definition of <SCHEMEINLINE>fact-iter</SCHEMEINLINE></SCHEME><JAVASCRIPT>declaration of <JAVASCRIPTINLINE>fact_iter</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>:
124122

125123
<SNIPPET PAGE="33">
126124
<EXAMPLE>factorial_example</EXAMPLE>
@@ -345,7 +343,9 @@ function fact_iter(product, counter, max_count) {
345343
recursive
346344
<EM><SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE></EM>.
347345
When we describe a <SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE> as recursive,
348-
we are referring to the syntactic fact that the <SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE> definition
346+
we are referring to the syntactic fact that the
347+
<SPLITINLINE><SCHEME>procedure definition</SCHEME>
348+
<JAVASCRIPT>function declaration</JAVASCRIPT></SPLITINLINE>
349349
refers (either directly or indirectly) to the <SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE> itself. But
350350
when we describe a process as following a pattern that is, say,
351351
linearly recursive, we are speaking about how the process evolves, not
@@ -593,8 +593,11 @@ A(3, 3);
593593
</JAVASCRIPT>
594594
</SNIPPET>
595595

596-
Consider the following <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>, where <SCHEMEINLINE>A</SCHEMEINLINE> is the <SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
597-
defined above:
596+
Consider the following <SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>, where <SCHEMEINLINE>A</SCHEMEINLINE> is the
597+
<SPLITINLINE><SCHEME>procedure defined</SCHEME>
598+
<JAVASCRIPT>function declared</JAVASCRIPT>
599+
</SPLITINLINE>
600+
above:
598601

599602
<SNIPPET PAGE="36">
600603
<NAME>fghk_definition</NAME>

xml/chapter1/section2/subsection4.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ function fast_expt(b, n) {
148148
? square(fast_expt(b, n / 2))
149149
: b * fast_expt(b, n - 1);
150150
}
151-
152151
</JAVASCRIPT>
153152
</SNIPPET>
154153

xml/chapter1/section2/subsection6.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ expmod(4, 3, 5);
269269
<SCHEMEINLINE>random</SCHEMEINLINE>, which we assume
270270
<SPLITINLINE>
271271
<SCHEME>is included as a primitive in Scheme.</SCHEME>
272-
<JAVASCRIPT>our JavaScript environment defines as a primitive function.c</JAVASCRIPT>
272+
<JAVASCRIPT>our JavaScript environment defines as a primitive function.</JAVASCRIPT>
273273
</SPLITINLINE>
274274
<SPLITINLINE>
275275
<SCHEME>
@@ -514,7 +514,8 @@ smallest_divisor(199);
514514
procedure,
515515
</SCHEME>
516516
<JAVASCRIPT>
517-
Assume that our JavaScript environment defines a primitive function called
517+
Assume that our JavaScript environment declares a
518+
primitive function called
518519
<SCHEMEINLINE>runtime</SCHEMEINLINE>
519520
that returns an integer that specifies the amount of time the system
520521
has been running (measured in microseconds). The
@@ -654,8 +655,9 @@ timed_prime_test(43);
654655
should not be 2, 3, 4, 5, 6, <ELLIPSIS/>
655656
but rather 2, 3, 5, 7, 9, <ELLIPSIS/>.
656657
To implement this
657-
change, define a
658-
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
658+
change,
659+
<SPLITINLINE>define a <SCHEME>procedure</SCHEME>
660+
<JAVASCRIPT>declare a function</JAVASCRIPT></SPLITINLINE>
659661
<SCHEMEINLINE>next</SCHEMEINLINE> that returns 3 if its input is
660662
equal to 2 and otherwise returns its input plus<SPACE/>2.
661663
Modify the

0 commit comments

Comments
 (0)