-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-1_exercises.rkt
173 lines (126 loc) · 3.32 KB
/
1-1_exercises.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
;1.1
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.2
(/ (+
(+ 5 4)
(- 2
(- 3
(+ 6
(/ 4 5)))))
(* 3
(- 6 2)
(- 2 7)))
-37/150
;;;;
;second attempt
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (* (- 6 2) (- 2 7))))
;;;;;;;;;;;;;;;;;;;;;;;;;
;1.3
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sumsqlgst a b c)
(cond
((and (>= a c) (>= b c)) (sum-of-squares a b))
((and (>= b a) (>= c a)) (sum-of-squares b c))
((and (>= a b) (>= c b)) (sum-of-squares a c))))
(square-largest 2 5 9)
(square-largest 17 82 5)
(square-largest .75 8 2)
; second attempt
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sumsq-largest a b c)
(cond ((> a b) (> b c)
(+ (sum-of-squares a b)))
((> a c) (> c b)
(+ (sum-of-squares a c)))
(+ (sum-of-squares b c))))
(sumsq-largest 1 2 3)
(sumsq-largest 5 1 10)
(sumsq-largest 2 10 1)
; third attempt
(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (* x x) (* y y)))
(define (sum-greater x y z)
(cond
((and (> x y) (> y z)) (sum-of-squares x y))
((and (> x z) (> z y)) (sum-of-squares x z))
((and (> z x) (> y x)) (sum-of-squares z y))))
(sum-greater 2 5 9)
(sum-greater 17 82 5)
(sum-greater .75 8 2)
; forth attempt
(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sumsqr-largest a b c)
(cond ((and (>= a c) (>= b c)) (sum-of-squares a b))
((and (>= b a) (>= c a)) (sum-of-squares b c))
((and (>= a b) (>= c b)) (sum-of-squares a c))))
(sumsqr-largest 3 6 9)
;; the order in which you do the greater thans matters. it is checking for systematic truths, so a and c greater than b, b and c greater than a, a and b greater than c
; 5th
(define (square x)
(* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (sum-sqlgst a b c)
(cond
((and (> a c) (> b c)) (sum-of-squares a b))
((and (> b a) (> c a)) (sum-of-squares b c))
((and (> a b) (> c b)) (sum-of-squares a c))
(else '(huh?))))
(sum-sqlgst 2 5 9)
;;;;;;;;;;;;;;;;;;;;;;;;;
; 1.4. Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
; define procedure a-plus-abs-a-b
; if b is greater than zero plus or minus one, return a b
;;;;;;;;;;;;;;;;;;;;;;;;;
;1.8
(define (square x)
(* x x))
(define (cube x)
(* (square x) x))
(define (good-enough? guess prior-guess)
(< (abs (- (square guess) (square prior-guess))) 0.001))
(define (improve-cube guess x)
(/ (+ (* 2 guess) (/ x (square guess))) 3))
(define (cubrt-iter guess prior-guess x)
(if (good-enough? guess prior-guess)
guess
(cubrt-iter (improve-cube guess x)
guess
x)))
(define (cube-root x)
(cubrt-iter 1.0 0.0 x))
(cube-root 27)
(cube-root 382)
(cube-root .73)