-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter2.hs
40 lines (35 loc) · 1.45 KB
/
Chapter2.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
38
39
40
module Chapter2 (emptyEnv, extendEnv, applyEnv) where
---2.1
--Unary Representation
zero = []
isZero [] = True
isZero _ = False
successor xs = True : xs
predecessor (x:xs) = xs
--Interal Number Representation
zero' = 0
isZero' 0 = True
isZero' _ = False
successor' x = x + 1
predecessor' x = x - 1
---2.2.2
data Env = Empty | ExtendedEnv Identifier Value Env
deriving (Read, Show, Eq, Ord)
type Identifier = String
data Value = Ident Identifier | IntVal Integer | BoolVal Bool | CharVal Char | List [Value] | Function [Value] Value
deriving (Read, Show, Eq, Ord)
emptyEnv' = Empty
extendEnv' = ExtendedEnv
applyEnv' Empty ident = error ("No binding for " ++ ident ++ " found")
applyEnv' (ExtendedEnv a b env) ident
| a == ident = b
| otherwise = applyEnv' env ident
---2.2.3
emptyEnv :: (Eq a, Show a) => a -> b
emptyEnv = \x -> error ("No binding for " ++ show x ++ " found")
extendEnv :: (Eq a, Show a) => a -> b -> (a -> b) -> (a -> b)
extendEnv i v env = \x -> if x==i then v else (env x)
applyEnv :: (Eq a, Show a) => (a -> b) -> a -> b
applyEnv env ident = env ident
---2.3 - 2.5
--We already know about datatypes and parsing lambda expressions, see my Lambda project for more :)