-
Notifications
You must be signed in to change notification settings - Fork 0
/
aufgabe12_4.hs
37 lines (31 loc) · 1.08 KB
/
aufgabe12_4.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
33
34
35
36
37
-- Aufgabe 12.4
-- a)
data Expr = TermType Term | NTermType Term | Sum Expr Term | Dif Expr Term deriving Show
data Term = FactorType Factor | Prod Term Factor deriving Show
data Factor = Lit Int | ExprType Expr deriving Show
-- b)
-- Usage in GHCi?
-- Simple example:
-- evalTerm (Prod (FactorType (Lit 4)) (Lit 1))
--
-- We can see, Prod needs some arguments, that are to be found in Term
-- or Factor algebraic types respectivelly. So we need to give them to
-- the function
--
-- Advanced:
-- evalTerm (Prod (FactorType (Lit 4)) (ExprType (TermType (FactorType
-- (Lit 1)))))
--
-- That is actually the same, as the example before, we just do a
-- round-trip trough other algebraic types
evalExpr :: Expr -> Int
evalExpr (TermType a) = evalTerm a
evalExpr (NTermType a) = - (evalTerm a)
evalExpr (Sum a b) = (evalExpr a) + (evalTerm b)
evalExpr (Dif a b) = (evalExpr a) - (evalTerm b)
evalFactor :: Factor -> Int
evalFactor (Lit a) = a
evalFactor (ExprType a) = evalExpr a
evalTerm :: Term -> Int
evalTerm (FactorType a) = evalFactor a
evalTerm (Prod a b) = (evalTerm a) * (evalFactor b)