Section 2.3.2 descrbed 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)))
(make-product (multiplicand exp)
(deriv (multiplier 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 the program
into data-directed style by rewriting the basic derivative procedure
as
(define (deriv exp var)
(cons ((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 (operans exp) (cdr exp))
number?
and variable?
into the data-directed dispatch?In this simple algebraic manipulator the type of an expression is
the algrebraic 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?