Skip to content

Commit

Permalink
Merge branch 'master' into ex-2-87
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-henz authored Jul 15, 2024
2 parents a47899f + 4d8a92f commit b969e50
Show file tree
Hide file tree
Showing 26 changed files with 1,145 additions and 667 deletions.
2 changes: 1 addition & 1 deletion javascript/latexContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ export const ending = `
%\\shipoutAnswer
\\newpage
\\markboth{References}{References}
\\renewcommand{\\chaptermark}[1]{\\markboth{References}{References}}
\\input{./others/97references97.tex}
\\newpage
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
},
"homepage": "https://sourceacademy.org/sicpjs",
"devDependencies": {
"@babel/node": "^7.24.6",
"@babel/core": "^7.24.6",
"@babel/preset-env": "^7.24.6",
"@babel/node": "^7.24.7",
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"fs-extra": "^11.2.0",
"http-server": "^14.1.1",
"husky": "^8.0.3",
Expand Down
Binary file added static/img_javascript/ex-3-13-sol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img_javascript/ex-3-14-sol-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img_javascript/ex-3-14-sol-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img_javascript/ex5-1-solution-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/img_javascript/ex5-1-solution-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions xml/chapter1/section1/subsection6.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,9 @@ abs(-5);
<META>alternative-expression</META> and returns its value as the value of the
conditional.<FOOTNOTE>
<INDEX>conditional expression<SUBINDEX>non-boolean value as predicate</SUBINDEX></INDEX>
Conditionals in full JavaScript accept any value, not just a boolean, as the result of evaluating
Conditionals
<LABEL NAME="foot:any-value-as-predicate"/>
in full JavaScript accept any value, not just a boolean, as the result of evaluating
the <META>predicate</META> expression (see footnote<SPACE/><REF NAME="foot:truthy"/>
in section<SPACE/><REF NAME="sec:eval-data-structures"/> for details). The programs in this book
use only boolean values as predicates of conditionals.
Expand Down Expand Up @@ -591,8 +593,10 @@ $\vdots$
<INDEX>evaluation<SUBINDEX>of <JAVASCRIPTINLINE>&amp;&amp;</JAVASCRIPTINLINE><ORDER>of ;1</ORDER></SUBINDEX><FRAGILE/></INDEX>
<EM>logical conjunction</EM>, meaning roughly
the same as the English word <QUOTE>and.</QUOTE>
This syntactic form is syntactic sugar<FOOTNOTE>
Syntactic forms that are simply convenient
We assume<FOOTNOTE>This assumption is justified by the restriction mentioned
in footnote<SPACE/><REF NAME="foot:any-value-as-predicate"/>. Full JavaScript
needs to consider the case where the result of evaluating <META>expression</META><LATEXINLINE>$_1$</LATEXINLINE> is neither true nor false.</FOOTNOTE> this syntactic form to be syntactic
sugar<FOOTNOTE>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</EM>, to use a
phrase coined by
Expand All @@ -617,7 +621,7 @@ $\vdots$
<INDEX>evaluation<SUBINDEX>of {\tt "|"|}<ORDER>of ;2</ORDER></SUBINDEX><FRAGILE/></INDEX>
<EM>logical disjunction</EM>, meaning roughly
the same as the English word <QUOTE>or.</QUOTE>
This syntactic form is syntactic sugar for<BR/>
We assume this syntactic form to be syntactic sugar for<BR/>
<META>expression</META><LATEXINLINE>$_1$</LATEXINLINE> <JAVASCRIPTINLINE>?</JAVASCRIPTINLINE>
<JAVASCRIPTINLINE>true</JAVASCRIPTINLINE> <JAVASCRIPTINLINE>:</JAVASCRIPTINLINE>
<META>expression</META><LATEXINLINE>$_2$</LATEXINLINE>.
Expand Down
12 changes: 5 additions & 7 deletions xml/chapter1/section1/subsection7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -708,23 +708,21 @@ function sqrt_iter(guess, x) {
<REQUIRES>sqrt</REQUIRES>
<REQUIRES>improve</REQUIRES>
<REQUIRES>sqrt_iter</REQUIRES>
<REQUIRES>square_definition</REQUIRES>
<EXAMPLE>example_1.8</EXAMPLE>
<JAVASCRIPT>
const error_threshold = 0.01;
const relative_tolerance = 0.0001;
function is_good_enough(guess, x) {
return relative_error(guess, improve(guess, x))
&lt; error_threshold;
}
function relative_error(estimate, reference) {
return abs(estimate - reference) / reference;
return abs(square(guess) - x) &lt; guess * relative_tolerance;
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
<SNIPPET HIDE="yes">
<NAME>example_1.8</NAME>
<JAVASCRIPT>
sqrt(3);
display(sqrt(0.0001));
display(sqrt(4000000000000));
</JAVASCRIPT>
<SCHEME>
</SCHEME>
Expand Down
11 changes: 6 additions & 5 deletions xml/chapter1/section2/subsection6.xml
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,13 @@ function timed_prime_test(n) {
function start_prime_test(n, start_time) {
return is_prime(n)
? report_prime(get_time() - start_time)
: true;
: false;
}
<SHORT_SPACE_AND_ALLOW_BREAK/>
function report_prime(elapsed_time) {
display(" *** ");
display(elapsed_time);
return true;
}
</JAVASCRIPT>
</SNIPPET>
Expand Down Expand Up @@ -726,8 +727,8 @@ function search_for_primes(start, times) {
? true
: start &gt; 2 &amp;&amp; start % 2 === 0
? search_for_primes(start + 1, times)
// if we get undefined -&gt; its a prime
: is_undefined(timed_prime_test(start))
// if we get true, it's a prime
: timed_prime_test(start)
? search_for_primes(start + 2, times - 1)
: search_for_primes(start + 2, times);
}
Expand All @@ -740,8 +741,8 @@ function search_for_primes(start, times) {
<REQUIRES>search_for_primes_definition</REQUIRES>
<JAVASCRIPT>
search_for_primes(10000, 3);
// search_for_primes(100000, 3);
// search_for_primes(1000000, 3);
// search_for_primes(10000000, 3);
// search_for_primes(10000000000, 3);
</JAVASCRIPT>
<SCHEME>
</SCHEME>
Expand Down
2 changes: 1 addition & 1 deletion xml/chapter2/section2/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ function matrix_times_vector(m, v) {
function transpose(mat) {
return accumulate_n(<METAPHRASE>??</METAPHRASE>, <METAPHRASE>??</METAPHRASE>, mat);
}
function matrix_times_matrix(n, m) {
function matrix_times_matrix(m, n) {
const cols = transpose(n);
return map(<METAPHRASE>??</METAPHRASE>, m);
}
Expand Down
2 changes: 2 additions & 0 deletions xml/chapter2/section3/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ tree_to_list_2(union_set_as_tree(
</JAVASCRIPT>
</SNIPPET>
<SNIPPET>
<NAME>intersection_set_as_tree_solution</NAME>
<REQUIRES>intersection_set_ordered</REQUIRES>
<REQUIRES>list_to_tree</REQUIRES>
<REQUIRES>tree_to_list_2</REQUIRES>
Expand Down Expand Up @@ -1639,6 +1640,7 @@ lookup(3, list(make_record(2, "Venus"),
<LABEL NAME="ex:set-lookup-binary-tree"/>
<SOLUTION>
<SNIPPET>
<NAME>ex_set_lookup_binary_tree</NAME>
<REQUIRES>make_tree_function</REQUIRES>
<REQUIRES>record</REQUIRES>
<EXAMPLE>lookup_example2</EXAMPLE>
Expand Down
4 changes: 2 additions & 2 deletions xml/chapter2/section4/subsection2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function magnitude_rectangular(z) {
square(imag_part_rectangular(z)));
}
function angle_rectangular(z) {
return math_atan(imag_part_rectangular(z),
return math_atan2(imag_part_rectangular(z),
real_part_rectangular(z));
}
function make_from_real_imag_rectangular(x, y) {
Expand Down Expand Up @@ -297,7 +297,7 @@ function angle_polar(z) { return tail(z); }
function make_from_real_imag_polar(x, y) {
return attach_tag("polar",
pair(math_sqrt(square(x) + square(y)),
math_atan(y, x)));
math_atan2(y, x)));
}
<ALLOW_BREAK/>
function make_from_mag_ang_polar(r, a) {
Expand Down
12 changes: 6 additions & 6 deletions xml/chapter2/section4/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ function install_rectangular_package() {
return math_sqrt(square(real_part(z)) + square(imag_part(z)));
}
function angle(z) {
return math_atan(imag_part(z), real_part(z));
return math_atan2(imag_part(z), real_part(z));
}
function make_from_mag_ang(r, a) {
return pair(r * math_cos(a), r * math_sin(a));
Expand Down Expand Up @@ -508,7 +508,7 @@ function install_polar_package() {
}
function make_from_real_imag(x, y) {
return pair(math_sqrt(square(x) + square(y)),
math_atan(y, x));
math_atan2(y, x));
}

// interface to the rest of the system
Expand Down Expand Up @@ -1551,7 +1551,7 @@ function make_from_real_imag(x, y) {
: op === "magnitude"
? math_sqrt(square(x) + square(y))
: op === "angle"
? math_atan(y, x)
? math_atan2(y, x)
: error(op, "unknown op -- make_from_real_imag");
}
return dispatch;
Expand All @@ -1567,7 +1567,7 @@ function make_from_real_imag(x, y) {
: op === "magnitude"
? math_sqrt(square(x) + square(y))
: op === "angle"
? math_atan(y, x)
? math_atan2(y, x)
: error(op, "unknown op -- make_from_real_imag");
}
return dispatch;
Expand Down Expand Up @@ -1696,7 +1696,7 @@ function install_rectangular_package() {
square(imag_part(z)));
}
function angle(z) {
return math_atan(imag_part(z), real_part(z));
return math_atan2(imag_part(z), real_part(z));
}
function make_from_mag_ang(r, a) {
return pair(r * math_cos(a), r * math_sin(a));
Expand Down Expand Up @@ -1730,7 +1730,7 @@ function install_polar_package() {
}
function make_from_real_imag(x, y) {
return pair(math_sqrt(square(x) + square(y)),
math_atan(y, x));
math_atan2(y, x));
}

// interface to the rest of the system
Expand Down
4 changes: 2 additions & 2 deletions xml/chapter2/section5/subsection2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ function install_rectangular_package() {
square(imag_part(z)));
}
function angle(z) {
return math_atan(imag_part(z), real_part(z));
return math_atan2(imag_part(z), real_part(z));
}
function make_from_mag_ang(r, a) {
return pair(r * math_cos(a), r * math_sin(a));
Expand Down Expand Up @@ -515,7 +515,7 @@ function install_polar_package() {
}
function make_from_real_imag(x, y) {
return pair(math_sqrt(square(x) + square(y)),
math_atan(y, x));
math_atan2(y, x));
}

// interface to the rest of the system
Expand Down
132 changes: 132 additions & 0 deletions xml/chapter3/section3/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,17 @@ list_ref(z, 100);
</JAVASCRIPT>
</SPLITINLINE>
<LABEL NAME="ex:make-cycle"/>
<SOLUTION>
(provided by GitHub user jonathantorres)
If we try to compute <JAVASCRIPTINLINE>last_pair(z)</JAVASCRIPTINLINE>, the
program will enter in a infinite loop, since the end of the list points back
to the beginning.

<FIGURE src="img_javascript/ex-3-13-sol.png">
<LABEL NAME= "ex:3-13-sol"/>
</FIGURE>

</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down Expand Up @@ -934,6 +945,33 @@ const w = mystery(v);
What would be printed as the values of <SCHEMEINLINE>v</SCHEMEINLINE>
and <SCHEMEINLINE>w</SCHEMEINLINE>?
<LABEL NAME="ex:mystery"/>
<SOLUTION>
(provided by GitHub user jonathantorres)
The application <JAVASCRIPTINLINE>mystery(x)</JAVASCRIPTINLINE> will
reverse the list <JAVASCRIPTINLINE>x</JAVASCRIPTINLINE> in-place.
Initially
<JAVASCRIPTINLINE>v</JAVASCRIPTINLINE> looks like this:

<FIGURE src="img_javascript/ex-3-14-sol-2.png">
<LABEL NAME= "ex:3-14-sol-1"/>
</FIGURE>

After evaluating
<JAVASCRIPTINLINE>const w = mystery(v);</JAVASCRIPTINLINE>
the values of
<JAVASCRIPTINLINE>v</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>w</JAVASCRIPTINLINE> become:

<FIGURE src="img_javascript/ex-3-14-sol-2.png">
<LABEL NAME= "ex:3-14-sol-2"/>
</FIGURE>

The function <JAVASCRIPTINLINE>display</JAVASCRIPTINLINE>
prints <JAVASCRIPTINLINE>["a", null]</JAVASCRIPTINLINE> for
<JAVASCRIPTINLINE>v</JAVASCRIPTINLINE> and
<JAVASCRIPTINLINE>["d", ["c", ["b", ["a", null]]]]</JAVASCRIPTINLINE> for
<JAVASCRIPTINLINE>w</JAVASCRIPTINLINE>.
</SOLUTION>
</EXERCISE>

<INDEX>mutable data objects<SUBINDEX>list structure<CLOSE/></SUBINDEX></INDEX>
Expand Down Expand Up @@ -1522,6 +1560,56 @@ display(count_pairs(cycle));
distinct pairs in any structure. (Hint: Traverse the structure, maintaining
an auxiliary data structure that is used to keep track of which pairs have
already been counted.)
<SOLUTION>
<SNIPPET>
<EXAMPLE>exercise_3_17_solution_example</EXAMPLE>
<JAVASCRIPT>
// solution provided by GitHub user clean99

function count_pairs(x) {
let counted_pairs = null;
function is_counted_pair(current_counted_pairs, x) {
return is_null(current_counted_pairs)
? false
: head(current_counted_pairs) === x
? true
: is_counted_pair(tail(current_counted_pairs), x);
}
function count(x) {
if(! is_pair(x) || is_counted_pair(counted_pairs, x)) {
return 0;
} else {
counted_pairs = pair(x, counted_pairs);
return count(head(x)) +
count(tail(x)) +
1;
}
}
return count(x);
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>exercise_3_17_solution_example</NAME>
<JAVASCRIPT>
const three_list = list("a", "b", "c");
const one = pair("d", "e");
const two = pair(one, one);
const four_list = pair(two, "f");
const seven_list = pair(two, two);
const cycle = list("g", "h", "i");
set_tail(tail(tail(cycle)), cycle);

// return 3; return 3; return 3;
display(count_pairs(three_list));
display(count_pairs(four_list));
display(count_pairs(seven_list));

// return 3
display(count_pairs(cycle));
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand All @@ -1542,6 +1630,50 @@ display(count_pairs(cycle));
would go into an infinite loop. Exercise<SPACE/><REF NAME="ex:make-cycle"/>
constructed such lists.
<LABEL NAME="ex:find-cycle"/>
<SOLUTION>
<SNIPPET>
<EXAMPLE>exercise_3_18_solution_example</EXAMPLE>
<JAVASCRIPT>
// solution provided by GitHub user clean99

function contains_cycle(x) {
let counted_pairs = null;
function is_counted_pair(counted_pairs, x) {
return is_null(counted_pairs)
? false
: head(counted_pairs) === x
? true
: is_counted_pair(tail(counted_pairs), x);
}
function detect_cycle(x) {
if (is_null(x)) {
return false;
} else if (is_counted_pair(counted_pairs, x)) {
return true;
} else {
counted_pairs = pair(x, counted_pairs);
return detect_cycle(tail(x));
}
}
return detect_cycle(x);
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>exercise_3_18_solution_example</NAME>
<JAVASCRIPT>
const three_list = list("a", "b", "c");
const cycle = list("g", "h", "i");
set_tail(tail(tail(cycle)), cycle);

// displays false
display(contains_cycle(three_list));

// displays true
display(contains_cycle(cycle));
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
Loading

0 comments on commit b969e50

Please sign in to comment.