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

Lambda calculus, Type theory, Formal semantics, Program analysis

Prev     Next     All lectures           Code


f1 x = if (x == "hello") then Nothing 
       else (Just 10)

f2 x = if (x == 'a') then Nothing 
       else (Just 9)

add x y = do
  x' <- f1 x
  y' <- f2 y
  return (x' + y')
  -   The same example as in the previous slide but now f1 and f2 return Monads. This is more likely what is desired - if there is a failure in f1 or f2 it should be propagated to add so a computation is not even attempted. Try this:
 Prelude> add "hello" 's'
 Prelude> add "bye" 's'
 Just 19
The type of add is now this:
 add :: Num b => [Char] -> Char -> Maybe b