From 3b303a3e32d6fe6939ae6a8ae9b8e19be6b9a4f7 Mon Sep 17 00:00:00 2001 From: Kai Nakazawa Date: Sat, 25 May 2024 17:06:29 +0900 Subject: [PATCH 1/2] Fix typo in Chapter 2.3.2 / Exercise 2.58 --- xml/chapter2/section3/subsection2.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml/chapter2/section3/subsection2.xml b/xml/chapter2/section3/subsection2.xml index e8cf330ed..da960f44e 100644 --- a/xml/chapter2/section3/subsection2.xml +++ b/xml/chapter2/section3/subsection2.xml @@ -1340,7 +1340,7 @@ list("x", "+", list(3, "*", list("x", "+", list("y", "+", 2)))) example: -list("x", "+", "3", "*", list("x", "+", "y", "+", 2)) +list("x", "+", 3, "*", list("x", "+", "y", "+", 2)) From 1e265a07f5d14c57c8bffbd8c6f3446af12bc689 Mon Sep 17 00:00:00 2001 From: Kai Nakazawa Date: Sun, 26 May 2024 16:27:46 +0900 Subject: [PATCH 2/2] 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 --- xml/chapter2/section3/subsection2.xml | 31 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/xml/chapter2/section3/subsection2.xml b/xml/chapter2/section3/subsection2.xml index da960f44e..e53229c00 100644 --- a/xml/chapter2/section3/subsection2.xml +++ b/xml/chapter2/section3/subsection2.xml @@ -1436,15 +1436,27 @@ deriv(list("x", "*", 4), "x"); example_2.61_2 function items_before_first(op, s) { - return head(s) === op + return is_string(head(s)) && 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)) && 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) && is_null(tail(s)) + ? head(s) + : s; +} +function contains_plus(s) { + return is_null(s) + ? false + : is_string(head(s)) && head(s) === "+" + ? true + : contains_plus(tail(s)); } function make_sum(a1, a2) { return number_equal(a1, 0) @@ -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) && - ! (is_null(member("+", x)); + return is_pair(x) && 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) @@ -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) && is_null(member("+", x); + return is_pair(x) && ! 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)