SICP Exercise 2.73
Question
2.3.2 described a program that performs symbolic differentiation:
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp)
(if (same-variable? exp var) 1 0))
((sum? exp)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product
(multiplier exp)
(deriv (multiplicand exp) var))
(make-product
(deriv (multiplier exp) var)
(multiplicand exp))))
⟨more rules can be added here⟩
(else (error "unknown expression type:
DERIV" exp))))
We can regard this program as performing a dispatch on the type of the
expression to be differentiated. In this situation the “type tag” of the datum
is the algebraic operator symbol (such as +
) and the operation being performed
is deriv
. We can transform this program into data-directed style by rewriting
the basic derivative procedure as
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp)
(if (same-variable? exp var)
1
0))
(else ((get 'deriv (operator exp))
(operands exp)
var))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
-
Explain what was done above. Why can’t we assimilate the predicates
number?
andvariable?
into the data-directed dispatch? -
Write the procedures for derivatives of sums and products, and the auxiliary code required to install them in the table used by the program above.
-
Choose any additional differentiation rule that you like, such as the one for exponents (Exercise 2.56), and install it in this data-directed system.
-
In this simple algebraic manipulator the type of an expression is the algebraic operator that binds it together. Suppose, however, we indexed the procedures in the opposite way, so that the dispatch line in
deriv
looked like((get (operator exp) 'deriv) (operands exp) var)
What corresponding changes to the derivative system are required?
Answer
-
The reason why
number?
andvariable?
cannot become part of the table of operations used for a data-directed approach is that we are using the operand as the Type in the table, with'deriv
being the Operation. However, if we have a single number or variable, there is no operator prefix, and the selectorsoperator
andoperand
will not work as they are supposed to. -
This is my installation package:
(define (install-sum-product-deriv-package) ; internal procedures (define (augend s) (car s)) (define (addend s) (cadr s)) (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 (deriv-sum exp var) (make-sum (deriv (addend exp) var) (deriv (augend exp) var))) (define (multiplicand s) (car s)) (define (multiplier s) (cadr s)) (define (make-product m1 m2) (cond ((or (=number? m1 0) (=number? m2 0)) 0) ((=number? m1 1) m2) ((=number? m2 1) m1) ((and (number? m1) (number? m2)) (* m1 m2)) (else (list '* m1 m2)))) (define (deriv-product exp var) (make-sum (make-product (multiplier exp) (deriv (multiplicand exp) var)) (make-product (deriv (multiplier exp) var) (multiplicand exp)))) ; interface to the rest of the system (put 'deriv '+ deriv-sum) (put 'deriv '* deriv-product) 'done)
-
Similar to the last one:
(define (install-exponentiation-deriv-package) ; internal procedures (define (base s) (car s)) (define (exponent s) (cadr s)) (define (make-exponentiation r1 r2) (cond ((=number? r2 0) 1) ((=number? r2 1) r1) ((and (number? r1) (number? r2)) (expt r1 r2)) (else (list '** r1 r2)))) (define (deriv-exp exp var) (make-product (make-product (exponent exp) (make-exponentiation (base exp) (- (exponent exp) 1))) (deriv (base exp) var))) ; interface to the rest of the system (put 'deriv '** deriv-exp) 'done)
-
Only a very simple change is required, namely, the order of parameters in the
put
statements within the installation package. Rather than(put 'deriv '** deriv-exp)
we can simply use
(put '** 'deriv deriv-exp)