1 ;; 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:
3 (define (make-point x y)
10 (define (make-segment start end)
12 (define (start-segment seg)
14 (define (end-segment seg)
16 (define (midpoint-segment seg)
19 (let ((x1 (x-point (start-segment seg)))
20 (x2 (x-point (end-segment seg)))
21 (y1 (y-point (start-segment seg)))
22 (y2 (y-point (end-segment seg))))
23 (make-point (average x1 x2)
26 (define (print-point p)
34 (define x1y2 (make-point 1 2))
35 (define x-4y-3 (make-point -4 -3))
36 (define x1y2tox-4y-3 (make-segment x1y2 x-4y-3))
37 (print-point (midpoint-segment x1y2tox-4y-3))
38 (display "=(-3/2,-1/2)")