-
Notifications
You must be signed in to change notification settings - Fork 1
/
chap1_1.sml
150 lines (97 loc) · 2.39 KB
/
chap1_1.sml
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
(*
* SICP in SML/NJ
* by Kenji Nozawa (hz7k-nzw@asahi-net.or.jp)
*
* Dependency: util.sml;
*)
structure U = Util;
structure I = Util.Int;
structure R = Util.Real;
(* 1 Building Abstractions with Procedures *)
(* 1.1 The Elements of Programming *)
(* 1.1.1 Expressions *)
486;
137 + 349;
1000 - 334;
99 * 5;
10 div 5;
2.7 + 10.0;
21 + 35 + 12 + 7;
25 * 4 * 12;
(3 * 5) + (10 - 6);
(3 * ((2 * 4) + (3 + 5))) + ((10 - 7) + 6);
(* 1.1.2 Naming and the Environment *)
val size = 2;
size;
5 * size;
val pi = 3.14159;
val radius = 10.0;
pi * radius * radius;
fun circumference () = 2.0 * pi * radius;
circumference();
(* 1.1.3 Evaluating Combinations *)
(2 + (4 * 6)) * (3 + 5 + 7);
(* 1.1.4 Compound Procedures *)
(*
* fun square x = x * x;
* => included in util.sml
*)
I.square 21;
I.square (2 + 5);
I.square (I.square 3);
fun sumOfSquares (x, y) = I.square x + I.square y;
sumOfSquares (3, 4);
fun f a = sumOfSquares (a + 1, a * 2);
f 5;
(* 1.1.5 The Substitution Model for Procedure Application *)
(* 1.1.6 Conditional Expressions and Predicates *)
(*
* fun abs x = if x < 0 then ~x else x;
* => included in util.sml
*)
fun ge (x, y) = (x > y) orelse (x = y);
fun ge (x, y) = not (x < y);
(* 1.1.7 Example: Square Roots by Newton's Method *)
(*
* fun average (x, y) = (x + y) / 2.0;
* => included in util.sml
*)
fun improve (guess, x) =
R.average (guess, x / guess);
fun goodEnough (guess, x) =
R.abs (R.square guess - x) < 0.001;
fun sqrtIter (guess, x) =
if goodEnough (guess, x) then guess
else sqrtIter (improve (guess, x), x);
fun sqrt x = sqrtIter (1.0, x);
sqrt 9.0;
sqrt (100.0 + 37.0);
sqrt (sqrt 2.0 + sqrt 3.0);
R.square (sqrt 1000.0);
(* 1.1.8 Procedures as Black-Box Abstractions *)
fun sqrt x =
let
fun goodEnough (guess, x) =
R.abs (R.square guess - x) < 0.001
and improve (guess, x) =
R.average (guess, x / guess)
fun sqrtIter (guess, x) =
if goodEnough (guess, x) then guess
else sqrtIter (improve (guess, x), x)
in
sqrtIter (1.0, x)
end;
sqrt 9.0;
fun sqrt x =
let
fun goodEnough guess =
R.abs (R.square guess - x) < 0.001
and improve guess =
R.average (guess, x / guess)
fun sqrtIter guess =
if goodEnough guess then guess
else sqrtIter (improve guess)
in
sqrtIter 1.0
end;
sqrt 9.0;