-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-1.rkt
250 lines (175 loc) · 5.75 KB
/
1-1.rkt
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
;1.1.1
(+ 137 349)
(- 1000 334)
(* 5 99)
(/ 10 5)
(+ 2.7 10)
(+ 21 35 12 7)
(* 25 4 12)
(+ (* 3 5) (- 10 6))
(+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))
(+ (* 3
(+ (* 2 4)
(+ 3 5)))
(+ (- 10 7)
6))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.2
(define size 2)
size
( * 5 size)
(define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
(define circumference (* 2 pi radius))
circumference
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.3
(* (+ 2 (* 4 6))
(+ 3 5 7))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.4
; (define (<name> <formal parameters>) <body>)
(define (square x) (* x x))
(square 21)
(square (+ 2 5))
(square (square 3))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 3 4)
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
(f 5)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.5
(define (square x) (* x x))
(square 21)
(square (+ 2 5))
(square (square 3))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(sum-of-squares 3 4)
(define (f a)
(sum-of-squares (+ 5 1) (* 5 2)))
(f 5)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.6
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
; last line could also read (else (< x 0) (- x))))
(abs 3)
(define (abs x)
(if (< x 0)
(- x)
x))
; another way of defining this with if
; general form of if: (if <predicate> <consequent> <alternative>)
; (and <e1> ... <en>)
; evaluates the expressions <e> one at a time, in left-to-right order. If any evaluate to false, the value of the and expression is false, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to true values, the value of the and expression is the value of the last one.
; (or <e1> ... <en>)
; evaluates the expressions <e> one at a time, in left-to-right order. If any <e> evaluates to a true value, that value is returned as the value of the or expression, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to false, the value of the or expression is false.
; (not <e>)
; The value of a not expression is true when the expression <e> evaluates to false, and false otherwise.
(and (> x 5) (< x 10))
(define (>= x y)
(or (> x y) (= x y)))
(define (>= x y)
(not (< x y)))
10
(+ 5 3 4)
(- 9 1)
(/ 6 2)
(+ (* 2 4) (- 4 6))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
b
a)
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
(+ 2 (if (> b a) b a))
(* (cond ((> a b) a)
((< a b) b)
(else -1))
(+ a 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;1.1.7
; The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge.
(define (square x)
(* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 9)
(sqrt (+ 100 37))
(sqrt (+ (sqrt 2) (sqrt 3)))
(square (sqrt 1000))
; square x is equal to x times itself
; average x y is equal to x + y / 2
; sqrt calls sqrt-iter
; sqrt-iter says guess variable x
; if that's good enough, guess x and return guess
; if not, call sqrt-iter again and improve the guess of x, then return x, repeating as necessary to refine
; improve takes guess and x as variables, then returns the average guess by dividing x by the guess
; good enough takes guess and x and returns if the result is greater than the absolute value of the square guess minus x within a value of 0.001
(sqrt 9)
(sqrt (+ 100 37))
(sqrt (+ (sqrt 2) (sqrt 3)))
(square (sqrt 1000))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
1.1.8
(define (square x) (* x x))
(define (square x)
(exp (double (log x))))
(define (double x) (+ x x))
; these processes all do the same thing but with different details. and example of how procedure definition supresses detail. they are black box procedures that can be called to do the same functions
; ****** if the parameters were not local to the bodies of their respective procedures, then the parameter x in square could be confused with the parameter x in good-enough?, and the behavior of good-enough? would depend upon which version of square we used. Thus, square would not be the black box we desired. *********
(define (sqrt x)
(sqrt-iter 1.0 x))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
; if you have a larger system, this becomes a problem because functions like good-enough or improve might be needed elsewhere
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
; block structure - but we can not only internalize the definitions of auxiliary procedures, we can simplify them
; since x is bound in the definition of sqrt, good-enough?, improve, and sqrt-iter, are in the scope of x
; so we do not need to pass x ecplicitly to each procedure, we can just let x be a free variable in the internal definitions
(define (sqrt x)
(define (good-enough? guess)
(< (abs (- (square guess) x)) 0.001))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))