1 #lang racket
2
3 (define (deriv exp
var)
4 (cond ((number? exp)
0)
5 ((variable?
exp)
6 (
if (same-variable? exp
var)
1 0))
7 ((sum?
exp)
8 (make-sum (deriv (addend exp)
var)
9 (deriv (augend exp)
var)))
10 ((product?
exp)
11 (make-
sum
12 (make-
product (multiplier exp)
13 (deriv (multiplicand exp)
var))
14 (make-product (deriv (multiplier exp)
var)
15 (multiplicand exp))))
16 (
else
17 (error
"unknow expression type --DERIV" exp))))
18
19 (define (variable?
x)
20 (symbol?
x))
21
22 (define (same-variable?
v1 v2)
23 (and (variable? v1) (variable? v2) (eq?
v1 v2)))
24
25 (define (=number?
exp num)
26 (and (number? exp) (=
exp num)))
27
28 (define (**
a b)
29 (define (iter n result)
30 (cond ((=
0 b)
1)
31 ((or (< n b) (= n b)) (iter (+ n
1) (*
a result)))
32 ((>
n b) result)))
33 (iter
1 1))
34
35 (define (sum?
x)
36 (and (pair? x) (eq? (cadr x)
'+)))
37
38 (define (product?
x)
39 (and (pair? x) (eq? (cadr x)
'*)))
40
41 (define (make-
sum a1 a2)
42 (cond ((=number? a1
0) a2)
43 ((=number? a2
0) a1)
44 ((and (number? a1) (number? a2)) (+
a1 a2))
45 (
else (list a1
'+ a2))))
46
47 (define (addend s) (car s))
48 (define (augend s) (caddr s))
49
50 (define (make-
product m1 m2)
51 (cond ((or (=number? m1
0) (=number? m2
0))
0)
52 ((=number? m1
1) m2)
53 ((=number? m2
1) m1)
54 ((and (number? m1) (number? m2)) (*
m1 m2))
55 (
else (list m1
'* m2))))
56
57 (define (multiplier p) (car p))
58 (define (multiplicand p) (caddr p))
59
60 ;(deriv
'(x + (x * x)) 'x)
61 ;(deriv
'(x * (x + x)) 'x)
62
63 ;;;;;;;;;;;;;;(b)
64 ;;;;;;;;;;;;;;不会
转载于:https://www.cnblogs.com/tclan126/p/6445071.html