Solution to SICP Exercise 2.2

Structure and Interpretation of Computer Programs

Solution to Exercise 2.2:

(define (make-segment start end)
(cons start end))

(define (start-segment s)
(car s))

(define (end-segment s)
(cdr s))

(define (make-point x y)
(cons x y))

(define (x-point p)
(car p))

(define (y-point p)
(cdr p))

(define (avg a b)
(/ (+ a b) 2))

(define (midpoint-segment s)
(let ((start (start-segment s))
(end (end-segment s)))
(make-point (avg (x-point start) (x-point end))
(avg (y-point start) (y-point end)))))

Advertisements