-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExArith.hs
33 lines (25 loc) · 846 Bytes
/
ExArith.hs
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
module ExArith where
data Arith = Atom Integer
| Plus Arith Arith
| Times Arith Arith
instance (Show Arith) where
show (Atom n) = show n
show (Plus s t) = showOp " + " s t
show (Times s t) = showOp " * " s t
showOp :: Show s => String -> s -> s -> String
showOp op s t = parenthesize $ show s ++ op ++ show t
parenthesize :: String -> String
parenthesize s = "(" ++ s ++ ")"
-- example expressions
ex1 = (Atom 23) `Plus` (Atom 2)
ex2 = (Atom 7) `Times` ((Atom 7) `Plus` ((Atom 2) `Times` (Atom 8)))
ex3 = Times ex1 ex2
ex4 = ex3 `Plus` ex1
-- val evaluates an expression and returns its value
val :: Arith -> Integer
val (Atom n) = n
val (Plus s t) = val s + val t
val (Times s t) = val s * val t
-- step should make only 1 step of calculation on a given Arith
step :: Arith -> Arith
step = undefined