# 2.58

## b）如果允许标准的代数写法，例如(x+3*(x+y+2))，问题就会变得更困难许多。在这种表达式里可能不写不必要的括号，并要假定乘法应该在加法之前完成。你还能为这种表示方式设计好适当的谓词、选择函数和构造函数，使我们的求导程序仍然能工作吗？

(define (=number? v n)
(if (number? v)
(= v n)
#f
)
)

(define (error msg v)
(list msg v)
)

(define variable? symbol?)
(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2))
)
(define (make-sum a1 a2)
(cond
((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2)) (+ a1 a2))
(else (list a1 '+ a2))
)
)
(define (make-product m1 m2)
(cond
((=number? m1 1) m2)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(else (list m1 '* m2))
)
)
(define (sum? x) (and (pair? x) (eq? (cadr x) '+)))
(define (product? x) (and (pair? x) (eq? (cadr x) '*)))

(define (deriv exp var)
(cond
((number? exp) 0)
((variable? exp)
(if (same-variable? exp var) 1 0)
)
((sum? exp)
(make-sum
(deriv (augend exp) var)
)
)
((product? exp)
(make-sum
(make-product
(multiplier exp)
(deriv (multiplicand exp) var)
)
(make-product
(deriv (multiplier exp) var)
(multiplicand exp)
)
)
)
(else
(error "unknown expression type -- DERIV" exp)
)
)
)

; 期待返回 1
(deriv '(x + 3) 'x)


(define (exponentiation? exp)
(and (pair? exp) (eq? (car exp) '**))
)

; 期待返回 true
(exponentiation? '(** x 3))


(define base cadr)

; 期待返回 x
(base '(** x 3))


(define exponent caddr)

; 期待返回 3
(exponent '(** x 3))


(define (make-exponentiation m1 m2) (list '** m1 m2))

; 期待返回 (** x 3)
(make-exponentiation 'x 3)



(define (deriv exp var)
(cond
((number? exp) 0)
((variable? exp)
(if (same-variable? exp var) 1 0)
)
((sum? exp)
(make-sum
(deriv (augend exp) var)
)
)
((product? exp)
(make-sum
(make-product
(multiplier exp)
(deriv (multiplicand exp) var)
)
(make-product
(deriv (multiplier exp) var)
(multiplicand exp)
)
)
)
((exponentiation? exp)
(cond
((= (exponent exp) 0) 0)
((= (exponent exp) 1) 1)
(else
(make-product
(exponent exp)
(make-exponentiation
(base exp)
(- (exponent exp) 1)
)
)
)
)
)
(else
(error "unknown expression type -- DERIV" exp)
)
)
)

(deriv '(** x 0) 'x)

; 期待得到 3x^2 的结果
(deriv '(** x 3) 'x)


$(x+(3*(x+(y+2))))$

; 期待得到 4
(deriv '(x + (3 * (x + (y + 2)))) 'x)


### 现在修改一下和的表示：

(define (augend exp)
(if (<= (length (cddr exp)) 1)
(cddr exp)
)
)

(cond
(if (pair? augend)
(make-sum (car augend) (cdr augend))
augend
)
)
(
(or (=number? augend 0) (null? augend))
)
(
(and (number? addend) (pair? augend) (number? (car augend)))
(make-sum (+ addend (car augend)) (cdr augend))
)
(else
)
)
)

(make-sum 1 (list 2 3 4 5 6))

(augend '(+ 2 3 4 5 6))


### 再来修改一下乘积的表示：

(define (make-product m1 m2)
(cond
((null? m2) m1)
((=number? m1 0) 0)
((=number? m1 1)
(if (pair? m2)
m2
)
)
((=number? m2 0) 0)
((=number? m2 1) m1)
((and (number? m1) (number? m2)) (* m1 m2))
(
(and (number? m1) (pair? m2) (number? (car m2)))
(make-product (* m1 (car m2)) (cdr m2))
)
(else
(if (pair? m2)
(list '* m1 (make-product (car m2) (cdr m2)))
(list '* m1 m2)
)
)
)
)

(define (multiplicand exp)
(if (<= (length (cddr exp)) 1)
(cons '* (cddr exp))
)
)

(multiplicand '(* 1 2 3 4 5))

(multiplicand '(* 1 2))

(make-product 1 (list 2 3 4 5 6))

(make-sum 'x 3)

(make-product 'x (list 'y (make-sum 'x 3)))


### 再来计算一次：

; 期待得到 4
(deriv '(x + (3 * (x + (y + 2)))) 'x)


b) 如果去掉括号还要求正确工作，就更有挑战性了。不过对于这个具体的例子，居然现在的版本就能计算正确了：

; 期待得到 4
(deriv '(x + 3 * (x + y + 2)) 'x)