Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions xml/chapter3/section1/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,19 @@ a(10); // output: 15
</SCHEMEOUTPUT>
</SNIPPET>

<SNIPPET HIDE="yes">
<NAME>make_accumulator_example1_solution</NAME>
<SCHEME>
(A 10) ;; output: 15
</SCHEME>
<JAVASCRIPT>
a(10); // output: 15
</JAVASCRIPT>
<SCHEMEOUTPUT>
15
</SCHEMEOUTPUT>
</SNIPPET>

<SNIPPET EVAL="yes">
<REQUIRES>make_accumulator_example1</REQUIRES>
<NAME>make_accumulator_example2</NAME>
Expand All @@ -829,6 +842,36 @@ a(10); // output: 25
25
</SCHEMEOUTPUT>
</SNIPPET>

<SNIPPET HIDE="yes">
<NAME>make_accumulator_example2_solution</NAME>
<SCHEME>
(A 10) ;; output: 25
</SCHEME>
<JAVASCRIPT>
a(10); // output: 25
</JAVASCRIPT>
<SCHEMEOUTPUT>
25
</SCHEMEOUTPUT>
</SNIPPET>

<SOLUTION>
<SNIPPET>
<EXAMPLE>make_accumulator_example</EXAMPLE>
<EXAMPLE>make_accumulator_example1_solution</EXAMPLE>
<EXAMPLE>make_accumulator_example2_solution</EXAMPLE>
<JAVASCRIPT>
function make_accumulator(current) {
function add(arg) {
current = current + arg;
return current;
}
return add;
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE><LABEL NAME="ex:make-monitored"/>
Expand Down
53 changes: 35 additions & 18 deletions xml/chapter3/section2/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const square = x =&gt; x * x;
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>. A new binding, which associates the
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
object with the symbol <SCHEMEINLINE>square</SCHEMEINLINE>, has been added to the global
frame. In general, <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>var</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> creates definitions by adding
frame. In general, <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE> creates</SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>const</JAVASCRIPTINLINE> and <JAVASCRIPTINLINE>let</JAVASCRIPTINLINE> create</JAVASCRIPT></SPLITINLINE> definitions by adding
bindings to frames.

<FIGURE>
Expand Down Expand Up @@ -239,24 +239,41 @@ const square = x =&gt; x * x;

<TEXT>
<!--\indsf{define}[environment model of]-->
We also specify that defining a symbol using <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT>var</JAVASCRIPT></SPLITINLINE> creates a
binding in the current environment frame and assigns to the symbol the
indicated value.<FOOTNOTE>If there is already a binding for the
variable in the current frame, then the binding is changed. This is
convenient because it allows redefinition of symbols; however, it also
means that <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT>var</JAVASCRIPT></SPLITINLINE> can be used to change values, and this brings
up the issues of assignment without explicitly using
<!--\indsf{set!}[environment model of]-->
<SPLITINLINE><SCHEME><SCHEMEINLINE>set!</SCHEMEINLINE></SCHEME><JAVASCRIPT>assignment</JAVASCRIPT></SPLITINLINE>.
Because of this, some people prefer redefinitions of existing symbols
to signal errors or warnings.</FOOTNOTE> Finally, we specify the behavior of
We also specify that defining a symbol using <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT>const</JAVASCRIPT>/<JAVASCRIPT>let</JAVASCRIPT></SPLITINLINE> creates a
constant/variable binding in the current environment frame and assigns to the symbol the
indicated value. Finally, we specify the behavior of
<SPLITINLINE><SCHEME><SCHEMEINLINE>set!</SCHEMEINLINE></SCHEME><JAVASCRIPT>assignment</JAVASCRIPT></SPLITINLINE>, the operation that forced us to introduce the environment
model in the first place. Evaluating the <SPLITINLINE><SCHEME>expression <SCHEMEINLINE>(set! </SCHEMEINLINE><LATEXINLINE>variable value</LATEXINLINE><SCHEMEINLINE>)</SCHEMEINLINE></SCHEME><JAVASCRIPT>statement <LATEXINLINE>variable</LATEXINLINE><JAVASCRIPTINLINE>=</JAVASCRIPTINLINE><LATEXINLINE>value</LATEXINLINE><JAVASCRIPTINLINE>;</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> in some environment locates the binding of
the variable in the environment and changes that binding to indicate
the new value. That is, one finds the first frame in the environment
that contains a binding for the variable and modifies that frame. If
the variable is unbound in the environment, then <SPLITINLINE><SCHEME><SCHEMEINLINE>set!</SCHEMEINLINE></SCHEME><JAVASCRIPT>assignment</JAVASCRIPT></SPLITINLINE> signals
an error.
model in the first place.
<SPLIT>
<SCHEME>
Evaluating the expression
<SCHEMEINLINE>(set! </SCHEMEINLINE><LATEXINLINE>variable value</LATEXINLINE><SCHEMEINLINE>)</SCHEMEINLINE>
in some environment locates the binding of
the variable in the environment.
For this, one finds the first frame in the environment
that contains a binding for the variable
and modifies that frame. If the variable
is unbound in the environment, then
<SCHEMEINLINE>set!</SCHEMEINLINE> signals an error.
</SCHEME>
<JAVASCRIPT>
Evaluating the
<JAVASCRIPT>statement <LATEXINLINE>name</LATEXINLINE><JAVASCRIPTINLINE>=</JAVASCRIPTINLINE><LATEXINLINE>value</LATEXINLINE><JAVASCRIPTINLINE>;</JAVASCRIPTINLINE></JAVASCRIPT> in some environment locates the binding of
the name in the environment.
For this, one finds the first frame in the environment
that contains a binding for the name.
If the name
is unbound in the environment, then
the assignment signals a <QUOTE>variable undefined</QUOTE> error.
Otherwise, if the binding is a constant binding, the assignment
signals an <QUOTE>assignment to constant</QUOTE>
error, because JavaScript forbids assignment
to constants. At last, if
the binding is a variable binding, that binding
is changed to indicate the new value of the variable.
</JAVASCRIPT>
</SPLIT>

</TEXT>

<TEXT>
Expand Down
7 changes: 1 addition & 6 deletions xml/chapter3/section2/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const w1 = make_withdraw(100);
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
object is the value
returned by the call to <SPLITINLINE><SCHEME><SCHEMEINLINE>make-withdraw</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>make_withdraw</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>. This is bound to
<SPLITINLINE><SCHEME><SCHEMEINLINE>W1</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>w1</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> in the global environment, since the <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>var</JAVASCRIPTINLINE> definition</JAVASCRIPT></SPLITINLINE> itself is being
<SPLITINLINE><SCHEME><SCHEMEINLINE>W1</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>w1</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> in the global environment, since the <SPLITINLINE><SCHEME><SCHEMEINLINE>define</SCHEMEINLINE></SCHEME><JAVASCRIPT>constant declaration</JAVASCRIPT></SPLITINLINE> itself is being
evaluated in the global environment. Figure<SPACE/><REF NAME="fig:w1"/> shows the
resulting environment structure.

Expand Down Expand Up @@ -331,11 +331,6 @@ is simply
</SCHEME>
</SNIPPET>
</SCHEME>
<JAVASCRIPT>
Recall Section<SPACE/><REF NAME="sec:introduce-let"/> that
<JAVASCRIPTINLINE></JAVASCRIPTINLINE> definitions can be seen as an alternate syntax
for function definition and application.
</JAVASCRIPT>
</SPLIT>
Use the environment model to analyze this alternate
version of <SCHEMEINLINE>make_withdraw</SCHEMEINLINE>, drawing figures like the ones above to
Expand Down