-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch22reader.hs
174 lines (122 loc) · 3.48 KB
/
ch22reader.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{-# LANGUAGE InstanceSigs #-}
module Chapter22Reader where
import Control.Applicative
import Data.Char
import Data.Maybe
-- Non-exercices (just composing functions)
hurr :: Num a => a -> a
hurr = (*2)
durr :: Num a => a -> a
durr = (+10)
m :: Num a => a -> a
m = hurr . durr
m' :: Num a => a -> a
m' = fmap hurr durr
m2 :: Num a => a -> a
m2 = (+) <$> hurr <*> durr
m2' :: Num a => a -> a
m2' = liftA2 (+) hurr durr
-- Short Exercise: Warming up (page 873)
cap :: [Char] -> [Char]
cap = map toUpper
rev :: [Char] -> [Char]
rev = reverse
composed :: [Char] -> [Char]
composed = cap . rev
fmapped :: [Char] -> [Char]
fmapped = fmap cap rev
tupled :: [Char] -> ([Char], [Char])
tupled = liftA2 (,) cap rev
tupled' :: [Char] -> ([Char], [Char])
tupled' = do
x' <- cap
y' <- rev
return $ (,) x' y'
tupled'' :: [Char] -> ([Char], [Char])
tupled'' = cap >>= (\x' -> rev >>= (\y' -> return $ (,) x' y'))
-- Exercise: Ask (page 879)
newtype Reader r a =
Reader { runReader :: r -> a }
ask :: Reader a a
ask = Reader id
-- Exercise: Reading Comprehension (page 882)
newtype HumanName = HumanName String deriving (Eq, Show)
newtype DogName = DogName String deriving (Eq, Show)
newtype Address = Address String deriving (Eq, Show)
data Person = Person
{ humanName :: HumanName
, dogName :: DogName
, address :: Address
} deriving (Eq, Show)
data Dog = Dog
{ dogsName :: DogName
, dogsAddress :: Address
} deriving (Eq, Show)
pers :: Person
pers = Person (HumanName "Ernesto") (DogName "Rocky") (Address "Minerva")
liftA2' :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
liftA2' f fa fb = f <$> fa <*> fb
asks :: (r -> a) -> Reader r a
asks = Reader
instance Functor (Reader r) where
fmap f (Reader ra) = Reader (f . ra)
instance Applicative (Reader r) where
pure :: a -> Reader r a
pure a = Reader $ const a
(<*>) :: Reader r (a -> b) -> Reader r a -> Reader r b
(Reader rab) <*> (Reader ra) = Reader $ \r -> rab r (ra r)
getDogR :: Reader Person Dog
getDogR = pure Dog <*> asks dogName <*> asks address
-- Exercise: Reader Monad (page 887)
instance Monad (Reader r) where
return = pure
(>>=) :: Reader r a -> (a -> Reader r b) -> Reader r b
(Reader ra) >>= aRb = Reader $ \r -> runReader (aRb (ra r)) r
getDogRM :: Reader Person Dog
getDogRM = do
dn <- asks dogName
ad <- asks address
return $ Dog dn ad
-- 22.11 Chapter Exercises (page 890)
x :: [Integer]
x = [1, 2, 3]
y :: [Integer]
y = [4, 5, 6]
z :: [Integer]
z = [7, 8, 9]
xs :: Maybe Integer
xs = lookup 3 $ zip x y
ys :: Maybe Integer
ys = lookup 6 $ zip y z
zs :: Maybe Integer
zs = lookup 4 $ zip x y
z' :: Integer -> Maybe Integer
z' n = lookup n $ zip x z
x1 :: Maybe (Integer, Integer)
x1 = liftA2 (,) xs ys
x2 :: Maybe (Integer, Integer)
x2 = liftA2 (,) ys zs
x3 :: Integer -> (Maybe Integer, Maybe Integer)
x3 = runReader $ pure (,) <*> asks z' <*> asks z'
summed :: Num c => (c, c) -> c
summed = uncurry (+)
bolt :: Integer -> Bool
bolt = liftA2 (&&) (>3) (<8)
main :: IO ()
main = do
print $ sequenceA [Just 3, Just 2, Just 1]
print $ sequenceA [x, y]
print $ sequenceA [xs, ys]
print $ summed <$> ((,) <$> xs <*> ys)
print $ fmap summed ((,) <$> xs <*> zs)
print $ bolt 7
print $ fmap bolt z
print $ sequenceA [(>3), (<8), even] 7
print $ and (sequA 6)
print $ sequA (fromMaybe 0 s')
print $ bolt (fromMaybe 0 ys)
print $ bolt (fromMaybe 0 $ z' 5)
sequA :: Integral a => a -> [Bool]
sequA = sequenceA [(>3), (<8), even]
s' :: Maybe Integer
s' = summed <$> ((,) <$> xs <*> ys)