2.46
练习 2.46 从原点出发的一个二维向量v可以用一个由x坐标和y坐标构成的序对表示。请为这样的向量实现一个数据抽象:给出一个构造函数make-vect,以及对应的选择函数xcor-vect和ycor-vect。借助于你给出的构造函数和选择函数,实现过程add-vect、sub-vect和scale-vect,它们能完成向量加法、向量减法和向量的伸缩。
(define make-vect cons)
(define xcor-vect car)
(define ycor-vect cdr)
(define v (make-vect 1 2))
v
x(1 . 2)
(xcor-vect v)
1
(ycor-vect v)
2
(define (add-vect v1 v2)
(make-vect
(+ (xcor-vect v1) (xcor-vect v2))
(+ (ycor-vect v1) (ycor-vect v2))
)
)
(add-vect v v)
(2 . 4)
(define (sub-vect v1 v2)
(make-vect
(- (xcor-vect v1) (xcor-vect v2))
(- (ycor-vect v1) (ycor-vect v2))
)
)
(sub-vect v v)
(0 . 0)
(define (scale-vect s v)
(make-vect
(* s (xcor-vect v))
(* s (ycor-vect v))
)
)
(scale-vect 5 v)
(5 . 10)