灯下 登录
计算机科学 / SICP / 2.3.2 Example: Symbolic Differentiation

Exercise 2.58 · 习题

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.

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.

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?

练习 2.58:假设我们希望修改微分程序,使其能处理普通数学记法,即 + 和 * 是中缀运算符而非前缀运算符。由于微分程序是基于抽象数据定义的,我们可以仅通过改变定义微分器所操作的代数表达式表示的谓词、选择函数和构造函数,来使程序适应不同的表达式表示。

说明如何使程序能够对以中缀形式给出的代数表达式求导,例如 (x + (3 * (x + (y + 2))))。为简化任务,假设 + 和 * 总是接受两个参数,且表达式完全带括号。

若允许使用标准代数记法,例如 (x + 3 * (x + y + 2))——省略不必要的括号,并假设乘法先于加法——问题就会变得相当困难。你能为这种记法设计合适的谓词、选择函数和构造函数,使我们的求导程序仍然可用吗?