-
Notifications
You must be signed in to change notification settings - Fork 23
/
lab11-reader.hs
55 lines (40 loc) · 1.2 KB
/
lab11-reader.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---Monada Reader
newtype Reader env a = Reader {runReader :: env -> a}
instance Monad (Reader env) where
return x = Reader (\_ -> x)
ma >>= k = Reader f
where
f env =
let a = runReader ma env
in runReader (k a) env
instance Applicative (Reader env) where
pure = return
mf <*> ma = do
f <- mf
a <- ma
return (f a)
instance Functor (Reader env) where
fmap f ma = pure f <*> ma
ask :: Reader env env
ask = Reader id
local :: (r -> r) -> Reader r a -> Reader r a
local f ma = Reader $ (\r -> (runReader ma) (f r))
-- Reader Person String
data Person = Person {name :: String, age :: Int}
-- Câteva persoane date ca exemplu
persoana1 :: Person
persoana1 = Person "ada" 20
persoana2 :: Person
persoana2 = Person "ion" 19
showPersonN :: Person -> String
showPersonN p = "NAME:" ++ name p
showPersonA :: Person -> String
showPersonA p = "AGE:" ++ show (age p)
showPerson :: Person -> String
showPerson p = "(" ++ showPersonN p ++ "," ++ showPersonA p ++ ")"
mshowPersonN :: Reader Person String
mshowPersonN = Reader showPersonN
mshowPersonA :: Reader Person String
mshowPersonA = Reader showPersonA
mshowPerson :: Reader Person String
mshowPerson = Reader showPerson