-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_1_1.clj
151 lines (116 loc) · 4 KB
/
book_1_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
149
150
151
(ns sicp.chapter-1.part-1.book-1-1)
(comment "1.1.1 Expressions ----------------------------------------------------------------------")
(+ 137 349) ; 486
(- 1000 334) ; 666
(* 5 99) ; 495
(/ 10 5) ; 2
(+ 2.7 10) ; 12.7
(+ 21 35 12 7) ; 75
(* 25 4 12) ; 1200
(+ (* 3 5) (- 10 6)) ; 19
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) ; 57
(+ (* 3
(+ (* 2 4)
(+ 3 5)))
(+ (- 10 7)
6)) ; 57
(comment "1.1.2 Naming and the Environment -------------------------------------------------------")
(def size 2)
(* 5 size) ; 10
(def pi 3.14159)
(def radius 10)
(* pi (* radius radius)) ; 314.159
(def circumference (* 2 pi radius)) ; 62.8318
(comment "1.1.3 Evaluating Combinations ----------------------------------------------------------")
(* (+ 2 (* 4 6)) (+ 3 5 7)) ; 390
(comment "1.1.4 Compound Procedures --------------------------------------------------------------")
(defn square
[x]
(* x x))
(square 21) ; 441
(square (+ 2 5)) ; 49
(square (square 3)) ; 81
(defn sum-of-squares
[x y]
(+ (square x) (square y)))
(sum-of-squares 3 4) ; 25
(defn f
[a]
(sum-of-squares (+ a 1) (* a 2)))
(f 5) ; 136
(comment "1.1.5 The Substitution Model for Procedure Application ---------------------------------")
(f 5) ; 136
(sum-of-squares (+ 5 1) (* 5 2)) ; 136
(+ (square 6) (square 10)) ; 136
(+ (* 6 6) (* 10 10)) ; 136
(+ 36 100) ; 136
(sum-of-squares (+ 5 1) (* 5 2)) ; 136
(+ (square (+ 5 1))
(square (* 5 2))) ; 136
(+ (* (+ 5 1) (+ 5 1))
(* (* 5 2) (* 5 2))) ; 136
(comment "1.1.6 Conditional Expressions and Predicates -------------------------------------------")
; Exercises:
; * 1.1
; * 1.2
; * 1.3
; * 1.4
; * 1.5
; Renamed `abs` => `abs-x` due to Clojure conflit names
(defn abs-1
[x]
(cond (> x 0) x
(= x 0) 0
(< x 0) (- x)))
(defn abs-2
[x]
(cond (< x 0) (- x)
:else x))
(defn abs-3
[x]
(if (< x 0)
(- x)
x))
; Renamed due to Clojure conflit names
(defn >=-1
[x y]
(or (> x y) (= x y)))
(defn >=-2
[x y]
(not (< x y)))
(comment "1.1.7 Example: Square Roots by Newton’s Method -----------------------------------------")
; Exercises:
; * 1.6
; * 1.7
; * 1.8
(defn average
[x y]
(/ (+ x y) 2))
(defn improve
[guess x]
(average guess (/ x guess)))
(defn good-enough?
[guess x]
(< (abs (- (square guess) x)) 0.001))
(defn sqrt-iter
[guess x]
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(defn sqrt
[x]
(sqrt-iter 1.0 x))
(comment "1.1.8 Procedures as Black-Box Abstractions ---------------------------------------------")
(defn square-alt
[x]
(Math/exp (double (Math/log x))))
(defn sqrt-alt
[x]
(letfn [(good-enough? [guess x] (< (abs (- (* guess guess) x)) 0.001))
(improve [guess x] (/ (+ guess (/ x guess)) 2))
(sqrt-iter
[guess x]
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))]
(sqrt-iter 1.0 x)))