datatype Expr ADD of Expr Expr SUB of Expr Expr MUL of Expr Expr DIV o

 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
datatype Expr
= ADD of Expr*Expr
| SUB of Expr*Expr
| MUL of Expr*Expr
| DIV of Expr*Expr
| NUMBER of real
fun toString (ADD (n1,n2)) = "(" ^ (toString n1) ^ "+" ^ (toString n2) ^ ")"
| toString (SUB (n1,n2)) = "(" ^ (toString n1) ^ "-" ^ (toString n2) ^ ")"
| toString (MUL (n1,n2)) = "(" ^ (toString n1) ^ "*" ^ (toString n2) ^ ")"
| toString (DIV (n1,n2)) = "(" ^ (toString n1) ^ "/" ^ (toString n2) ^ ")"
| toString (NUMBER s) = Real.toString (s)
fun evaluate (ADD (n1, n2)) = (evaluate n1) + (evaluate n2)
| evaluate (SUB (n1, n2)) = (evaluate n1) - (evaluate n2)
| evaluate (MUL (n1, n2)) = (evaluate n1) * (evaluate n2)
| evaluate (DIV (n1, n2)) = (evaluate n1) / (evaluate n2)
| evaluate (NUMBER s) = s
fun change (ADD (n1, n2)) = ADD((change n2), (change n1))
| change (SUB (n1, n2)) = SUB((change n1), (change n2))
| change (MUL (n1, n2)) = MUL((change n2), (change n1))
| change (DIV (n1, n2)) = DIV((change n1), (change n2))
| change (NUMBER s) = NUMBER s
fun countOperations (ADD (n1, n2)) = countOperations(n1) + countOperations(n2) + 1
| countOperations (MUL (n1, n2)) = countOperations(n1) + countOperations(n2) + 1
| countOperations (SUB (n1, n2)) = countOperations(n1) + countOperations(n2) + 1
| countOperations (DIV (n1, n2)) = countOperations(n1) + countOperations(n2) + 1
| countOperations (NUMBER (n1)) = 0
fun processTree (f,g,h,d,n) (ADD (n1,n2)) = f (processTree (f,g,h,d,n) n1, processTree (f,g,h,d,n) n2)
| processTree (f,g,h,d,n) (MUL (n1,n2)) = g (processTree (f,g,h,d,n) n1, processTree (f,g,h,d,n) n2)
| processTree (f,g,h,d,n) (SUB (n1,n2)) = h (processTree (f,g,h,d,n) n1, processTree (f,g,h,d,n) n2)
| processTree (f,g,h,d,n) (DIV (n1,n2)) = d (processTree (f,g,h,d,n) n1, processTree (f,g,h,d,n) n2)
| processTree (f,g,h,d,n) (NUMBER s) = n s
val toString' = processTree (fn(n1,n2) => "(" ^ n1 ^ "+" ^ n2 ^ ")", fn(n1,n2) => "(" ^ n1 ^ "*" ^ n2 ^ ")", fn(n1,n2) => "(" ^ n1 ^ "-" ^ n2 ^ ")", fn(n1,n2) => "(" ^ n1 ^ "/" ^ n2 ^ ")" , fn s => "" ^ Real.toString s ^ "")
val countOperations' = processTree (fn(n1,n2) => n1 + n2 + 1, fn(n1,n2) => n1 + n2 + 1, fn(n1,n2) => n1 + n2 + 1, fn(n1,n2) => n1 + n2 + 1, fn n1 => 0)
val evaluate' = processTree (fn(n1,n2) => n1 + n2, fn(n1,n2) => n1 * n2, fn(n1,n2) => n1 - n2, fn(n1,n2) => n1 / n2, fn n1 => n1)
val change' = processTree (fn(n1,n2) => ADD(n2,n1), fn(n1,n2) => MUL(n2,n1), fn(n1,n2) => SUB(n1,n2), fn(n1,n2) => ADD(n1,n2), fn n1 => NUMBER(n1))
val testTree =
ADD(
NUMBER(5.0), MUL(
NUMBER(6.0), DIV(
DIV(
NUMBER(8.0), NUMBER(4.0)),
NUMBER(0.0))
)
)
fun main () = print (Real.toString (evaluate(testTree)))
fun main1 () = print (Int.toString (countOperations'(testTree)))
fun main2 () = print (toString' (testTree))
fun main3 () = print (Real.toString (evaluate'(testTree)))
fun main4 () = print (toString' (change (testTree)))