-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_2_03.clj
45 lines (38 loc) · 1.5 KB
/
ex_2_03.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns sicp.chapter-2.part-1.ex-2-03
(:require
[sicp.chapter-2.part-1.ex-2-02 :as ex-2-02]
[sicp.misc :as m]))
; 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?
(defn length-segment
[segment]
(Math/sqrt (+ (Math/pow (- (ex-2-02/x-point (ex-2-02/start-segment segment))
(ex-2-02/x-point (ex-2-02/end-segment segment)))
2)
(Math/pow (- (ex-2-02/y-point (ex-2-02/start-segment segment))
(ex-2-02/y-point (ex-2-02/end-segment segment)))
2))))
(defn make-rectangle
[segment-width segment-height]
(m/pair segment-width segment-height))
(defn side-width-rectangle
[rectangle]
(m/car rectangle))
(defn side-height-rectangle
[rectangle]
(m/cdr rectangle))
(defn area-rectangle
[rectangle]
(* (length-segment (side-width-rectangle rectangle))
(length-segment (side-height-rectangle rectangle))))
(defn perimeter-rectangle
[rectangle]
(* 2 (+ (length-segment (side-width-rectangle rectangle))
(length-segment (side-height-rectangle rectangle)))))