-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseful_datatypes.sml
More file actions
35 lines (25 loc) · 959 Bytes
/
useful_datatypes.sml
File metadata and controls
35 lines (25 loc) · 959 Bytes
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
(* Useful DataTypes *)
datatype suit = Club | Daimond | Heart | Spade
datatype rank = Jack | Queen | King | Ace | Num of int
datatype id = Student of int
| Name of string * (string option) * string
(* using datatype for tree like structure *)
datatype exp = Constant of int
| Negate of exp
|Add of exp * exp
| Multiply of exp * exp
fun eval e =
case e of
Constant i => i
| Negate e2 => ~ (eval e2)
| Add(e1,e2) => (eval e1) + (eval e2)
| Multiply(e1,e2) => (eval e1) * (eval e2)
fun number_of_adds e = (* exp -> int *)
case e of
Constant i => 0
| Negate e2 => number_of_adds e2
| Add(e1,e2) => 1 + number_of_adds e1 + number_of_adds e2
| Multiply(e1,e2) => number_of_adds e1 + number_of_adds e2
val example_exp : exp = Add (Constant 19, Negate (Constant 4))
val expamle_ans : int = eval example_exp
val example_addcount = number_of_adds(Multiply(example_exp,example_exp))