忘记密码   免费注册 查看新帖 | 论坛精华区

ChinaUnix.net

  平台 论坛 博客 认证专区 大话IT 视频 徽章 文库 沙龙 自测 下载 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
1234下一页
最近访问板块 发新帖
查看: 10622 | 回复: 33

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

论坛徽章:
0
发表于 2008-09-18 12:13 |显示全部楼层
此帖为 SICP 章节 2.1 的参考答案。为保持可读性,请勿直接在本帖讨论。讨论请另开新帖。




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

论坛徽章:
0
发表于 2008-09-18 12:18 |显示全部楼层

Exercise 2.1.

Exercise 2.1.  Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the rational number is positive, both the numerator and denominator are positive, and if the rational number is negative, only the numerator is negative.

论坛徽章:
0
发表于 2008-09-18 12:19 |显示全部楼层

Exercise 2.1. 参考答案


  1. (define (sign n)
  2.   (if (< n 0) -1 1))
  3.          
  4. (define (make-rate n m)
  5.   (let ((d (gcd n m))
  6.         (s (if (= (sign n) (sign m)) 1 -1))
  7.         (n (if (< n 0) (- n) n))
  8.         (m (if (< m 0) (- m) m)))
  9.     (if (= 0 m) (error "denomenator can't be zero")
  10.            (cons (* s (/ n d)) (/ m d)))))
复制代码

论坛徽章:
0
发表于 2008-09-18 12:20 |显示全部楼层

Exercise 2.2.

Exercise 2.2.  Consider the problem of representing line segments in a plane. Each segment is represented as a pair of points: a starting point and an ending point. Define a constructor make-segment and selectors start-segment and end-segment that define the representation of segments in terms of points. Furthermore, a point can be represented as a pair of numbers: the x coordinate and the y coordinate. Accordingly, specify a constructor make-point and selectors x-point and y-point that define this representation. Finally, using your selectors and constructors, define a procedure midpoint-segment that takes a line segment as argument and returns its midpoint (the point whose coordinates are the average of the coordinates of the endpoints). To try your procedures, you'll need a way to print points:

(define (print-point p)
  (newline)
  (display "(")
  (display (x-point p))
  (display ",")
  (display (y-point p))
  (display ")"))

论坛徽章:
0
发表于 2008-09-18 12:21 |显示全部楼层

Exercise 2.2. 参考答案


  1. (define (make-point x y)
  2.   (cons x y))

  3. (define (x-point p)
  4.   (car p))

  5. (define (y-point p)
  6.   (cdr p))

  7. (define (make-segment p1 p2)
  8.   (cons p1 p2))

  9. (define (start-segment s)
  10.   (car s))

  11. (define (end-segment s)
  12.   (cdr s))

  13. (define (midpoint-segment s)
  14.   (let* ((p1 (start-segment s))
  15.         (p2 (end-segment s))
  16.         (x (/ (+ (x-point p1) (x-point p2)) 2))
  17.         (y (/ (+ (y-point p1) (x-point p2)) 2)))
  18.     (make-point x y)))

  19.    
  20. (define (print-point p)
  21.   (display "(")
  22.   (display (x-point p))
  23.   (display ",")
  24.   (display (y-point p))
  25.   (display ")"))

  26. (define (print-segment s)
  27.   (print-point (start-segment s))
  28.   (display " ")
  29.   (print-point (end-segment s)))
复制代码


简单测试 segment 的代码


  1. (let* ((o (make-point 0 0))
  2.        (p (make-point 2 2))
  3.        (s (make-segment o p)))
  4.   (newline)
  5.   (print-segment s)
  6.   (newline)
  7.   (print-point (midpoint-segment s))
  8.   (newline))
复制代码


输出


  1. (0,0) (2,2)
  2. (1,1)
复制代码

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

论坛徽章:
0
发表于 2008-09-18 12:28 |显示全部楼层

Exercise 2.3.

Exercise 2.3.  Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation?

论坛徽章:
0
发表于 2008-09-18 12:40 |显示全部楼层

Exercise 2.3. 参考答案

周长的面积


  1. (define (perimeter-of-rect r)
  2.   (let ((w (width-of-rect r))
  3.         (h (height-of-rect r)))
  4.     (* 2 (+ w h))))

  5. (define (aera-of-rect r)
  6.   (let ((w (width-of-rect r))
  7.         (h (height-of-rect r)))
  8.     (* w h)))
复制代码


矩形的建构、选择,方案一:

这个实现有点作弊。如果矩形的中心在原点,并且四边分别与 x, y 轴平行。则给定长宽后,矩形可以唯一确定。其中长为平行于 x 轴的边长。

我们把上面定义的矩形称为标准矩形,则平面上任意一个矩形都可以看成某个标准矩形旋转一个角度,并平移一下得到。

下面的建构函数的参数包含长、宽、角度、位移。前两个决定了矩形的形状,后两个决定了矩形的位置。

我说这个方法有点作弊,是因为后两个参数与周长,面积无关,而本题关心的却是周长和面积。


  1. (define (make-rect w h angle offset)
  2.   (cons (cons w h) (cons angle offset)))

  3. (define (width-of-rect r)
  4.   (caar r))

  5. (define (height-of-rect r)
  6.   (cdar r))
复制代码


简单的周长、面积测试代码

  1. (let ((r (make-rect 2 3 0 0)))
  2.   (newline)
  3.   (display (perimeter-of-rect r))
  4.   (newline)
  5.   (display (aera-of-rect r))
  6.   (newline))
复制代码


输出

  1. 6
  2. 10
复制代码


矩形的建构、选择,方案二:

这个建构很直接,按顺时针或逆时针顺着矩形走,选连续的三个顶点。  注意,下面的 p2 点一定要是中间的点。


  1. (define (make-rect p1 p2 p3)
  2.   (list p1 p2 p3))

  3. (define (width-of-rect r)
  4.   (let ((s (cons (car r) (cadr r))))
  5.     (length-of-segment s)))

  6. (define (height-of-rect r)
  7.   (let ((s (cons (cadr r) (caddr r))))
  8.     (length-of-segment s)))

  9. (define (length-of-segment s)
  10.   (let* ((start (start-segment s))
  11.          (end (end-segment s))
  12.          (dx (- (x-point start) (x-point end)))
  13.          (dy (- (y-point start) (y-point end))))
  14.     (sqrt (+ (square dx) (square dy)))))
复制代码


矩形周长、面积的简单测试代码

  1. (let* ((p1 (make-point 0 1))
  2.        (p2 (make-point 0 0))
  3.        (p3 (make-point 2 0))
  4.        (r (make-rect p1 p2 p3)))
  5.   (newline)
  6.   (display (perimeter-of-rect r))
  7.   (newline)
  8.   (display (aera-of-rect r))
  9.   (newline))
复制代码


输出


  1. 6
  2. 2
复制代码

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

论坛徽章:
0
发表于 2008-09-18 12:48 |显示全部楼层

Exercise 2.4.

Exercise 2.4.  Here is an alternative procedural representation of pairs. For this representation, verify that (car (cons x y)) yields x for any objects x and y.

(define (cons x y)
  (lambda (m) (m x y)))

(define (car z)
  (z (lambda (p q) p)))

What is the corresponding definition of cdr? (Hint: To verify that this works, make use of the substitution model of section 1.1.5.)

论坛徽章:
0
发表于 2008-09-18 12:49 |显示全部楼层

Exercise 2.4. 参考答案


  1. (define (Cons x y)
  2.   (lambda (m) (m x y)))

  3. (define (Car z)
  4.   (z (lambda (p q) p)))

  5. (define (Cdr z)
  6.   (z (lambda (p q) q)))
复制代码

论坛徽章:
0
发表于 2008-09-18 12:51 |显示全部楼层

Exercise 2.5.

Exercise 2.5.  Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2^a 3^b. Give the corresponding definitions of the procedures cons, car, and cdr.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:1101082001
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP