Skip to content

Commit

Permalink
Fixes #999 (#1001)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PossibilityZero authored Jun 30, 2024
1 parent 244a97d commit e1534f6
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions xml/chapter2/section3/subsection2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ list("x", "+", list(3, "*", list("x", "+", list("y", "+", 2))))
example:
<SNIPPET EVAL="no">
<JAVASCRIPT>
list("x", "+", "3", "*", list("x", "+", "y", "+", 2))
list("x", "+", 3, "*", list("x", "+", "y", "+", 2))
</JAVASCRIPT>
</SNIPPET>
</JAVASCRIPT>
Expand Down Expand Up @@ -1436,15 +1436,27 @@ deriv(list("x", "*", 4), "x");
<EXAMPLE>example_2.61_2</EXAMPLE>
<JAVASCRIPT>
function items_before_first(op, s) {
return head(s) === op
return is_string(head(s)) &amp;&amp; head(s) === op
? null
: pair(head(s),
items_before_first(op, tail(s)));
}
function items_after_first(op, s) {
return head(s) === op
return is_string(head(s)) &amp;&amp; head(s) === op
? tail(s)
: items_after_first(op, tail(s);
: items_after_first(op, tail(s));
}
function simplify_unary_list(s) {
return is_pair(s) &amp;&amp; is_null(tail(s))
? head(s)
: s;
}
function contains_plus(s) {
return is_null(s)
? false
: is_string(head(s)) &amp;&amp; head(s) === "+"
? true
: contains_plus(tail(s));
}
function make_sum(a1, a2) {
return number_equal(a1, 0)
Expand All @@ -1458,14 +1470,13 @@ function make_sum(a1, a2) {
// a sequence of terms and operators is a sum
// if and only if at least one + operator occurs
function is_sum(x) {
return is_pair(x) &amp;&amp;
! (is_null(member("+", x));
return is_pair(x) &amp;&amp; contains_plus(x);
}
function addend(s) {
return items_before_first("+", s);
return simplify_unary_list(items_before_first("+", s));
}
function augend(s) {
return items_after_first("+", s);
return simplify_unary_list(items_after_first("+", s));
}
function make_product(m1, m2) {
return number_equal(m1, 0) || number_equal(m2, 0)
Expand All @@ -1481,13 +1492,13 @@ function make_product(m1, m2) {
// a sequence of terms and operators is a product
// if and only if no + operator occurs
function is_product(x) {
return is_pair(x) &amp;&amp; is_null(member("+", x);
return is_pair(x) &amp;&amp; ! contains_plus(x);
}
function multiplier(s) {
return items_before_first("*", s);
return simplify_unary_list(items_before_first("*", s));
}
function multiplicand(s) {
return items_after_first("*", s);
return simplify_unary_list(items_after_first("*", s));
}
function deriv(exp, variable) {
return is_number(exp)
Expand Down

0 comments on commit e1534f6

Please sign in to comment.