; ====================================================================== ; ; Structure and Interpretation of Computer Programs ; (trial answer to excercises) ; ; 计算机程序的构造和解释(习题试解) ; ; created: code17 04/18/05 ; modified: ; (保持内容完整不变前提下,可以任意转载) ; ======================================================================
;; SICP No.2.3
;; 任何一条有向线段seg(角度方向0 <= theta < pi/2)和一段距离d(d>0)可以唯一地确定二维 ;; 平面上的一个长方形(该线段构成长方形的一条边,与之平行的另一条边在该有向线段的逆时针方 ;; 向,且与其距离为d); 反之亦然,任何二维平面上的一个长方形均可唯一地对应到这样一条有向 ;; 线段和距离,因此这是一种一一对应的关系。(seg和d的取值限制确保了这种唯一性)
;; constructor (define (make-rectangle s d) (cons s d))
;; 我们可以定义两个selector,来获取长方形的长和宽 ;; (这里,“长”和“宽”不是表示数值上的大小关系,“长”为有向线段的那条边的长度 ;; “宽”为与其垂直的那条边的长度,后同) (define (length-rect x) (let ((seg (car x))) (let ((s (start-segment seg)) (e (end-segment seg))) (sqrt (+ (square (- (x-point s) (x-point e))) (square (- (y-point s) (y-point e)))))))) (define (width-rect x) (cdr x))
;; 周长 (define (perimeter-rect x) (* (+ (length-rect x) (width-rect x)) 2))
;; 面积 (define (area-rect x) (* (length-rect x) (width-rect x)))
;; 保持data-abstraction不变,我们可以用别的方法来定义长方形的内部表示 ;; 这样,任何使用长方形的外部函数均不会受到影响,无论是那些使用selector函数 ;; 的还是那些使用constructor
;; 比如我们可以定义长方形为 ;; ((length,width),(base-point,base-angle)) ;; 其中,base-point对应于原表示法中有向线段的起点(取值范围也需要对应成立) ;; base-angle对应于原有向线段的角度(取值范围也需对应成立),采用正弦值记录 ;; 这样的表示同样是一一对应关系
;; 我们依然可以兼容原始的constructor,以有向线段seg和距离d为输入 (define (make-rectangle s d) (let ((sp (start-segment s)) (ep (end-segment s))) (let ((length (sqrt (+ (square (- (x-point sp) (x-point ep))) (square (- (y-point sp) (y-point ep)))))) (width d) (base-point sp)) (let ((base-angle (/ (- (y-point ep) (y-point sp)) length))) (cons (cons length width) (cons base-point base-angle))))))
;; 则selector为 (define (length-rect x) (car (car x))) (define (width-rect x) (cdr (car x)))
;; 注意,base-point和base-angle在本题中没有用到,但这并不表示它们是不需要的, ;; 因为只有这四者同时存在,才能保证一个二维空间中的一个长方形的全部信息。
;; Test-it: ;; Welcome to MzScheme version 209, Copyright (c) 2004 PLT Scheme, Inc. ;; ;; ;; ;; ...(先输入第一种定义,此处略) ;; ;; > (define test-rect (make-rectangle (make-segment (make-point 0 0) ;; (make-point 3 4)) ;; 2)) ;; ;; ;; 看看内部表示 ;; > test-rect ;; (((0 . 0) 3 . 4) . 2) ;; ;; ;; 测试 ;; > (length-rect test-rec) ;; 5 ;; > (width-rect test-rec) ;; 2 ;; > ;; > (perimeter-rect test-rect) ;; 14 ;; > (area-rect test-rect) ;; 10 ;; ;; ;; ;; ... (输入第二种定义,外部周长/面积函数未动,此处略) ;; ;; > (define test-rect (make-rectangle (make-segment (make-point 0 0) ;; (make-point 3 4)) ;; 2)) ;; ;; ;; 看看内部表示 ;; > test-rect ;; ((5 . 2) (0 . 0) . 4/5) ;; ;; 测试 ;; > (length-rect test-rec) ;; 5 ;; > (width-rect test-rec) ;; 2 ;; > ;; > (perimeter-rect test-rect) ;; 14 ;; > (area-rect test-rect) ;; 10 ;; ;; ;; 内部表示不同,但结果是一样的,所有调用constructor和selector的外部函数均不需要 ;; ;; 任何修改。与课文中的例子类似, 第一种方案是在需要时才计算length和width,第二种 ;; ;; 方案在construt的时候就已经计算好了length和width。 因此,第一种在对数据的length ;; ;; width操作不普遍/频繁时高效, 第二种在length和width操作普遍/频繁时高效。

|