SICP Exercise 2.58 modified sum and product infix Part b

Exercise 2.58.  Suppose we want to modify the differentiation program so that it works with ordinary mathematical notation, in which + and * are infix rather than prefix operators. Since the differentiation program is defined in terms of abstract data, we can modify it to work with different representations of expressions solely by changing the predicates, selectors, and constructors that define the representation of the algebraic expressions on which the differentiator is to operate.

a. Show how to do this in order to differentiate algebraic expressions presented in infix form, such as (x + (3 * (x + (y + 2)))). To simplify the task, assume that + and * always take two arguments and that expressions are fully parenthesized.

b. The problem becomes substantially harder if we allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which drops unnecessary parentheses and assumes that multiplication is done before addition. Can you design appropriate predicates, selectors, and constructors for this notation such that our derivative program still works?

SOLUTION

The code and tests are here.

Allow standard algebraic notation, such as (x + 3 * (x + y + 2)), which does not use unnecessary parentheses and assumes that multiplication is done before addition.

Note: I have added a new procedure: remove-superfluous-parens that helps simplify expressions.

Let's take this example: (x * 3 + (x + y + 2) + (5 * x ** 2))
We work our way from the left to the right assuming that any expression no matter how complex it is, is a sum of other expressions. So the logic is: look for the first occurrence of the + sign. Everything to its left will be the addend and everything to its right will be the augend. 

Then we apply the sum rule of differentiation on the addend and the augend just like in the previous exercise. When we do this recursively, all the sum rules will get applied correctly regardless of how many terms are '+'ed together in the overall expression. If we do not find any + operators as we scan the list, we re-scan the list for * operators and apply product-rule. And if we do not find any * operators either, then we look for ** operators.

This order of evaluation ensures that multiplication is done before addition and exponentiation is done before multiplication. This logic also allows us to treat +, * and ** as if these operators work on only two operands.

Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions