免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
11 [报告]
发表于 2008-09-24 12:37 |只看该作者

  1. (define (square-list items)
  2.   (if (null? items)
  3.       '()
  4.       (cons (square (car items))
  5.             (square-list (cdr items)))))
复制代码


  1. (define (square-list items)
  2.   (map square items))
复制代码

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

Exercise 2.22.

Exercise 2.22.  Louis Reasoner tries to rewrite the first square-list procedure of exercise 2.21 so that it evolves an iterative process:

(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        (iter (cdr things)
              (cons (square (car things))
                    answer))))
  (iter items nil))

Unfortunately, defining square-list this way produces the answer list in the reverse order of the one desired. Why?

Louis then tries to fix his bug by interchanging the arguments to cons:

(define (square-list items)
  (define (iter things answer)
    (if (null? things)
        answer
        (iter (cdr things)
              (cons answer
                    (square (car things))))))
  (iter items nil))

This doesn't work either. Explain.

论坛徽章:
0
13 [报告]
发表于 2008-09-24 12:49 |只看该作者

Exercise 2.22. 答案

(1) lisp 的 list 有点象 stack,想象一下如何将一个 stack 中的元素弄出来,处理后放到另一个 stack 里就清楚了。一个元素被处理后,如果马上入表,则次序会颠倒;如果要延迟入表,则必需保留信息,这本质上就是递归。

(2) 这个做法试图从表尾插入元素,是行不通的:

注意如下的一些运算规律示意,不是真实代码:
  • (cons x y) = x.y
  • (cons  x (y)) = (x y)
  • (cons (x) y)= (x).y

论坛徽章:
0
14 [报告]
发表于 2008-09-24 12:51 |只看该作者

Exercise 2.23.

Exercise 2.23.  The procedure for-each is similar to map. It takes as arguments a procedure and a list of elements. However, rather than forming a list of the results, for-each just applies the procedure to each of the elements in turn, from left to right. The values returned by applying the procedure to the elements are not used at all -- for-each is used with procedures that perform an action, such as printing. For example,

(for-each (lambda (x) (newline) (display x))
          (list 57 321 88))
57
321
88

The value returned by the call to for-each (not illustrated above) can be something arbitrary, such as true. Give an implementation of for-each.

论坛徽章:
0
15 [报告]
发表于 2008-09-24 12:52 |只看该作者

Exercise 2.23. 答案


  1. (define (For-each f x)
  2.   (cond ((not (null? x)) (f (car x)) (For-each f (cdr x)))))
复制代码

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

Exercise 2.24

Exercise 2.24.  Suppose we evaluate the expression (list 1 (list 2 (list 3 4))). Give the result printed by the interpreter, the corresponding box-and-pointer structure, and the interpretation of this as a tree (as in figure 2.6).

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

Exercise 2.24. 答案

<wait>

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

Exercise 2.25

Exercise 2.25.  Give combinations of cars and cdrs that will pick 7 from each of the following lists:

(1 3 (5 7) 9)

((7))

(1 (2 (3 (4 (5 (6 7))))))

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

Exercise 2.25 答案


  1. (car (cdaddr '(1 3 (5 7) 9)))

  2. (caar '((7)))

  3. (cadadr (cadadr (cadadr '(1 (2 (3 (4 (5 (6 7)))))))))
复制代码

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

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

Exercise 2.26.

Exercise 2.26.  Suppose we define x and y to be two lists:

(define x (list 1 2 3))
(define y (list 4 5 6))

What result is printed by the interpreter in response to evaluating each of the following expressions:

(append x y)

(cons x y)

(list x y)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP