-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_2_1.clj
148 lines (119 loc) · 3.13 KB
/
book_2_1.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(ns sicp.chapter-2.part-1.book-2-1
(:require
[sicp.chapter-2.part-1.ex-2-07 :as ex-2-07]
[sicp.misc :as m]))
(comment "2 Building Abstractions with Data ------------------------------------------------------")
(defn linear-combination
[a b x y]
(+ (* a x) (* b y)))
; (defn linear-combination [a b x y]
; (add (mul a x) (mul b y)))
(comment "2.1.1 Example: Arithmetic Operations for Rational Numbers ------------------------------")
; Exercises:
; * 2.1
; (defn make-rat [n d] (m/pair n d))
(defn make-rat
[numer denom]
(let [g (m/gcd numer denom)]
(m/pair (/ numer g)
(/ denom g))))
(defn numer
[ratio]
(m/car ratio))
(defn denom
[ratio]
(m/cdr ratio))
(defn add-rat
[x y]
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(defn sub-rat
[x y]
(make-rat (- (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(defn mul-rat
[x y]
(make-rat (* (numer x) (numer y))
(* (denom x) (denom y))))
(defn div-rat
[x y]
(make-rat (* (numer x) (denom y))
(* (denom x) (numer y))))
(defn equal-rat?
[x y]
(= (* (numer x) (denom y))
(* (numer y) (denom x))))
(defn print-rat
[ratio]
(str (numer ratio) "/" (denom ratio)))
(comment "2.1.2 Abstraction Barriers -------------------------------------------------------------")
; Exercises:
; * 2.2
; * 2.3
(defn make-rat-alt
[numer denom]
(m/pair numer denom))
(defn numer-alt
[ratio]
(let [g (m/gcd (m/car ratio) (m/cdr ratio))]
(/ (m/car ratio) g)))
(defn denom-alt
[ratio]
(let [g (m/gcd (m/car ratio) (m/cdr ratio))]
(/ (m/cdr ratio) g)))
(comment "2.1.3 What Is Meant by Data? -----------------------------------------------------------")
; Exercises:
; * 2.4
; * 2.5
; * 2.6
(defn pair-alt
[x y]
(let [dispatch (fn [m]
(cond
(= m 0) x
(= m 1) y
:else (throw (Exception. "Argument not 0 or 1: CONS"))))]
dispatch))
(defn car-alt
[z]
(z 0))
(defn cdr-alt
[z]
(z 1))
(comment "2.1.4 Extended Exercise: Interval Arithmetic -------------------------------------------")
; Exercises:
; * 2.7
; * 2.8
; * 2.9
; * 2.10
; * 2.11
; * 2.12
; * 2.13
; * 2.14
; * 2.15
; * 2.16
(defn mul-interval
[x y]
(let [p1 (* (ex-2-07/lower-bound x)
(ex-2-07/lower-bound y))
p2 (* (ex-2-07/lower-bound x)
(ex-2-07/upper-bound y))
p3 (* (ex-2-07/upper-bound x)
(ex-2-07/lower-bound y))
p4 (* (ex-2-07/upper-bound x)
(ex-2-07/upper-bound y))]
(ex-2-07/make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(defn div-interval
[x y]
(mul-interval x (ex-2-07/make-interval
(/ 1.0 (ex-2-07/upper-bound y))
(/ 1.0 (ex-2-07/lower-bound y)))))
(defn add-interval
[x y]
(ex-2-07/make-interval (+ (ex-2-07/lower-bound x)
(ex-2-07/lower-bound y))
(+ (ex-2-07/upper-bound x)
(ex-2-07/upper-bound y))))