Fork me on GitHub

递归计算过程recursive process):这种类型的计算过程由一个推迟执行的运算链条刻画。

迭代计算过程iterative process):其状态可以用固定数目的状态变量描述。与此同时,又存在一套固定规则,描述了计算过程从一个状态到下一个状态转换时,变量的更新方式,还有一个结束检测,描述计算过程应该终止的条件。

范例:换零钱

 1 (define (count-change amount)
 2     (cc amount 5))
 3 (define (cc amount kinds-of-coins)
 4     (cond ((= amount 0) 1)
 5            ((or (< amount 0) (= kinds-of-coins 0)) 0)
 6            (else (+ (cc amount
 7                         (- kinds-of-coins 1))
 8                     (cc (- amount
 9                            (first-denomination kinds-of-coins))
10                         kinds-of-coins)))))
11 (define (first-denomination kinds-of-coins)
12     (cond ((= kinds-of-coins 1) 1)
13            (= kinds-of-coins 2) 5)
14            (= kinds-of-coins 3) 10)
15            (= kinds-of-coins 4) 25)
16            (= kinds-of-coins 5) 50)))

部分习题:

exercise 1.9

 1 (+ 4 5)
 2 (inc (+ 3 5))
 3 (inc (inc (+ 2 5)))
 4 (inc (inc (inc (+ 1 5))))
 5 (inc (inc (inc (inc (+ 0 5)))))
 6 (inc (inc (inc (inc 5))))
 7 (inc (inc (inc 6)))
 8 (inc (inc 7))
 9 (inc 8)
10 9
11 
12 (+ 4 5)
13 (+ 3 6)
14 (+ 2 7)
15 (+ 1 8)
16 (+ 0 9)
17 9

exercise 1.11

 1 ;recursive process
 2 (define (f n)
 3     (if (< n 3)
 4     n
 5     (+ (f (- n 1))
 6        (* 2
 7           (f (- n 2)))
 8        (* 3
 9           (f (- n 3))))))
10 
11 ;iterative process
12 (define (f n)
13     (define (f-iter a b c n)
14         (if (= n 0)
15         a
16         (f-iter b
17                 c
18                 (+ (* 3 a)
19                    (* 2 b)
20                    c)
21                 (- n 1)))
22     (f-iter 0 1 2 n))

 exercise 1.12

需要输入两个参数,代表第几行第几个元素,索引从0开始,则第一个元素表示为 (pascal 0 0)。

1 (define (pascal row col)
2     (cond ((= col 0) 1)
3           ((= col row) 1)
4           ((> col row) (display "col can't larger then row"))    
5           (else (+ (pascal (- row 1)
6                            (- col 1))
7                    (pascal (- row 1)
8                            col)))))

 exercise 1.15

a) p将被使用5次,代换模型如下。

1 (sine 12.15)
2 (p (sine 4.05))
3 (p (p (sine 1.35)))
4 (p (p (p (sine 0.45))))
5 (p (p (p (p (sine 0.15)))))
6 (p (p (p (p (p (sine 0.05))))))

 exercise 1.16

 1 (define (fast-expt b n)
 2     (define (fast-expt-iter b n a)
 3          (cond ((= n 0) a)
 4                ((even? n) (fast-expt-iter (square b)
 5                                           (/ n 2)
 6                                           a))
 7                (else (fast-expt-iter b
 8                                      (- n 1)
 9                                      (* a b)))))
10     (fast-expt-iter b n 1))

 exercise 1.17

1 (define (double x)
2     (+ x x))
3 (define (halve x)
4     (/ x 2))
5 (define (fast-multi a b)
6     (cond ((= b 0) 0)
7           ((even? b) (fast-multi (double a) (halve b)))
8           (else (+ a (fast-multi a (- b 1))))))

 exercise 1.18

 1 (define (double x)
 2     (+ x x))
 3 (define (halve x)
 4     (/ x 2))
 5 (define (fast-multi a b)
 6     (define (fast-multi-iter a b product)
 7         (cond ((= b 0) product)
 8               ((even? b) (fast-multi-iter (double a) (halve b) product))
 9               (else (fast-multi-iter a (- b 1) (+ a product)))))
10     (fast-multi-iter a b 0))

 exercise 1.19

 1 (define (fib n)
 2     (fib-iter 1 0 0 1 n))
 3 (define (fib-iter a b p q count)
 4     (cond ((= count 0) b)
 5           ((even? count) (fib-iter a 
 6                                    b
 7                                    (+ (square p) (square q))
 8                                    (+ (* 2 p q) (square q))
 9                                    (/ count 2)))
10           (else (fib-iter (+ (* b q) (* a q) (* a p))
11                           (+ (* b p) (* a q))
12                           p
13                           q
14                           (- count 1)))))

  exercise 1.20

 1 ;normal order evaluate
 2 (gcd 206 40)
 3 (if (= 40 0)
 4     206
 5     (gcd 40 (remainder 206 40)))
 6 (gcd 40 (remainder 206 40))
 7 (if (= (remainder 206 40) 0)
 8     40
 9     (gcd (remainder 206 40) (remainder 40 (remainder 206 40))))
10 ......
11 
12 
13 ;application order evaluate
14 (gcd 206 40)
15 (gcd 40 6)
16 (gcd 6 4)
17 (gcd 4 2)
18 (gcd 2 0)
19 2

 

 

posted on 2012-10-31 14:13  sungoshawk  阅读(242)  评论(0编辑  收藏  举报