【SICP练习】84 练习2.56

简介:

这道题的代码略长啊。不过我也是因此而知道Edwin上的代码居然可以复制到word上,以前还想当然的以为不能复制的,毕竟在Edwin上粘贴用的Ctrl+Y,而不是Ctrl+V。在这里我就只将书上没有的代码贴出来了。

(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) (multiiplicand exp))))
           ((exponentiation? exp)
     (let ((n (exponent exp)) (u (base exp)))
              (make-product n (make-product (make-exponentiation u (- u 1)) (deriv u var)))))
    (else
             (error "unknown expression type -- DERIV" exp))))
(define (make-exponentiation base exponent)
  (cond ((= exponent 0)
     1)
    ((= exponent 1)
     base)
    (else 
     (list '** base exponent))))
(define (exponentiation? x)
  (and (pair? x)
       (eq? (car x) '**)))
(define (base exp)
  (cadr exp))
(define (exponent exp)
  (caddr exp))
目录
相关文章