2 (:use [sicp ex2_2 utils]
5 (declare width-rect height-rect)
6 ;; wishful thinking. Assume that constructors and selectors are
7 ;; available. Now compute perimeter.
8 (defn perimeter-rect [rect]
9 (* 2 (+ (width-rect rect)
12 (defn area-rect [rect]
16 ;; now implement the rectangle. One way to represent a rectangle
17 ;; is to use a pair of points representing diagonal corners.
18 (defn make-rect [p1 p2]
30 (abs (- (x-point p1) (x-point p2)))))
35 (abs (- (y-point p1) (y-point p2)))))
37 (deftest test-rect-perimeter-square
38 (is (= (perimeter-rect (make-rect (make-point 0 0) (make-point 1 1))) 4)))
40 (deftest test-rect-area-square
41 (is (= (area-rect (make-rect (make-point 0 0) (make-point 2 2))) 4)))
43 ;; part2 - another implementation
44 ;; instead of coordinates, we store height and width itself.
45 (defn make-rect-1 [h w]
48 (defn width-rect-1 [r]
51 (defn height-rect-1 [r]
54 ;; the below routines are unmodified versions of the above
55 ;; procedures, but renamed to work around the compiler. Is
56 ;; there a simpler way?
57 (defn perimeter-rect-1 [rect]
58 (* 2 (+ (width-rect-1 rect)
59 (height-rect-1 rect))))
61 (defn area-rect-1 [rect]
62 (* (width-rect-1 rect)
63 (height-rect-1 rect)))
65 (deftest test-rect-perimeter-square-1
66 (is (= (perimeter-rect-1 (make-rect-1 1 1)) 4)))
68 (deftest test-rect-area-square-1
69 (is (= (area-rect-1 (make-rect-1 2 2)) 4)))