免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: win_hate
打印 上一主题 下一主题

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

论坛徽章:
0
21 [报告]
发表于 2008-09-26 12:41 |只看该作者

Exercise 2.26. 答案

  • 先看 (cons x y)

    由于 (cons x '(Y)) = ( x Y),所以


    1. (cons x y) = (cons '(1 2 3) '(4 5 6)) = ( (1 2 3) 4 5 6)
    复制代码

  • 再看 (append x y)

    设 x = (x1 x2 ... xn)。 由于 (append '(x X) '(Y) ) = (cons x (append '(X) '(Y))),所以
    (append '(x1 x2 ... xn) '(Y)) = (cons (x1 (cons x2 (... (cons xn '(Y))...)。故


    1. (append  x y)= (append '(1 2 3) '(4 5 6))= (1 2 3 4 5 6)
    复制代码

  • 最后看 (list x y)

    由于 (list X1 X2 ... Xn) = (cons X1 (cons X2 (cons ... (cons Xn nil))...), 所以


    1. (list '(1 2 3) '(4 5 6)) = ( (1 2 3) (4 5 6) )
    复制代码

论坛徽章:
0
22 [报告]
发表于 2008-09-28 22:40 |只看该作者

Exercise 2.27.

Exercise 2.27.  Modify your reverse procedure of exercise 2.18 to produce a deep-reverse procedure that takes a list as argument and returns as its value the list with its elements reversed and with all sublists deep-reversed as well. For example,

(define x (list (list 1 2) (list 3 4)))

x
((1 2) (3 4))

(reverse x)
((3 4) (1 2))

(deep-reverse x)
((4 3) (2 1))

论坛徽章:
0
23 [报告]
发表于 2008-09-28 22:41 |只看该作者

Exercise 2.27. 答案


  1. (define (deep-reverse x)
  2.   (if (list? x) (reverse (map deep-reverse x))
  3.       x))
复制代码

论坛徽章:
0
24 [报告]
发表于 2008-09-28 22:42 |只看该作者

Exercise 2.28.

Exercise 2.28.  Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-right order. For example,

(define x (list (list 1 2) (list 3 4)))

(fringe x)
(1 2 3 4)

(fringe (list x x))
(1 2 3 4 1 2 3 4)

论坛徽章:
0
25 [报告]
发表于 2008-09-28 22:43 |只看该作者

Exercise 2.28. 答案


  1. (define (fringe x)
  2.   (cond ((null? x) x)
  3.         ((list? x) (append (fringe (car x)) (fringe (cdr x))))
  4.         (else (list x))))
复制代码

论坛徽章:
0
26 [报告]
发表于 2008-09-28 22:45 |只看该作者

Exercise 2.29.

Exercise 2.29.  A binary mobile consists of two branches, a left branch and a right branch. Each branch is a rod of a certain length, from which hangs either a weight or another binary mobile. We can represent a binary mobile using compound data by constructing it from two branches (for example, using list):

(define (make-mobile left right)
  (list left right))

A branch is constructed from a length (which must be a number) together with a structure, which may be either a number (representing a simple weight) or another mobile:

(define (make-branch length structure)
  (list length structure))

a.  Write the corresponding selectors left-branch and right-branch, which return the branches of a mobile, and branch-length and branch-structure, which return the components of a branch.

b.  Using your selectors, define a procedure total-weight that returns the total weight of a mobile.

c.  A mobile is said to be balanced if the torque applied by its top-left branch is equal to that applied by its top-right branch (that is, if the length of the left rod multiplied by the weight hanging from that rod is equal to the corresponding product for the right side) and if each of the submobiles hanging off its branches is balanced. Design a predicate that tests whether a binary mobile is balanced.

d.  Suppose we change the representation of mobiles so that the constructors are

(define (make-mobile left right)
  (cons left right))
(define (make-branch length structure)
  (cons length structure))

How much do you need to change your programs to convert to the new representation?

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

论坛徽章:
0
27 [报告]
发表于 2008-09-28 23:05 |只看该作者

Exercise 2.29. 答案

  • a.


    1. (define (left-branch x)
    2.   (car x))

    3. (define (right-branch x)
    4.   (cadr x))

    5. (define (branch-length x)
    6.   (car x))

    7. (define (branch-structure x)
    8.   (cadr x))
    复制代码

  • b. 本来应该写两个函数,判断对象类型为 structure 还是 branch。但注意到 structure 必定是两个 branch,且 branch 不可以直接连 branch。所以只要在写代码的时候注意一下,就可以避免判断。


    1. (define  (total-weight x)
    2.   (if (not (pair? x)) x
    3.       (+ (total-weight (branch-structure (left-branch x)))
    4.          (total-weight (branch-structure (right-branch x))))))
    复制代码

  • c. 利用 total-weight 获取左右分支的结构重量,用 branch-length 获取左右分支的长度,就可以判断当前点是否平衡。递归使用这个规则,就可以判断。这里应该把效率也考虑进去,因为 total-weight 是递归计算的,如果在每个分叉上调用 total-weight,则进行了太多的重复计算。所以我把 total-weight 改进为 wwbc,全称为 weight-with-balance-check。在某个分叉上调用 wwbc,如果不平衡,则返回 false,如果平衡,则返回重量。


    1. (define (wwbc x)
    2.   (if (not (pair? x)) x
    3.       (let* ((lb (left-branch x)) (rb (right-branch x))
    4.              (wl (wwbc (branch-structure lb)))
    5.              (wr (if (eq? wl #f) #f (wwbc (branch-structure rb)))))
    6.         (cond ((eq? #f wl) #f)
    7.               ((= (* wl (branch-length lb))
    8.                   (* wr (branch-length rb))) (+ wl wr))
    9.               (else #f)))))

    10. (define (balance? x)
    11.   (not (eq? (wwbc x) #f)))
    复制代码


    简单的测试代码:

    1. (balance? '((2 ((2 1) (1 2)))
    2.                (3 ((2 1) (2 1)))))

    3. (balance? '((4 4) (2 ((5 3) (3 5)))))
    复制代码

  • d. 只需要把选择函数略为改动即可。


[ 本帖最后由 win_hate 于 2008-9-29 15:59 编辑 ]

论坛徽章:
0
28 [报告]
发表于 2008-09-29 15:59 |只看该作者

Exercise 2.30

Exercise 2.30.  Define a procedure square-tree analogous to the square-list procedure of exercise 2.21. That is, square-list should behave as follows:

(square-tree
(list 1
       (list 2 (list 3 4) 5)
       (list 6 7)))
(1 (4 (9 16) 25) (36 49))

Define square-tree both directly (i.e., without using any higher-order procedures) and also by using map and recursion.

论坛徽章:
0
29 [报告]
发表于 2008-09-29 16:02 |只看该作者

Exercise 2.30. 答案

不使用高阶函数


  1. (define (Square-tree t)
  2.     (cond ((null? t) '())
  3.           ((pair? t) (cons (Square-tree (car t))
  4.                            (Square-tree (cdr t))))
  5.           (else (* t t))))
复制代码


使用高阶函数:


  1. (define (square-tree t)
  2.   (if (pair? t) (map square-tree t)
  3.       (* t t)))
复制代码

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

论坛徽章:
0
30 [报告]
发表于 2008-09-29 16:06 |只看该作者

Exercise 2.31.

Exercise 2.31.  Abstract your answer to exercise 2.30 to produce a procedure tree-map with the property that square-tree could be defined as

(define (square-tree tree) (tree-map square tree))
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP