forked from cs3110/textbook-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter7.ml
139 lines (102 loc) · 3.86 KB
/
chapter7.ml
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
(********************************************************************
* exercise: mutable fields
********************************************************************)
type student = {name: string; mutable gpa: float}
let alice = {name = "Alice"; gpa = 3.7}
let () = alice.gpa <- 4.0
(********************************************************************
* exercise: refs
********************************************************************)
(* Exercise: refs *)
let (_ : bool ref) = ref true
let (_ : int list ref) = ref [5;3]
let (_ : int ref list) = [ref 5; ref 3]
(********************************************************************
* exercise: inc fun
********************************************************************)
let cs3110 =
let inc = ref (fun x -> x + 1) in
!inc 3109
(********************************************************************
* exercise: addition assignment
********************************************************************)
let (+:=) x y =
x := !x + y
(********************************************************************
* exercise: physical equality
********************************************************************)
let _ =
let x = ref 0 in
let y = x in
let z = ref 0 in
assert (x == y);
assert (not (x == z));
assert (x = y);
assert (x = z);
x := 1;
assert (x = y);
assert (not (x = z))
(********************************************************************
* exercise: norm
********************************************************************)
(* AF: the float array [| x1; ...; xn |] represents the
* vector (x1, ..., xn)
* RI: the array is non-empty *)
type vector = float array
(** [norm v] is the Euclidean norm of [v]. *)
let norm v =
sqrt (Array.fold_left (fun acc x -> acc +. x ** 2.) 0. v)
(* another solution: same asymptotic complexity but
less efficient. Perhaps more readable. *)
let norm' v =
v
|> Array.map (fun x -> x ** 2.) (* square each element *)
|> Array.fold_left (+.) 0. (* sum all elements *)
|> sqrt
(********************************************************************
* exercise: normalize
********************************************************************)
(* effects: [normalize v] modifies [v] to be its normalized form. *)
let normalize v =
let n = norm v in (* Must calculate norm before iteration *)
Array.iteri (fun i x -> v.(i) <- x /. n) v
(********************************************************************
* exercise: normalize loop
********************************************************************)
(* effects: [normalize_loop v] modifies [v] to be its normalized form. *)
let normalize_loop v =
let n = norm v in
for i = 0 to Array.length v - 1 do
v.(i) <- v.(i) /. n
done
(********************************************************************
* exercise: norm loop
********************************************************************)
(** [norm_loop v] is the Euclidean norm of [v]. *)
let norm_loop v =
let n = ref 0.0 in
for i = 0 to Array.length v - 1 do
n := !n +. (v.(i) ** 2.)
done;
sqrt !n
(********************************************************************
* exercise: imperative factorial
********************************************************************)
(** [fact_loop n] is the factorial of [n].
* requires: [n >= 0]
*)
let fact_loop n =
let ans = ref 1 in
for i = 1 to n do
ans := !ans * i
done;
!ans
(********************************************************************
* exercise: init matrix
********************************************************************)
(* [init_matrix n o f] creates and returns an [n] by [o] matrix [m]
* with [m.(i).(j) = f i j] for all [i] and [j] in bounds.
* requires: [n, o >= 0]
*)
let init_matrix n o f =
Array.init n (fun i -> Array.init o (fun j -> f i j))