-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_1_3.clj
179 lines (145 loc) · 3.59 KB
/
book_1_3.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(ns sicp.chapter-1.part-3.book-1-3
(:require
[sicp.misc :as m]))
(comment "1.3.1 Procedures as Arguments ----------------------------------------------------------")
; Exercises:
; * 1.29
; * 1.30
; * 1.31
; * 1.32
; * 1.33
(defn sum-integers
[a b]
(if (> a b)
0
(+ a (sum-integers (+ a 1) b))))
(defn sum-cubes
[a b]
(if (> a b)
0
(+ (m/cube a)
(sum-cubes (+ a 1) b))))
(defn pi-sum
[a b]
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2)))
(pi-sum (+ a 4) b))))
(defn sum-terms
[term a next-fn b]
(if (> a b)
0
(+ (term a)
(sum-terms term (next-fn a) next-fn b))))
(defn sum-integers-2
[a b]
(sum-terms identity a inc b))
(defn sum-cubes-2
[a b]
(sum-terms m/cube a inc b))
(defn pi-sum-2
[a b]
(sum-terms #(/ 1.0 (* % (+ % 2))) a #(+ % 4) b))
(defn integral
[f a b dx]
(letfn [(add-dx [x] (+ x dx))]
(* (sum-terms f (+ a (/ dx 2)) add-dx b) dx)))
(comment "1.3.2 Constructing Procedures Using Lambda ---------------------------------------------")
; Exercises:
; * 1.34
(defn pi-sum-lamda
[a b]
(sum-terms #(/ 1.0 (* % (+ % 2))) a #(+ % 4) b))
(defn integral-lamda
[f a b dx]
(* (sum-terms f (+ a (/ dx 2.0)) #(+ % dx) b) dx))
(defn f-1
[x y]
(letfn [(f-helper
[a b]
(+ (* x (m/square a))
(* y b)
(* a b)))]
(f-helper (+ 1 (* x y))
(- 1 y))))
(defn f-2
[x y]
(let [a (+ 1 (* x y))
b (- 1 y)]
(+ (* x (m/square a))
(* y b)
(* a b))))
(comment "1.3.3 Procedures as General Methods ----------------------------------------------------")
; Exercises:
; * 1.35
; * 1.36
; * 1.37
; * 1.38
; * 1.39
(defn search
[f neg-point pos-point]
(let [midpoint (m/average neg-point pos-point)]
(if (m/close-enough? neg-point pos-point)
midpoint
(let [test-value (f midpoint)]
(cond
(m/positive? test-value) (search f neg-point midpoint)
(m/negative? test-value) (search f midpoint pos-point)
:else midpoint)))))
(defn half-interval-method
[f a b]
(let [a-value (f a)
b-value (f b)]
(cond
(and (m/negative? a-value) (m/positive? b-value)) (search f a b)
(and (m/negative? b-value) (m/positive? a-value)) (search f b a)
:else (throw (Exception. (str "Values are not of opposite sign " a " " b))))))
(defn fixed-point
[f first-guess]
(letfn [(try-fn
[guess]
(let [next (f guess)]
(if (m/close-enough? guess next 0.00001) next (recur next))))]
(try-fn first-guess)))
(defn sqrt
[x]
(fixed-point #(/ (+ % (/ x %)) 2) 1.0))
(comment "1.3.4 Procedures as Returned Values ----------------------------------------------------")
; Exercises:
; * 1.40
; * 1.41
; * 1.42
; * 1.43
; * 1.44
; * 1.45
; * 1.46
(defn average-damp
[f]
(fn [x] (m/average x (f x))))
(defn sqrt-lamda
[x]
(fixed-point (average-damp #(/ x %)) 1.0))
(defn cube-root
[x]
(fixed-point (average-damp #(/ x (m/square %))) 1.0))
(defn deriv
([g] (deriv g 0.00001))
([g dx] (fn [x] (/ (- (g (+ x dx)) (g x)) dx))))
(defn newton-transform
[g]
(fn [x] (- x (/ (g x) ((deriv g) x)))))
(defn newtons-method
[g guess]
(fixed-point (newton-transform g) guess))
(defn sqrt-newton
[x]
(newtons-method (fn [y] (- (m/square y) x)) 1.0))
(defn fixed-point-of-transform
[g transform guess]
(fixed-point (transform g) guess))
(defn sqrt-transform-average
[x]
(fixed-point-of-transform (fn [y] (/ x y)) average-damp 1.0))
(defn sqrt-newtons-transform
[x]
(fixed-point-of-transform (fn [y] (- (m/square y) x)) newton-transform 1.0))