-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch10.ml
200 lines (158 loc) · 5.21 KB
/
ch10.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
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
(* Definitions from the chapter *)
type colour =
| Red
| Green
| Blue
| Yellow
| RGB of int * int * int
(* col : colour *)
let col = Blue
(* cols : colour list *)
let cols = [Red ; Red ; Green ; Yellow]
(* colpair : char * colour *)
let colpair = ('R', Red)
(* cols' : colour list *)
let cols' = [Red; Red; Green; Yellow; RGB (150, 0, 255)]
let components colour =
match colour with
| Red -> (255, 0, 0)
| Green -> (0, 255, 0)
| Blue -> (0, 0, 255)
| Yellow -> (255, 255, 0)
| RGB (r, g, b) -> (r, g, b)
(* here the definition of type 'a option is introduced but it's already in pervasives *)
(* nothing : 'a option *)
let nothing = None
(* number : int option *)
let number = Some 50
(* numbers : int option list *)
let numbers = [Some 12; None; None; Some 2]
(* word : char list option *)
let word = Some ['c';'a';'k';'e']
(* lookup_opt : 'a -> 'a list -> 'a option *)
let rec lookup_opt x l =
match l with
| [] -> None
| h :: tl -> if h = x then Some h else lookup_opt x tl
type 'a sequence =
| Nil
| Cons of 'a * 'a sequence
(* empty_sequence : 'a sequence *)
(* like [] *)
let empty_sequence = Nil
(* singleton_sequence : 'a -> 'a sequence *)
(* like [e] *)
let singleton_sequence e = Cons (e, Nil)
(* sequence_of_list : 'a list -> 'a sequence *)
(* not in the text, I just wanted to write this to make it easier to make sequences *)
let rec sequence_of_list l =
match l with
| [] -> Nil
| h :: tl -> Cons (h, sequence_of_list tl)
(* axe : char sequence *)
(* Cons ('a', Cons ('x', Cons ('e', Nil)))*)
let axe = sequence_of_list ['a';'x';'e']
(* length : 'a sequence -> int *)
let rec length s =
match s with
| Nil -> 0
| Cons (_, tl) -> 1 + length tl
(* append : 'a sequence -> 'a sequence -> 'a sequence *)
let rec append a b =
match a with
| Nil -> b
| Cons (h, tl) -> Cons (h, append tl b)
(* list_of_sequence : 'a sequence -> 'a list *)
(* not in the text, I just thought that since could make a sequence from a list
I ought also to be able to make a list from a sequence *)
let rec list_of_sequence s =
match s with
| Nil -> []
| Cons (h, tl) -> h :: list_of_sequence tl
type expr =
| Num of int
| Add of expr * expr
| Subtract of expr * expr
| Multiply of expr * expr
| Divide of expr * expr
(* one_plus_two_times_three : expr *)
(* 1 + (2 * 3) *)
let one_plus_two_times_three = Add (Num 1, Multiply (Num 2, Num 3))
(* evaluate : expr -> int *)
let rec evaluate e =
match e with
| Num x -> x
| Add (e, e') -> evaluate e + evaluate e'
| Subtract (e, e') -> evaluate e - evaluate e'
| Multiply (e, e') -> evaluate e * evaluate e'
| Divide (e, e') -> evaluate e / evaluate e'
(* Questions *)
(* 1. Design a new type `rect` for representing rectangles. Treat squares as a special case. *)
type rect =
| Square of int
| Rectangle of int * int (* (width, height) *)
(* 2. Write a function of type `rect -> int` to calculate the area of a given rect. *)
(* width : rect -> int *)
let width r =
match r with
| Square s -> s
| Rectangle (width, _) -> width
(* height : rect -> int *)
let height r =
match r with
| Square s -> s
| Rectangle (_, height) -> height
(* the width and height functions aren't strictly necessary but it seemed like a good idea
to write functions that make it obvious which part of the rectangle tuple is the width and which is the height *)
(* area : rect -> int *)
let area r = (width r) * (height r)
(* 3. Write a function which rotates a rect such that it is at least as tall as it is wide. *)
(* rotate_upright : rect -> rect *)
let rotate_upright r =
match r with
| Square _ -> r
| Rectangle (s1,s2) ->
if s2 >= s1 then r
else Rectangle (s2, s1)
(* 4. Write a function which, given a rect list, returns another list which has the smallest total width and whose
members are sorted narrowest first. *)
(* tidy_rectangles : rect list -> rect list *)
let tidy_rectangles rs = List.map rotate_upright rs |> List.sort compare
(* 5. write `take`, `drop`, and `map` for the sequence type. *)
let rec take_s n s =
if n <= 0 then Nil else
match s with
| Nil -> Nil
| Cons (h, tl) -> Cons (h, (take_s (n - 1) tl))
let rec drop_s n s =
if n <= 0 then s else
match s with
| Nil -> Nil
| Cons (_, tl) -> drop_s (n - 1) tl
let rec map_s f s =
match s with
| Nil -> Nil
| Cons (h, tl) -> Cons (f h, map_s f tl)
(* 6. extend the expr type and evaluate function to allow raising a number to a power *)
type expr' =
| Num of Num.num (* NOTE: Num is not part of OCaml after 4.05 *)
| Add of expr' * expr'
| Subtract of expr' * expr'
| Multiply of expr' * expr'
| Divide of expr' * expr'
| Exponentiate of expr' * expr'
let rec evaluate' (e : expr') =
match e with
| Num x -> x
| Add (e, e') -> Num.add_num (evaluate' e) (evaluate' e')
| Subtract (e, e') -> Num.sub_num (evaluate' e) (evaluate' e')
| Multiply (e, e') -> Num.mult_num (evaluate' e) (evaluate' e')
| Divide (e, e') -> Num.div_num (evaluate' e) (evaluate' e')
| Exponentiate (e, e') -> Num.power_num (evaluate' e) (evaluate' e')
(* 7. Use the option type to deal with the problem that Division_by_zero may be raised
from the evaluate function *)
let safe_evaluate e =
try
Ok (evaluate' e)
with
| err -> Error err