**Exercise 2.2**

An implementation of the required procedures follows:

```
;; Point selectors and constructors.
(define (make-point x y)
(cons x y))
(define (x-point p)
(car p))
(define (y-point p)
(cdr p))
;; Segment selectors and constructors.
(define (make-segment start end)
(cons start end))
(define (start-segment s)
(car s))
(define (end-segment s)
(cdr s))
;; Calculates the midpoint of the segment s.
(define (midpoint-segment s)
(make-point (/ (+ (x-point (start-segment s))
(x-point (end-segment s)))
2)
(/ (+ (y-point (start-segment s))
(y-point (end-segment s)))
2)))
```

**Exercise 2.3**

For the purposes of this exercise a rectangle will have two selectors *width-rect*, and *height-rect* for extracting the width and height respectively. Furthermore for sake of simplicity it is assumed that we are only interested in rectangles with sides parallel to the coordinate axes.

Having specified these selectors we can directly jump ahead and implement the *perimeter-rect* and *area-rect* procedures:

```
(define (perimeter-rect r)
(+ (* 2 (width-rect r))
(* 2 (height-rect r))))
(define (area-rect r)
(* (width-rect r)
(height-rect r)))
```

Now we can create different representations for a rectangle. The first representation treats a rectangle as a pair of points corresponding to diagonally opposite corners:

```
;; Representation 1
(define (make-rect p1 p2)
(cons p1 p2))
(define (width-rect r)
(abs (- (x-point (car r)) (x-point (cdr r)))))
(define (height-rect r)
(abs (- (y-point (car r)) (y-point (cdr r)))))
```

The second representation considers a rectangle as a point corresponding to its lower left corner, a width, and a height:

```
;; Representation 2
(define (make-rect p width height)
(cons p (cons width height)))
(define (width-rect r)
(car (cdr r)))
(define (height-rect r)
(cdr (cdr r)))
```

Changing the representation does not require changing the implementation of the *perimeter-rect* and *area-rect* procedures. This design technique is sometimes called programming to an interface, not an implementation.