# 2.3

## 练习 2.3 请实现一种平面矩形的表示（提示：你有可能借用练习2.2的结果）。基于你的构造函数和选择函数定义几个过程，计算给定矩形的周长和面积等。现在请再为矩形实现另一种表示方式。你应该怎样设计系统，使之能提供适当的抽象屏障，使同一个周长或者面积过程对两种不同表示都能工作？

### 这里只给出了简化的两种方式，只支持边与坐标轴平等的矩形

#### 第一种方式

(define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (make-segment start end) (cons start end) ) (define (start-segment segment) (car segment)) (define (end-segment segment) (cdr segment)) (define (print-point p) (newline) (display "(") (display (x-point p)) (display ", ") (display (y-point p)) (display ")")) (define (print-segment segment) (newline) (print-point (start-segment segment)) (display " ---- ") (print-point (end-segment segment)) ) (define (rect left-top-point right-bottom-point) (cons left-top-point right-bottom-point)) (define (print-rect rect) (newline) (display "Rectange: ") (print-segment rect) ) (define r (rect (make-point 0 0) (make-point 5 10))) (print-rect r) (define left-top-point car) (define right-bottom-point cdr) (print-point (left-top-point r)) (print-point (right-bottom-point r)) (define (length rectangle) (abs (- (x-point (left-top-point rectangle)) (x-point (right-bottom-point rectangle))))) (newline) (display "length of the rectangle is ") (display (length r)) (define (width rectangle) (abs (- (y-point (left-top-point rectangle)) (y-point (right-bottom-point rectangle))))) (newline) (display "width of the rectangle is ") (display (width r)) (define (area rectangle) (* (length rectangle) (width rectangle))) (newline) (display "area of the rectangle is ") (display (area r))

#### 第二种方式

(define (print-point p) (newline) (display "(") (display (x-point p)) (display ", ") (display (y-point p)) (display ")")) (define (print-segment segment) (newline) (print-point (start-segment segment)) (display " ---- ") (print-point (end-segment segment)) ) (define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (make-segment start end) (cons start end) ) (define (start-segment segment) (car segment)) (define (end-segment segment) (cdr segment)) (define (rect2 bottom-segment height) (cons bottom-segment height)) (define r (rect2 (make-segment (make-point 5 10) (make-point 10 10)) 8)) (define (print-rect2 r) (newline) (print-segment (car r)) (display "*") (display (cdr r)) ) (display "The rectangle is ") (print-rect2 r) (define side car) (newline) (display "The side of the rectangle is ") (print-segment (side r)) (define (length r) (abs (- (x-point (start-segment (side r))) (x-point (end-segment (side r)))))) (newline) (display "The length of the rectangle is ") (display (length r)) (define width cdr) (define (area r) (* (length r) (width r))) (newline) (display "The area of the rectangle is ") (display (area r))