Skip to content

Commit 3eb9584

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

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

xml/chapter2/section5/subsection1.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,54 @@ function install_complex_package() {
994994
arithmetic package. This operation should work for ordinary numbers,
995995
rational numbers, and complex numbers.
996996
<LABEL NAME="ex:=zero?"/>
997+
<SOLUTION>
998+
<SNIPPET EVAL="no">
999+
<JAVASCRIPT>
1000+
// provided by GitHub user clean99
1001+
1002+
function is_equal_to_zero(x) {
1003+
return apply_generic("is_equal", list(x));
1004+
}
1005+
1006+
function install_javascript_number_package() {
1007+
// ...
1008+
1009+
put("is_equal_to_zero", "javascript_number",
1010+
x => x === 0);
1011+
1012+
// ...
1013+
}
1014+
1015+
function install_rational_package() {
1016+
// ...
1017+
1018+
function is_equal_to_zero(x) {
1019+
return numer(x) === 0;
1020+
}
1021+
1022+
put("is_equal_to_zero", "rational",
1023+
is_equal_to_zero);
1024+
1025+
// ...
1026+
}
1027+
1028+
function install_complex_package() {
1029+
// ...
1030+
1031+
function is_equal_to_zero(z) {
1032+
return real_part(z) === 0
1033+
? imag_part(z) === 0
1034+
: false;
1035+
}
1036+
1037+
put("is_equal_to_zero", "complex",
1038+
is_equal_to_zero);
1039+
1040+
//...
1041+
}
1042+
</JAVASCRIPT>
1043+
</SNIPPET>
1044+
</SOLUTION>
9971045
</EXERCISE>
9981046

9991047
<INDEX>generic arithmetic operations<CLOSE/></INDEX>

xml/chapter3/section3/subsection1.xml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,56 @@ display(count_pairs(cycle));
15221522
distinct pairs in any structure. (Hint: Traverse the structure, maintaining
15231523
an auxiliary data structure that is used to keep track of which pairs have
15241524
already been counted.)
1525+
<SOLUTION>
1526+
<SNIPPET>
1527+
<EXAMPLE>exercise_3_17_solution_example</EXAMPLE>
1528+
<JAVASCRIPT>
1529+
// solution provided by GitHub user clean99
1530+
1531+
function count_pairs(x) {
1532+
let counted_pairs = null;
1533+
function is_counted_pair(current_counted_pairs, x) {
1534+
return is_null(current_counted_pairs)
1535+
? false
1536+
: head(current_counted_pairs) === x
1537+
? true
1538+
: is_counted_pair(tail(current_counted_pairs), x);
1539+
}
1540+
function count(x) {
1541+
if(! is_pair(x) || is_counted_pair(counted_pairs, x)) {
1542+
return 0;
1543+
} else {
1544+
counted_pairs = pair(x, counted_pairs);
1545+
return count(head(x)) +
1546+
count(tail(x)) +
1547+
1;
1548+
}
1549+
}
1550+
return count(x);
1551+
}
1552+
</JAVASCRIPT>
1553+
</SNIPPET>
1554+
<SNIPPET HIDE="yes">
1555+
<NAME>exercise_3_17_solution_example</NAME>
1556+
<JAVASCRIPT>
1557+
const three_list = list("a", "b", "c");
1558+
const one = pair("d", "e");
1559+
const two = pair(one, one);
1560+
const four_list = pair(two, "f");
1561+
const seven_list = pair(two, two);
1562+
const cycle = list("g", "h", "i");
1563+
set_tail(tail(tail(cycle)), cycle);
1564+
1565+
// return 3; return 3; return 3;
1566+
display(count_pairs(three_list));
1567+
display(count_pairs(four_list));
1568+
display(count_pairs(seven_list));
1569+
1570+
// return 3
1571+
display(count_pairs(cycle));
1572+
</JAVASCRIPT>
1573+
</SNIPPET>
1574+
</SOLUTION>
15251575
</EXERCISE>
15261576

15271577
<EXERCISE>
@@ -1542,6 +1592,50 @@ display(count_pairs(cycle));
15421592
would go into an infinite loop. Exercise<SPACE/><REF NAME="ex:make-cycle"/>
15431593
constructed such lists.
15441594
<LABEL NAME="ex:find-cycle"/>
1595+
<SOLUTION>
1596+
<SNIPPET>
1597+
<EXAMPLE>exercise_3_18_solution_example</EXAMPLE>
1598+
<JAVASCRIPT>
1599+
// solution provided by GitHub user clean99
1600+
1601+
function contains_cycle(x) {
1602+
let counted_pairs = null;
1603+
function is_counted_pair(counted_pairs, x) {
1604+
return is_null(counted_pairs)
1605+
? false
1606+
: head(counted_pairs) === x
1607+
? true
1608+
: is_counted_pair(tail(counted_pairs), x);
1609+
}
1610+
function detect_cycle(x) {
1611+
if (is_null(x)) {
1612+
return false;
1613+
} else if (is_counted_pair(counted_pairs, x)) {
1614+
return true;
1615+
} else {
1616+
counted_pairs = pair(x, counted_pairs);
1617+
return detect_cycle(tail(x));
1618+
}
1619+
}
1620+
return detect_cycle(x);
1621+
}
1622+
</JAVASCRIPT>
1623+
</SNIPPET>
1624+
<SNIPPET HIDE="yes">
1625+
<NAME>exercise_3_18_solution_example</NAME>
1626+
<JAVASCRIPT>
1627+
const three_list = list("a", "b", "c");
1628+
const cycle = list("g", "h", "i");
1629+
set_tail(tail(tail(cycle)), cycle);
1630+
1631+
// displays false
1632+
display(contains_cycle(three_list));
1633+
1634+
// displays true
1635+
display(contains_cycle(cycle));
1636+
</JAVASCRIPT>
1637+
</SNIPPET>
1638+
</SOLUTION>
15451639
</EXERCISE>
15461640

15471641
<EXERCISE>

0 commit comments

Comments
 (0)