Objectives Monads Other Monads
Monads
- Dr. Mattox Beckman
Monads Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation
Objectives Monads Other Monads Monads Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives Monads Other Monads Objectives Describe the problem that monads attempt to solve. Know
Objectives Monads Other Monads
Objectives Monads Other Monads
Objectives Monads Other Monads
Objectives Monads Other Monads
1 instance Monad Maybe where 2
3 instance Monad [] where 4
5 instance Monad (Either a) where 6
Objectives Monads Other Monads
1 Nothing
2 (Just a) >>= f = f a 3
Objectives Monads Other Monads
1 inc1 a = a + 1 2 r1 = inc1 <$> Just 10 -- result: Just 11 3 r2 = inc1 <$> Nothing -- result: Nothing
1 inc2 a = Just (a+1) 2 recip a | a =/ 0
3
Objectives Monads Other Monads
Objectives Monads Other Monads
1 minc x = x >>= (\xx -> return (xx + 1)) 2 madd a b = a >>= (\aa -> 3
4 -- but wait!!!
1 liftM f a = a >>= (\aa -> return (f aa)) 2 liftM2 f a b = a >>= (\aa -> 3
Objectives Monads Other Monads
1 minc = liftM inc 2 madd = liftM2 add 3 msub = liftM2 sub 4 mdiv a b = a >>= (\aa -> 5
6
7
Objectives Monads Other Monads
1 instance Monad Maybe where 2
3 4
5
6 7
Objectives Monads Other Monads
Objectives Monads Other Monads
1 instance Monad [] where 2
3 4
5
6 7
Objectives Monads Other Monads