3 days ago

my attempt to do the exercises in sicp.

## Friday, August 1, 2008

### sicp exercise 2.73

;; Exercise 2.73. Section 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))

;; a. Explain what was done above. Why can't we assimilate the predicates number? and variable? into the data-directed dispatch?

;; b. 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.

;; c. 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.

;; d. 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?

;; a.

;; because the consitions number? and variable? do not call deriv. whereas other numerical conditions call deriv.

;; b.

(define (deriv-sum operands var)

(make-sum (deriv (car operands) var)

(deriv (cadr operands) var)))

(define (deriv-product operands var)

(make-sum (make-product (cadr operands) (deriv (car operands) var))

(make-product (car operands) (deriv (cadr operands) var))))

(define (install-deriv)

(put 'deriv '+ deriv-sum)

(put 'deriv '* deriv-product)

'done)

;; c.

(define (deriv-exponentiation operands var)

(make-product (cadr operands)

(make-product (make-exponentiation (car operands)

(- (cadr operands) 1))

(deriv (car operands) var))))

(define (install-deriv)

(put 'deriv '** deriv-exponentiation)

'done)

;; d. the derivative system will change as follows. the order of operator and procedure name shall be reversed while installing the derivative system

(define (deriv exp var)

(cond ((number? exp) 0)

((variable? exp) (if (same-variable? exp var) 1 0))

(else ((get (operator exp) 'deriv) (operands exp)

var))))

(define (install-deriv)

(put '+ 'deriv deriv-sum)

(put '* 'deriv deriv-product)

'done)

Subscribe to:
Post Comments (Atom)

## No comments:

Post a Comment