Exercise 2.2: Consider the problem of
representing line segments in a plane. Each segment is represented as a pair
of points: a starting point and an ending point. Define a constructor
make-segment and selectors start-segment and end-segment
that define the representation of segments in terms of points. Furthermore, a
point can be represented as a pair of numbers: the x coordinate and the
y coordinate. Accordingly, specify a constructor make-point and
selectors x-point and y-point that define this representation.
Finally, using your selectors and constructors, define a procedure
midpoint-segment that takes a line segment as argument and returns its
midpoint (the point whose coordinates are the average of the coordinates of the
endpoints). To try your procedures, you’ll need a way to print points:
(define (print-point p)
(newline)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))
练习 2.2:考虑在平面上表示线段的问题。每条线段表示为一对点:起点和终点。定义构造函数 make-segment 以及选择函数 start-segment 和 end-segment,用于以点来定义线段的表示。此外,一个点可以用一对数(x 坐标和 y 坐标)来表示。据此,指定构造函数 make-point 和选择函数 x-point、y-point 来定义这种表示。最后,利用你的选择函数和构造函数,定义过程 midpoint-segment,它以一条线段为参数,返回其中点(坐标为端点坐标平均值的点)。为了试验你的过程,你需要一种打印点的方式:
(define (print-point p)
(newline)
(display “(”)
(display (x-point p))
(display “,”)
(display (y-point p))
(display “)”))
(define (print-point p)
(newline)
(display "(")
(display (x-point p))
(display ",")
(display (y-point p))
(display ")"))