Note: this wiki is now retired and will no longer be updated!

The static final versions of the pages are left as a convenience for readers. Note that meta-pages such as "discussion," "history," etc., will not work.

SICP exercise 2.48

From Drewiki
Jump to: navigation, search

Problem

A directed line segment in the plane can be represented as a pair of vectors - the vector running from the origin to the start-point of the segment, and the vector running from the origin to the end-point of the segment. Use your vector representation from exercise 2.46 to define a representation for segments with a constructor make-segment and selectors start-segment and end-segment.

Solution

Since segments are just pairs of vectors, this is a straightforward problem. Here's one possible implementation:

(define (make-vect x y)
  (cons x y))
 
(define (make-segment start end)
  (cons start end))
 
(define (start-segment segment)
  (car segment))
 
(define (end-segment segment)
  (cdr segment))

Test:

(define segment (make-segment (make-vect 2 1)
                              (make-vect 4 5)))
 
(start-segment segment)

Output:

(2 . 1)
(end-segment segment)

Output:

(4 . 5)
Personal tools