Skip to content

Commit

Permalink
Merge branch 'master' into ex-4-4-sol
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-henz authored Jul 3, 2024
2 parents 9681e84 + aef4e9c commit b348692
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 19 deletions.
18 changes: 18 additions & 0 deletions xml/chapter1/section1/subsection6.xml
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,24 @@ a === 4
</JAVASCRIPT>
</SPLIT>
<LABEL NAME="ex:1_1"/>
<SOLUTION>
Solution provided by GitHub user Emekk
<OL>
<LI>10</LI>
<LI>12</LI>
<LI>8</LI>
<LI>3</LI>
<LI>6</LI>
<LI>3</LI>
<LI>4</LI>
<LI>19</LI>
<LI>false</LI>
<LI>4</LI>
<LI>16</LI>
<LI>6</LI>
<LI>16</LI>
</OL>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
49 changes: 48 additions & 1 deletion xml/chapter2/section5/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,54 @@ function contents(datum) {
arithmetic package. This operation should work for ordinary numbers,
rational numbers, and complex numbers.
<LABEL NAME="ex:equ?"/>
</EXERCISE>
<SOLUTION>
<SNIPPET EVAL="no">
<JAVASCRIPT>
// provided by GitHub user clean99

function is_equal(x, y) {
return apply_generic("is_equal", list(x, y));
}

function install_javascript_number_package() {
// ...

put("is_equal", list("javascript_number", "javascript_number"),
(x, y) => x === y);

// ...
}

function install_rational_package() {
// ...

function is_equal(x, y) {
return numer(x) * denom(y) === numer(y) * denom(x);
}

put("is_equal", list("rational", "rational"), is_equal);

// ...
}

function install_complex_package() {
// ...

function is_equal(z1, z2) {
return real_part(z1) === real_part(z2)
? imag_part(z1) === imag_part(z2)
: false;
}

put("is_equal", list("complex", "complex"),
is_equal);

//...
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Define a generic predicate
Expand Down
31 changes: 31 additions & 0 deletions xml/chapter2/section5/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,37 @@ head(tail(tail(tail(mul(p1, p2)))));
subtraction of polynomials.
(Hint: You may find it helpful to define a generic negation operation.)
<LABEL NAME="ex:sub-poly"/>
<SOLUTION>
<SNIPPET EVAL="no">
<NAME>ex_2_88_solution</NAME>
<JAVASCRIPT>
function sub_terms(L1, L2) {
if (is_empty_termlist(L1)) {
return L2;
} else if (is_empty_termlist(L2)) {
return L1;
} else {
const t1 = first_term(L1);
const t2 = first_term(L2);
return order(t1) &gt; order(t2)
? adjoin_term(t1, sub_terms(rest_terms(L1), L2))
: order(t1) &lt; order(t2)
? adjoin_term(t2, sub_terms(L1, rest_terms(L2)))
: adjoin_term(make_term(order(t1),
sub(coeff(t1), coeff(t2))),
sub_terms(rest_terms(L1),
rest_terms(L2)));
}
}
function sub_poly(p1, p2) {
return is_same_variable(variable(p1), variable(p2))
? make_poly(variable(p1),
sub_terms(term_list(p1), term_list(p2)))
: error(list(p1, p2), "polys not in same var -- sub_poly");
}
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
16 changes: 7 additions & 9 deletions xml/chapter3/section1/subsection3.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,22 +1091,20 @@ paul_acc("withdraw", "rosebud")(50); // Withdraws 50, should return 40
<SNIPPET>
<EXAMPLE>exercise_3_8_solution_example</EXAMPLE>
<JAVASCRIPT>
function make_f(init) {
return x => {
init = x - init;
return init;
};
let v = -0.5;
function f(x) {
v = x + v;
return v;
}

const f = make_f(1/2);
</JAVASCRIPT>
</SNIPPET>

<SNIPPET HIDE="yes">
<NAME>exercise_3_8_solution_example</NAME>
<JAVASCRIPT>
display(f(1) + f(0));
display(f(0) + f(1));
// try these separately
display(f(0) + f(1)); // returns 0
display(f(1) + f(0)); // returns 1
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
Expand Down
19 changes: 10 additions & 9 deletions xml/chapter3/section3/subsection1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ set_to_wow(z2);
<JAVASCRIPT>function</JAVASCRIPT>
</SPLITINLINE>
<SNIPPET EVAL="yes">
<NAME>count_pairs</NAME>
<INDEX><DECLARATION>count_pairs</DECLARATION><FRAGILE/></INDEX>
<SCHEME>
(define (count-pairs x)
Expand Down Expand Up @@ -1476,18 +1477,18 @@ function count_pairs(x) {
<SOLUTION>
<SNIPPET>
<NAME>exercise_3_16_solution</NAME>
<REQUIRES>count_pairs</REQUIRES>
<EXAMPLE>exercise_3_16_solution_example</EXAMPLE>
<SCHEME>
</SCHEME>
<JAVASCRIPT>
const cycle = make_cycle(three_list);

const three_list = list("a", "b", "c");

const one = pair("a", "b");
const three = pair(one, one);
const four = pair(three, "c");
const seven = pair(three, three);
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);
</JAVASCRIPT>
</SNIPPET>

Expand All @@ -1496,8 +1497,8 @@ const seven = pair(three, three);
<JAVASCRIPT>
// return 3; return 4; return 7;
display(count_pairs(three_list));
display(count_pairs(four));
display(count_pairs(seven));
display(count_pairs(four_list));
display(count_pairs(seven_list));

// never return at all
display(count_pairs(cycle));
Expand Down
98 changes: 98 additions & 0 deletions xml/chapter3/section3/subsection2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,34 @@ delete_queue(q1);
</SPLITINLINE>
that takes a queue as input and prints the sequence of items in the queue.
<LABEL NAME="ex:3_21"/>
<SOLUTION>
<SNIPPET>
<REQUIRES>make_queue</REQUIRES>
<REQUIRES>modify_pointers</REQUIRES>
<REQUIRES>insert_queue</REQUIRES>
<REQUIRES>is_empty_queue</REQUIRES>
<REQUIRES>delete_queue</REQUIRES>
<EXAMPLE>ex_3_21_solution_example</EXAMPLE>
<JAVASCRIPT>
function print_queue(q) {
return display(head(q));
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>ex_3_21_solution_example</NAME>
<JAVASCRIPT>
const q1 = make_queue();
print_queue(q1); // prints: null
insert_queue(q1, "a");
print_queue(q1); // prints: ["a", null]
insert_queue(q1, "b");
print_queue(q1); // prints: ["a", ["b", null]]
delete_queue(q1);
print_queue(q1); // prints: ["b", null]
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down Expand Up @@ -786,6 +814,76 @@ function make_queue() {
and provide implementations of the queue operations using this
representation.
<LABEL NAME="ex:3_22"/>
<SOLUTION>
<SNIPPET>
<EXAMPLE>ex_3_22_example</EXAMPLE>
<JAVASCRIPT>
// provided by GitHub user devinryu

function make_queue() {
let front_ptr = null;
let rear_ptr = null;
function is_empty_queue() {
return is_null(front_ptr);
}
function insert_queue(item) {
let new_pair = pair(item, null);
if (is_empty_queue()) {
front_ptr = new_pair;
rear_ptr = new_pair;
} else {
set_tail(rear_ptr, new_pair);
rear_ptr = new_pair;
}
}
function delete_queue() {
if (is_empty_queue()) {
error("FRONT called with an empty queue");
} else {
front_ptr = tail(front_ptr);
}
}
function print_queue() {
display(front_ptr);
}
function dispatch(m) {
return m === "insert_queue"
? insert_queue
: m === "delete_queue"
? delete_queue
: m === "is_empty_queue"
? is_empty_queue
: m === "print_queue"
? print_queue
: error(m, "Unknow operation -- DISPATCH");
}
return dispatch;
}
function insert_queue(queue, item) {
return queue("insert_queue")(item);
}
function delete_queue(queue) {
return queue("delete_queue")();
}
function print_queue(queue) {
return queue("print_queue")();
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>ex_3_22_example</NAME>
<JAVASCRIPT>
const q = make_queue();
print_queue(q); // prints: null
insert_queue(q, "a");
print_queue(q); // prints: ["a", null]
insert_queue(q, "b");
print_queue(q); // prints: ["a", ["b", null]]
delete_queue(q);
print_queue(q); // prints: ["b", null]
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
Loading

0 comments on commit b348692

Please sign in to comment.