blob: 7a1b5e5c8f625be39e5e9f491df8f933319977be (
plain)
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
|
module Expr where
import Data.List
data Atom
= Rational Float
| Imaginary Float
| Matrix [[Expr]]
deriving (Eq)
data Expr
= EAtom Atom
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
| Mod Expr Expr
| Exp Expr Expr
| Dot Expr Expr
| Variable String
| Function String Expr
deriving (Eq)
instance Show Expr where
show (EAtom a) = show a
show (Add e1 e2) = show e1 ++ " + " ++ show e2
show (Sub e1 e2) = show e1 ++ " - " ++ show e2
show (Mul e1 e2) = show e1 ++ " * " ++ show e2
show (Div e1 e2) = show e1 ++ " / " ++ show e2
show (Mod e1 e2) = show e1 ++ " % " ++ show e2
show (Exp e1 e2) = show e1 ++ " ^ " ++ show e2
show (Dot e1 e2) = show e1 ++ " ** " ++ show e2
show (Variable name) = name
show (Function name e) = name ++ "(" ++ show e ++ ")"
instance Show Atom where
show (Rational r) = show r
show (Imaginary i) = show i ++ "i"
show (Matrix m) = intercalate "\n" (map showRow m)
where showRow r = "[ " ++ intercalate ", " (map show r) ++ " ]"
|