Skip to content

Commit e1113bd

Browse files
authored
Merge branch 'master' into ex-3-23-sol
2 parents dd110b1 + aef4e9c commit e1113bd

File tree

8 files changed

+438
-19
lines changed

8 files changed

+438
-19
lines changed

xml/chapter1/section1/subsection6.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,24 @@ a === 4
965965
</JAVASCRIPT>
966966
</SPLIT>
967967
<LABEL NAME="ex:1_1"/>
968+
<SOLUTION>
969+
Solution provided by GitHub user Emekk
970+
<OL>
971+
<LI>10</LI>
972+
<LI>12</LI>
973+
<LI>8</LI>
974+
<LI>3</LI>
975+
<LI>6</LI>
976+
<LI>3</LI>
977+
<LI>4</LI>
978+
<LI>19</LI>
979+
<LI>false</LI>
980+
<LI>4</LI>
981+
<LI>16</LI>
982+
<LI>6</LI>
983+
<LI>16</LI>
984+
</OL>
985+
</SOLUTION>
968986
</EXERCISE>
969987

970988
<EXERCISE>

xml/chapter2/section5/subsection1.xml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,54 @@ function contents(datum) {
932932
arithmetic package. This operation should work for ordinary numbers,
933933
rational numbers, and complex numbers.
934934
<LABEL NAME="ex:equ?"/>
935-
</EXERCISE>
935+
<SOLUTION>
936+
<SNIPPET EVAL="no">
937+
<JAVASCRIPT>
938+
// provided by GitHub user clean99
939+
940+
function is_equal(x, y) {
941+
return apply_generic("is_equal", list(x, y));
942+
}
943+
944+
function install_javascript_number_package() {
945+
// ...
946+
947+
put("is_equal", list("javascript_number", "javascript_number"),
948+
(x, y) => x === y);
949+
950+
// ...
951+
}
952+
953+
function install_rational_package() {
954+
// ...
955+
956+
function is_equal(x, y) {
957+
return numer(x) * denom(y) === numer(y) * denom(x);
958+
}
959+
960+
put("is_equal", list("rational", "rational"), is_equal);
961+
962+
// ...
963+
}
964+
965+
function install_complex_package() {
966+
// ...
967+
968+
function is_equal(z1, z2) {
969+
return real_part(z1) === real_part(z2)
970+
? imag_part(z1) === imag_part(z2)
971+
: false;
972+
}
973+
974+
put("is_equal", list("complex", "complex"),
975+
is_equal);
976+
977+
//...
978+
}
979+
</JAVASCRIPT>
980+
</SNIPPET>
981+
</SOLUTION>
982+
</EXERCISE>
936983

937984
<EXERCISE>
938985
Define a generic predicate

xml/chapter2/section5/subsection3.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,37 @@ head(tail(tail(tail(mul(p1, p2)))));
10251025
subtraction of polynomials.
10261026
(Hint: You may find it helpful to define a generic negation operation.)
10271027
<LABEL NAME="ex:sub-poly"/>
1028+
<SOLUTION>
1029+
<SNIPPET EVAL="no">
1030+
<NAME>ex_2_88_solution</NAME>
1031+
<JAVASCRIPT>
1032+
function sub_terms(L1, L2) {
1033+
if (is_empty_termlist(L1)) {
1034+
return L2;
1035+
} else if (is_empty_termlist(L2)) {
1036+
return L1;
1037+
} else {
1038+
const t1 = first_term(L1);
1039+
const t2 = first_term(L2);
1040+
return order(t1) &gt; order(t2)
1041+
? adjoin_term(t1, sub_terms(rest_terms(L1), L2))
1042+
: order(t1) &lt; order(t2)
1043+
? adjoin_term(t2, sub_terms(L1, rest_terms(L2)))
1044+
: adjoin_term(make_term(order(t1),
1045+
sub(coeff(t1), coeff(t2))),
1046+
sub_terms(rest_terms(L1),
1047+
rest_terms(L2)));
1048+
}
1049+
}
1050+
function sub_poly(p1, p2) {
1051+
return is_same_variable(variable(p1), variable(p2))
1052+
? make_poly(variable(p1),
1053+
sub_terms(term_list(p1), term_list(p2)))
1054+
: error(list(p1, p2), "polys not in same var -- sub_poly");
1055+
}
1056+
</JAVASCRIPT>
1057+
</SNIPPET>
1058+
</SOLUTION>
10281059
</EXERCISE>
10291060

10301061
<EXERCISE>

xml/chapter3/section1/subsection3.xml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,22 +1091,20 @@ paul_acc("withdraw", "rosebud")(50); // Withdraws 50, should return 40
10911091
<SNIPPET>
10921092
<EXAMPLE>exercise_3_8_solution_example</EXAMPLE>
10931093
<JAVASCRIPT>
1094-
function make_f(init) {
1095-
return x => {
1096-
init = x - init;
1097-
return init;
1098-
};
1094+
let v = -0.5;
1095+
function f(x) {
1096+
v = x + v;
1097+
return v;
10991098
}
1100-
1101-
const f = make_f(1/2);
11021099
</JAVASCRIPT>
11031100
</SNIPPET>
11041101

11051102
<SNIPPET HIDE="yes">
11061103
<NAME>exercise_3_8_solution_example</NAME>
11071104
<JAVASCRIPT>
1108-
display(f(1) + f(0));
1109-
display(f(0) + f(1));
1105+
// try these separately
1106+
display(f(0) + f(1)); // returns 0
1107+
display(f(1) + f(0)); // returns 1
11101108
</JAVASCRIPT>
11111109
</SNIPPET>
11121110
</SOLUTION>

xml/chapter3/section3/subsection1.xml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ set_to_wow(z2);
14411441
<JAVASCRIPT>function</JAVASCRIPT>
14421442
</SPLITINLINE>
14431443
<SNIPPET EVAL="yes">
1444+
<NAME>count_pairs</NAME>
14441445
<INDEX><DECLARATION>count_pairs</DECLARATION><FRAGILE/></INDEX>
14451446
<SCHEME>
14461447
(define (count-pairs x)
@@ -1476,18 +1477,18 @@ function count_pairs(x) {
14761477
<SOLUTION>
14771478
<SNIPPET>
14781479
<NAME>exercise_3_16_solution</NAME>
1480+
<REQUIRES>count_pairs</REQUIRES>
14791481
<EXAMPLE>exercise_3_16_solution_example</EXAMPLE>
14801482
<SCHEME>
14811483
</SCHEME>
14821484
<JAVASCRIPT>
1483-
const cycle = make_cycle(three_list);
1484-
14851485
const three_list = list("a", "b", "c");
1486-
1487-
const one = pair("a", "b");
1488-
const three = pair(one, one);
1489-
const four = pair(three, "c");
1490-
const seven = pair(three, three);
1486+
const one = pair("d", "e");
1487+
const two = pair(one, one);
1488+
const four_list = pair(two, "f");
1489+
const seven_list = pair(two, two);
1490+
const cycle = list("g", "h", "i");
1491+
set_tail(tail(tail(cycle)), cycle);
14911492
</JAVASCRIPT>
14921493
</SNIPPET>
14931494

@@ -1496,8 +1497,8 @@ const seven = pair(three, three);
14961497
<JAVASCRIPT>
14971498
// return 3; return 4; return 7;
14981499
display(count_pairs(three_list));
1499-
display(count_pairs(four));
1500-
display(count_pairs(seven));
1500+
display(count_pairs(four_list));
1501+
display(count_pairs(seven_list));
15011502

15021503
// never return at all
15031504
display(count_pairs(cycle));

xml/chapter3/section3/subsection2.xml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,34 @@ delete_queue(q1);
738738
</SPLITINLINE>
739739
that takes a queue as input and prints the sequence of items in the queue.
740740
<LABEL NAME="ex:3_21"/>
741+
<SOLUTION>
742+
<SNIPPET>
743+
<REQUIRES>make_queue</REQUIRES>
744+
<REQUIRES>modify_pointers</REQUIRES>
745+
<REQUIRES>insert_queue</REQUIRES>
746+
<REQUIRES>is_empty_queue</REQUIRES>
747+
<REQUIRES>delete_queue</REQUIRES>
748+
<EXAMPLE>ex_3_21_solution_example</EXAMPLE>
749+
<JAVASCRIPT>
750+
function print_queue(q) {
751+
return display(head(q));
752+
}
753+
</JAVASCRIPT>
754+
</SNIPPET>
755+
<SNIPPET HIDE="yes">
756+
<NAME>ex_3_21_solution_example</NAME>
757+
<JAVASCRIPT>
758+
const q1 = make_queue();
759+
print_queue(q1); // prints: null
760+
insert_queue(q1, "a");
761+
print_queue(q1); // prints: ["a", null]
762+
insert_queue(q1, "b");
763+
print_queue(q1); // prints: ["a", ["b", null]]
764+
delete_queue(q1);
765+
print_queue(q1); // prints: ["b", null]
766+
</JAVASCRIPT>
767+
</SNIPPET>
768+
</SOLUTION>
741769
</EXERCISE>
742770

743771
<EXERCISE>
@@ -786,6 +814,76 @@ function make_queue() {
786814
and provide implementations of the queue operations using this
787815
representation.
788816
<LABEL NAME="ex:3_22"/>
817+
<SOLUTION>
818+
<SNIPPET>
819+
<EXAMPLE>ex_3_22_example</EXAMPLE>
820+
<JAVASCRIPT>
821+
// provided by GitHub user devinryu
822+
823+
function make_queue() {
824+
let front_ptr = null;
825+
let rear_ptr = null;
826+
function is_empty_queue() {
827+
return is_null(front_ptr);
828+
}
829+
function insert_queue(item) {
830+
let new_pair = pair(item, null);
831+
if (is_empty_queue()) {
832+
front_ptr = new_pair;
833+
rear_ptr = new_pair;
834+
} else {
835+
set_tail(rear_ptr, new_pair);
836+
rear_ptr = new_pair;
837+
}
838+
}
839+
function delete_queue() {
840+
if (is_empty_queue()) {
841+
error("FRONT called with an empty queue");
842+
} else {
843+
front_ptr = tail(front_ptr);
844+
}
845+
}
846+
function print_queue() {
847+
display(front_ptr);
848+
}
849+
function dispatch(m) {
850+
return m === "insert_queue"
851+
? insert_queue
852+
: m === "delete_queue"
853+
? delete_queue
854+
: m === "is_empty_queue"
855+
? is_empty_queue
856+
: m === "print_queue"
857+
? print_queue
858+
: error(m, "Unknow operation -- DISPATCH");
859+
}
860+
return dispatch;
861+
}
862+
function insert_queue(queue, item) {
863+
return queue("insert_queue")(item);
864+
}
865+
function delete_queue(queue) {
866+
return queue("delete_queue")();
867+
}
868+
function print_queue(queue) {
869+
return queue("print_queue")();
870+
}
871+
</JAVASCRIPT>
872+
</SNIPPET>
873+
<SNIPPET HIDE="yes">
874+
<NAME>ex_3_22_example</NAME>
875+
<JAVASCRIPT>
876+
const q = make_queue();
877+
print_queue(q); // prints: null
878+
insert_queue(q, "a");
879+
print_queue(q); // prints: ["a", null]
880+
insert_queue(q, "b");
881+
print_queue(q); // prints: ["a", ["b", null]]
882+
delete_queue(q);
883+
print_queue(q); // prints: ["b", null]
884+
</JAVASCRIPT>
885+
</SNIPPET>
886+
</SOLUTION>
789887
</EXERCISE>
790888

791889
<EXERCISE>

0 commit comments

Comments
 (0)