-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-3.rkt
224 lines (165 loc) · 4.3 KB
/
1-3.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
;1.3.1
(define (sum-integers a b)
(if (> a b)
0
(+ a (sum-intigers (+ a 1) b))))
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a) (sum-cubes (+ a 1) b))))
(define (pi-sum a b)
(if (> a b)
0
(+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
;;;
(define (inc n) (+ n 1))
(define (sum-cubes a b)
(sum-cube a inc b))
(sum-cubes 1 10)
;;;
(define (identity x) x)
(define (sum-integers a b)
(sum identity a inc bb))
(sum-integers 1 10)
(define (sum f a next stopnumber)
(if (> a stopnumber)
0
(+ (f a)
(sum f (next a) next stopnumber))))
;;; renaming variables so that they are easier to understand in terms of the actual function they perform
;;;
(define (cube x) (* x x x))
(define (pi-sum a b)
(define (pi-term x)
(/ 1.0 (* x (+ x 2))))
(define (pi-next x)
(+ x 4))
(sum pi-term a pi-next b))
(* 8 (pi-sum 1 1000))
(define (integral f a b dx)
(define (add-dx x) (+ x dx))
(* (sum f (+ a (/ dx 2.0)) add-dx b)
dx))
(integral cube 0 1 0.01)
(integral cube 0 1 0.001)
; 1.3.2
(lambda (x) (+ x 4))
(lambda (x) (/ 1.0 (* x (+ x 2))))
(define (pi-sum a b)
(sum (lambda (x) (/ 1.0 (* x (+ x 2))))
a
(lambda (x) (+ x 4))
b))
(define (integral f a b dx)
(* (sum f
(+ a (/ dx 2.0))
(lambda (x) (+ x dx))
b)
dx))
; (lambda (<formal-parameters>) <body>)
;(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))
; the procedure of an argument x that adds x to 4
((lambda (x y z) (+ x y (square z))) 1 2 3)
; 12
; using let to create local variables
(define (f x y)
(define (f-helper a b)
(+ (* x (square a))
(* y b)
(* a b)))
(f-helper (+ 1 (* x y))
(- 1 y)))
(define (f x y)
((lambda (a b)
(+ (* x (square a))
(* y b)
(* a b)))
(+ 1 (* x y))
(- 1 y)))
; The general form of a let expression is
;(let ((<var1> <exp1>)
; (<var2> <exp2>)
; (<varn> <expn>))
; <body>)
; let <var1> have the value <exp1> and
; <var2> have the value <exp2> and
; <varn> have the value <expn>
; in <body>
; using let to write the f procedure
(define (f x y)
(let ((a (+ 1 (* x y)))
(b (- 1 y)))
(+ (* x (square a))
(* y b)
(* a b))))
; when let is evaluated, each name is associated with the value of the corresponding expression. the body of let is evaluated with these names bounda s local variables
; let is interpreted as an alternate syntax for
; ((lambda (<var1> ...<varn>)
; <body>)
; <exp1>
; <expn>)
(define x 5)
(+ (let ((x 3))
(+ x (* x 10)))
x)
; 38
(define x 2)
(let ((x 3)
(y (+ x 2)))
(* x y))
; 12
; 1.3.3
(define (average a b)
(/ (+ a b) 2))
(define (search f neg-point pos-point)
(let ((midpoint (average neg-point pos-point)))
(if (close-enough? neg-point pos-point)
midpoint
(let ((test-value (f midpoint)))
(cond (( positive? test-value)
(search f neg-point midpoint))
((negative? test-value)
(search f midpoint pos-point))
(else midpoint))))))
(define (close-enough? x y)
(< (abs (- x y)) 0.001))
(define (half-interval-method f a b)
(let ((a-value (f a))
(b-value (f b)))
(cond ((and (negative? a-value) (positive? b-value))
(search f a b))
((and (negative? b-value) (positive? a-value))
(search f a b))
(else
(error "Values ar enot of opposite sign" a b)))))
(half-interval-method sin 2.0 4.0)
(half-interval-method (lambda (x) (- (* x x x) (* 2 x) 3))
1.0
2.0)
(define (close-enough? x y)
(< (abs (- x y)) 0.001))
(define tolerance 0.00001)
(define (fixed-point f first-guess)
(define (close-enough v1 v2)
(< (abs (- v1 v2)) tolerance))
(define (try guess)
(let ((next (f guess)))
(if (close-enough? guess next)
next
(try next))))
(try first-guess))
(fixed-point cos 1.0)
(fixed-point (lambda (y) (+ (sin y) (cos y)))
1.0)
(define (sqrt x)
(fixed-point (lambda (y) (/ x y))
1.0))
(define (sqrt x)
(fixed-point (lambda (y) (average y (/ x y)))
1.0))