Skip to content

Commit e1534f6

Browse files
Fixes #999 (#1001)
* Fix typo in Chapter 2.3.2 / Exercise 2.58 * Fix errors in solution to problem 2.58 (#999) - Correct syntax errors - Fix usage of features not defined for the Source interpreter of Chapter 2 - Fix logic error handing single-item lists to deriv function
1 parent 244a97d commit e1534f6

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

xml/chapter2/section3/subsection2.xml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ list("x", "+", list(3, "*", list("x", "+", list("y", "+", 2))))
13401340
example:
13411341
<SNIPPET EVAL="no">
13421342
<JAVASCRIPT>
1343-
list("x", "+", "3", "*", list("x", "+", "y", "+", 2))
1343+
list("x", "+", 3, "*", list("x", "+", "y", "+", 2))
13441344
</JAVASCRIPT>
13451345
</SNIPPET>
13461346
</JAVASCRIPT>
@@ -1436,15 +1436,27 @@ deriv(list("x", "*", 4), "x");
14361436
<EXAMPLE>example_2.61_2</EXAMPLE>
14371437
<JAVASCRIPT>
14381438
function items_before_first(op, s) {
1439-
return head(s) === op
1439+
return is_string(head(s)) &amp;&amp; head(s) === op
14401440
? null
14411441
: pair(head(s),
14421442
items_before_first(op, tail(s)));
14431443
}
14441444
function items_after_first(op, s) {
1445-
return head(s) === op
1445+
return is_string(head(s)) &amp;&amp; head(s) === op
14461446
? tail(s)
1447-
: items_after_first(op, tail(s);
1447+
: items_after_first(op, tail(s));
1448+
}
1449+
function simplify_unary_list(s) {
1450+
return is_pair(s) &amp;&amp; is_null(tail(s))
1451+
? head(s)
1452+
: s;
1453+
}
1454+
function contains_plus(s) {
1455+
return is_null(s)
1456+
? false
1457+
: is_string(head(s)) &amp;&amp; head(s) === "+"
1458+
? true
1459+
: contains_plus(tail(s));
14481460
}
14491461
function make_sum(a1, a2) {
14501462
return number_equal(a1, 0)
@@ -1458,14 +1470,13 @@ function make_sum(a1, a2) {
14581470
// a sequence of terms and operators is a sum
14591471
// if and only if at least one + operator occurs
14601472
function is_sum(x) {
1461-
return is_pair(x) &amp;&amp;
1462-
! (is_null(member("+", x));
1473+
return is_pair(x) &amp;&amp; contains_plus(x);
14631474
}
14641475
function addend(s) {
1465-
return items_before_first("+", s);
1476+
return simplify_unary_list(items_before_first("+", s));
14661477
}
14671478
function augend(s) {
1468-
return items_after_first("+", s);
1479+
return simplify_unary_list(items_after_first("+", s));
14691480
}
14701481
function make_product(m1, m2) {
14711482
return number_equal(m1, 0) || number_equal(m2, 0)
@@ -1481,13 +1492,13 @@ function make_product(m1, m2) {
14811492
// a sequence of terms and operators is a product
14821493
// if and only if no + operator occurs
14831494
function is_product(x) {
1484-
return is_pair(x) &amp;&amp; is_null(member("+", x);
1495+
return is_pair(x) &amp;&amp; ! contains_plus(x);
14851496
}
14861497
function multiplier(s) {
1487-
return items_before_first("*", s);
1498+
return simplify_unary_list(items_before_first("*", s));
14881499
}
14891500
function multiplicand(s) {
1490-
return items_after_first("*", s);
1501+
return simplify_unary_list(items_after_first("*", s));
14911502
}
14921503
function deriv(exp, variable) {
14931504
return is_number(exp)

0 commit comments

Comments
 (0)