my attempt to do the exercises in sicp.

Sunday, July 20, 2008

sicp exercise 2.48



;; Exercise 2.48.  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.


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

(define (add-vect vect1 vect2)
  (let ((x1 (xcor-vect vect1)))
       ((x2 (xcor-vect vect2)))
       ((y1 (ycor-vect vect1)))
       ((y2 (ycor-vect vect2)))
    (make-vect (+ x1 x2) (+ y1 y2))))

(define (sub-vect vect1 vect2)
  (let ((x1 (xcor-vect vect1)))
       ((x2 (xcor-vect vect2)))
       ((y1 (ycor-vect vect1)))
       ((y2 (ycor-vect vect2)))
    (make-vect (- x1 x2) (- y1 y2))))

(define (make-segment start end) (cons start end))
(define (start-segment line-segment) (car line-segment))
(define (end-segment   line-segment) (cdr line-segment))


No comments: