2.57 Exercise 2.57
It is enough to change augend (and multiplicand) to return the sum
(and product) of the terms after the addend (or multiplier). However,
we cannot always append a + or * to the front of the rest of the
terms, or we will end up with things like (+ x) when there is only one
term left. Therefore, we must only return the last term (the behavior we
already have) if there is one term left, and otherwise return a new sum (or
product). We do not have to change deriv at all. The implementations are
below.
(define (augend s) | (if (null? (cdddr s)) | (caddr s) | (append (list '+) (cddr s)))) |
|
(define (multiplicand p) | (if (null? (cdddr p)) | (caddr p) | (append (list '*) (cddr p)))) |
|
We can verify that this works by taking the book’s cue:
> (equal? | (deriv '(* (* x y) (+ x 3)) 'x) | (deriv '(* x y (+ x 3)) 'x)) |
|
#t |