Exercise 2.56

Read Exercise 2.56 ~ Solution


(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))))
        ((exponentiation? exp)
         (let ((base (base exp))
               (exponent (exponent exp)))
           (make-product exponent
                         (make-product (make-exponentiation base 
                                                            (make-sum exponent -1))
                                       (deriv base var))))) 
        (else
         (error "unknown expression type -- DERIV" exp))))

(define (make-exponentiation base exponent)
  (cond ((=number? base 0) 0)
        ((=number? exponent 0) 1)
        ((=number? exponent 1) base)
        (else (list '** base exponent))))

(define base cadr)
(define exponent caddr)

(define (exponentiation? x)
  (and (pair? x) 
       (eq? (car x) '**)))

Leave a comment