# sarabander/p2pu-sicp

data-directed and message passing styles

1 parent 8bcaf3c commit 77da674fb798e6f8b3ac8914fbfcf8e44e5d7685 committed Oct 15, 2011
Showing with 217 additions and 0 deletions.
1. +52 −0 2.4/2.73.scm
2. +125 −0 2.4/2.74.scm
3. +20 −0 2.4/2.75.scm
4. +20 −0 2.4/2.76.scm
52 2.4/2.73.scm
 @@ -0,0 +1,52 @@ + +(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. The separate differentiation rules were replaced by single dispatch. +;; It extracts the operator from expression, and looks up the row with +;; 'deriv and the operator from the table. It then applies the found +;; derivation procedure to operands, using var as differentiation variable. + +;; number? and variable? can't be assimilated because they deal with the +;; case when the expression is an atom, not a list. The get procedure +;; needs a list to work. + +;; b. + +;; For two operands. Needs generalizing. + +(define (deriv-sum operands var) + (make-sum (deriv (first operands) var) + (deriv (second operands) var))) + +(define (deriv-product operands var) + (make-sum + (make-product (first operands) + (deriv (second operands) var)) + (make-product (deriv (first operands) var) + (second operands)))) + + +(put 'deriv '+ deriv-sum) + +(put 'deriv '* deriv-product) + +;; c. + +(define (deriv-cosine operand var) + (- (make-product ('sin (first operand)) + (deriv (first operand))))) + +(put 'deriv 'cos deriv-cosine) + +;; d. We only need to change the definition of get and put.
125 2.4/2.74.scm