Scheme中矩形构造函数的实现

时间:2014-08-12 16:49:32

标签: scheme

我正在观看sicp 3a的讲座,他为make-rect指定了构造函数和选择器,但没有提供实现。

(define make-rect list)
(define make-vect cons) 
(define origin car) 
(define horiz cadr) 
(define vert caddr)

(define t (make-rect (make-vect 0 0) (make-vect 2 3) (make-vect 9 9)))


(origin t)
(horiz t)
(vert t)

我不确定这是否符合矩形构造函数的所有要求。

2 个答案:

答案 0 :(得分:0)

需要三个向量来描述可以旋转的矩形。但是,您的代码描述的是平行四边形而不是矩形。

无法保证
(= (+ (square (distance origin horizontal))
      (square (distance origin vertical)))
   (square (distance horizontal vertical)))

SICP 中的类似代码(用于平行四边形)是根据本书Section 2.2.4中的frames编写的。

答案 1 :(得分:-1)

可能不是,因为你可以用类似

的东西来滥用它
 (define t 
   (make-rect 
    (make-vect 'bacon '(and eggs)) 
    (make-vect 'coffee 'tea) 
    (make-vect '(I am not a point) '(and neither am I))))

为哪

 (origin t)
 (horiz t)
 (vert t)

返回

 '(bacon and eggs)
 '(coffee . tea)
 '((I am not a point) and neither am I)