2.46

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

(x1,y1)+(x2,y2)=(x1+x2,y1+y2) (x_1, y_1) + (x_2, y_2) = (x_1 + x_2, y_1 + y_2)

(x1,y1)(x2,y2)=(x1x2,y1y2) (x_1, y_1) - (x_2, y_2) = (x_1 - x_2, y_1 - y_2)

s(x,y)=(sx,sy) s\cdot(x, y) = (sx, sy)


(define make-vect cons)
(define xcor-vect car)
(define ycor-vect cdr)

(define v (make-vect 1 2))
v
(xcor-vect v)
(ycor-vect v)
(define (add-vect v1 v2)
    (make-vect
        (+ (xcor-vect v1) (xcor-vect v2))
        (+ (ycor-vect v1) (ycor-vect v2))
    )
)

(add-vect v v)
(define (sub-vect v1 v2)
    (make-vect
        (- (xcor-vect v1) (xcor-vect v2))
        (- (ycor-vect v1) (ycor-vect v2))
    )
)

(sub-vect v v)
(define (scale-vect s v)
    (make-vect
        (* s (xcor-vect v))
        (* s (ycor-vect v))
    )
)

(scale-vect 5 v)

results matching ""

    No results matching ""