-
Notifications
You must be signed in to change notification settings - Fork 5
/
chapter_1_1.metta
executable file
·195 lines (152 loc) · 4.45 KB
/
chapter_1_1.metta
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
(= (sqr $x) (* $x $x))
!(assertEqual
(sqr 21)
441)
!(assertEqual
(sqr (+ 2 5))
49)
!(assertEqual
(sqr (sqr 3))
81)
(= (sum-of-squares $x $y) (+ (sqr $x) (sqr $y)))
!(assertEqual
(sum-of-squares 3 4)
25)
(= (f $a) (sum-of-squares (+ $a 1) (* $a 2)))
!(assertEqual
(f 5)
136)
(= (abs $x)
(if (> $x 0)
$x
(if (== $x 0)
0
(* $x -1))))
!(assertEqual
(abs -5)
5)
(= (_abs $x)
(if (< $x 0)
(* $x -1)
$x))
!(assertEqual
(_abs -5)
5)
(= (>= $x $y) (or (> $x $y) (== $x $y)))
!(assertEqual
(>= 6 5)
True)
; Exercise 1.3
; Define procedure that takes three numbers as arguments and returns
; the sum of the squares of the two larger numbers
; I haven't got an idea of function's name
(= (exercise_1_3 $x1 $x2 $x3)
(if (and (>= $x1 $x2) (>= $x3 $x2))
(sum-of-squares $x1 $x3)
(if (and (>= $x3 $x1) (>= $x2 $x1))
(sum-of-squares $x2 $x3)
(sum-of-squares $x1 $x2))))
; Check function using all permutations of 1 2 and 5
!(assertEqual
(exercise_1_3 1 2 5)
29)
!(assertEqual
(exercise_1_3 1 5 2)
29)
!(assertEqual
(exercise_1_3 2 1 5)
29)
!(assertEqual
(exercise_1_3 2 5 1)
29)
!(assertEqual
(exercise_1_3 5 1 2)
29)
!(assertEqual
(exercise_1_3 5 2 1)
29)
; -----------------------End of Exercise 1.3----------------------------
(= (a-plus-abs-b $a $b)
((if (> $b 0) + -) $a $b))
!(assertEqual
(a-plus-abs-b 5 -5)
10)
; Test to determine whether the interpreter he is faced with is using
; applicative-order evaluation or normal-order evaluation
(: test (-> Number Atom Atom))
(= (p) (p))
(= (test $x $y)
(if (== $x 0)
0
$y))
; In the case of metta, evaluation type depends on the type definition of function "test".
; Default type which is %Undefined% implies applicative-order,
; while (: test (-> Number Atom Atom)) implies normal-order evaluation.
!(assertEqual
(test 0 (p))
0)
(= (average $x $y)
(/ (+ $x $y) 2))
(= (improve $guess $x)
(average $guess (/ $x $guess)))
(= (good-enough? $guess $x)
(< (_abs (- (sqr $guess) $x)) 0.001))
(= (sqrt-iter $guess $x)
(if (good-enough? $guess $x)
$guess
(sqrt-iter (improve $guess $x) $x)))
(= (sqrt $x)
(sqrt-iter 1.0 $x))
!(assertEqual
(sqrt 9)
3.00009155413138)
!(assertEqual
(sqrt (+ (sqrt 2) (sqrt 3)))
1.7739279023207892)
; Exercise 1.7.
; The good-enough? test used in computing square roots will not be very
; effective for finding the square roots of very small numbers. Also, in real computers,
; arithmetic operations are almost always performed with limited precision. This makes our
; test inadequate for very large numbers. Explain these statements, with examples showing
; how the test fails for small and large numbers. An alternative strategy for implementing
; good-enough? is to watch how guess changes from one iteration to the next and to stop when
; the change is a very small fraction of the guess. Design a square-root procedure that uses
; this kind of end test. Does this work better for small and large numbers?
(= (better-good-enough? $old-guess $guess $x)
(< (abs (- $old-guess $guess)) 0.000001))
(= (better-sqrt $x)
(better-sqrt-iter 0.0 1.0 $x))
(= (better-sqrt-iter $oldguess $guess $x)
(if (better-good-enough? $oldguess $guess $x)
$guess
(better-sqrt-iter $guess (improve $guess $x)
$x)))
!(assertEqual
(better-sqrt 9)
3.0)
!(assertEqual
(better-sqrt (+ (better-sqrt 2) (better-sqrt 3)))
1.773771228186423)
!(assertEqual
(better-sqrt (sqr 1000))
1000)
; -----------------------End of Exercise 1.7----------------------------
; Exercise 1.8.
; Newton's method for cube roots is based on the fact that
; if y is an approximation to the cube root of x, then a better approximation is given by the value
; (x/y^2 + 2y) / 3
; Use this formula to implement a cube-root procedure analogous to the square-root procedure.
(= (cube $x) (* $x (sqr $x)))
(= (cubert $x) (cubert-iter 1.0 $x))
(= (cubert-iter $guess $x)
(if (good-enough-cubert? $guess $x)
$guess
(cubert-iter (improve-cubert $guess $x) $x)))
(= (improve-cubert $guess $x)
(/ (+ (/ $x (sqr $guess)) (* 2 $guess)) 3))
(= (good-enough-cubert? $guess $x)
(< (abs (- (improve-cubert $guess $x) $guess)) 0.000001))
!(assertEqual
(cube (cubert 8))
8.000000000144743)
; -----------------------End of Exercise 1.8----------------------------