Exercise 2.3. Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of your constructors and selectors, create procedures that compute the perimeter and the area of a given rectangle. Now implement a different representation for rectangles. Can you design your system with suitable abstraction barriers, so that the same perimeter and area procedures will work using either representation? ———————————————————————————————————————————————————————————————————————— ; storing a rectangle as two opposite corner points (define (make-rect p1 p2) (cons p1 p2)) (define (p1-rect rect) (car rect)) (define (p2-rect rect) (cdr rect)) ; height and width (define (abs-diff x y) (abs (- x y))) (define (height rect) (abs-diff (y-point (p1-rect rect)) (y-point (p2-rect rect)))) (define (width rect) (abs-diff (x-point (p1-rect rect)) (x-point (p2-rect rect)))) ; perimeter and area (define (perimeter rect) (* 2 (+ (height rect) (width rect)))) (define (area rect) (* (height rect) (width rect))) ; an alternative representation as a pair of xs and a pair of ys ; we use the same interface so all the other code above will still work (define (make-rect p1 p2) (cons (cons (x-point p1) (x-point p2)) (cons (y-point p1) (y-point p2)))) (define (p1-rect rect) (make-point (car (car rect)) (car (cdr rect)))) (define (p2-rect rect) (make-point (cdr (car rect)) (cdr (cdr rect))))