20-CS-4003-001 Organization of Programming Languages Fall 2017
Monads

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code

Basics

f1 x = if (x == "hello") then 2 else 10

f2 x = if (x == 'a') then 4 else 9

add x y = do
  x' <- (Just (f1 x))
  y' <- (Just (f2 y))
  return (x' + y')
  -   The same example as in the previous slide but now add returns a Monad. Try this:
 Prelude> add "hello" 's'
 Just 11
The types are now these:
 add :: Num b => [Char] -> Char -> Maybe b
 f1 :: Num a => [Char] -> a
 f2 :: Num a => Char -> a