灯下 登录
计算机科学 / SICP / 2.2.4 Example: A Picture Language

Exercise 2.46 · 习题

Exercise 2.46: A two-dimensional vector v
running from the origin to a point can be represented as a pair consisting of
an x-coordinate and a y-coordinate. Implement a data abstraction for
vectors by giving a constructor make-vect and corresponding selectors
xcor-vect and ycor-vect. In terms of your selectors and
constructor, implement procedures add-vect, sub-vect, and
scale-vect that perform the operations vector addition, vector
subtraction, and multiplying a vector by a scalar:
(

x
1

,

y
1

)
+
(

x
2

,

y
2

)

=

(

x
1

+

x
2

,

y
1

+

y
2

)
,

(

x
1

,

y
1

)

(

x
2

,

y
2

)

=

(

x
1

x
2

,

y
1

y
2

)
,

s

(
x
,
y
)

=

(

s

x

,

s

y

)

.

练习 2.46:从原点出发指向某点的二维向量 v 可以表示为由 x 坐标和 y 坐标组成的序对。通过给出构造函数 make-vect 和对应的选择函数 xcor-vect、ycor-vect,实现向量的数据抽象。基于你的选择函数和构造函数,实现过程 add-vect、sub-vect 和 scale-vect,分别执行向量加法、向量减法和向量与标量的乘法:

(x_1, y_1) + (x_2, y_2) = (x_1 + x_2, y_1 + y_2),

(x_1, y_1) - (x_2, y_2) = (x_1 - x_2, y_1 - y_2),

s · (x, y) = (sx, sy)。