免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 12462 | 回复: 42
打印 上一主题 下一主题

SICP 习题参考答案 1.2  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-07 09:49 |显示全部楼层 |倒序浏览

  1. 此帖为 SICP 章节 1.2 的参考答案。为保持可读性,请勿直接在本帖讨论。讨论请另开新帖。
复制代码




[ 本帖最后由 win_hate 于 2008-9-13 16:56 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-09-07 09:49 |显示全部楼层

Exercise 1.9.

Exercise 1.9.  Each of the following two procedures defines a method for adding two positive integers in terms of the procedures inc, which increments its argument by 1, and dec, which decrements its argument by 1.

(define (+ a b)
  (if (= a 0)
      b
      (inc (+ (dec a) b))))

(define (+ a b)
  (if (= a 0)
      b
      (+ (dec a) (inc b))))

Using the substitution model, illustrate the process generated by each procedure in evaluating (+ 4 5). Are these processes iterative or recursive?

论坛徽章:
0
3 [报告]
发表于 2008-09-07 09:50 |显示全部楼层

Exercise 1.9. 答案

[解]  第一个是 recursive 的,而第二个是 iterative 的。

论坛徽章:
0
4 [报告]
发表于 2008-09-07 09:51 |显示全部楼层

Exercise 1.10

Exercise 1.10.  The following procedure computes a mathematical function called Ackermann's function.

(define (A x y)
  (cond ((= y 0) 0)
        ((= x 0) (* 2 y))
        ((= y 1) 2)
        (else (A (- x 1)
                 (A x (- y 1))))))

What are the values of the following expressions?

(A 1 10)

(A 2 4)

(A 3 3)

Consider the following procedures, where A is the procedure defined above:

(define (f n) (A 0 n))

(define (g n) (A 1 n))

(define (h n) (A 2 n))

(define (k n) (* 5 n n))

Give concise mathematical definitions for the functions computed by the procedures f, g, and h for positive integer values of n. For example, (k n) computes 5n^2.

[ 本帖最后由 win_hate 于 2008-9-7 09:54 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-09-07 09:53 |显示全部楼层

Exercise 1.10. 答案

[解]
(A 0 n)=2n
(A 1 n)=2^n
(A 2 n) = 2^2^2...^2    // n 个 2,右结合

论坛徽章:
0
6 [报告]
发表于 2008-09-07 11:18 |显示全部楼层

Exercise 1.11.

Exercise 1.11.  A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.

论坛徽章:
0
7 [报告]
发表于 2008-09-07 11:21 |显示全部楼层

Exercise 1.11. 答案

递归代码


  1. (define (f n)
  2.   (if (< n 3) n
  3.       (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))))
复制代码


迭代代码


  1. (define (g x y z n)
  2.   (if (= 0 n) x
  3.       (g y z (+ (* 3 x) (* 2 y) z) (- n 1))))
复制代码


迭代代码这样使用:


  1. (g 0 1 2 n)
复制代码

[ 本帖最后由 win_hate 于 2008-9-7 11:22 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2008-09-07 11:24 |显示全部楼层

Exercise 1.12.

Exercise 1.12.  The following pattern of numbers is called Pascal's triangle.



The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a procedure that computes elements of Pascal's triangle by means of a recursive process.

[ 本帖最后由 win_hate 于 2008-9-8 19:07 编辑 ]

论坛徽章:
0
9 [报告]
发表于 2008-09-07 11:32 |显示全部楼层

Exercise 1.12. 答案


  1. (define (binomial n m)
  2.   (if (or (= m 0) (= m n))
  3.       1
  4.       (+ (binomial (- n 1) m) (binomial (- n 1) (- m 1)))))
复制代码


上面的递归虽然很直观,却是一个坏递归的例子。其空间需求以指数方式增长,并做了大量重复的工作。下面这个效率较高,但可读性就差一些。


  1. (define (binomial n m)
  2.   (define (f n m r)
  3.     (if (= m 0) r (f (- n 1) (- m 1) (/ (* r n) m))))

  4.   (cond ((< m 0) 0)
  5.         ((= m 0) 1)
  6.         ((> m n) 0)
  7.         ((= n m) 1)
  8.         ((and (> n 0) (> (* 2 m) n)) (f n (- n m) 1))
  9.         (else (f n m 1))))
复制代码

[ 本帖最后由 win_hate 于 2008-9-7 12:23 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2008-09-08 19:15 |显示全部楼层

Exercise 1.12

Exercise 1.13.  Prove that Fib(n) is the closest integer to ^n/5, where  = (1 + 5)/2. Hint: Let   = (1 - 5)/2. Use induction and the definition of the Fibonacci numbers (see section 1.2.2) to prove that Fib(n) = (^n  -^n)/5.

[ 本帖最后由 win_hate 于 2008-9-8 19:21 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP