Page 1

Some reflections on Monads

Page 2

Motivating example

Instead of writing code like this:

number :: String -> Maybe (Integer,String)
number s = case span isDigit s of
             ([],_) -> Nothing
             (ds,r) -> Just (read ds,r)

addition :: String -> Maybe (Integer,String)
addition s = case number s of
               Just (n1,'+':r) -> case number r of
                                    Just (n2,r') -> Just (n1+n2,r')
                                    _ -> Nothing
               _ -> Nothing
    
multiplication :: String -> Maybe (Integer,String)
multiplication s = case number s of
                     Just (n1,'*':r) -> case number r of
                                          Just (n2,r') -> Just (n1*n2,r')
                                          _ -> Nothing
                     _ -> Nothing

calculation :: String -> Maybe (Integer,String)
calculation s = case addition s of
                  Nothing -> multiplication s
                  result -> result
Page 3
Motivating example

We can write code like this:

number         = read <$> oneOrMore (sat isDigit)
addition       = (+) <$> number <* char '+' <*> number
multiplication = (*) <$> number <* char '*' <*> number
calculation    = addition <|> multiplication
Page 4

Domain-specific languages

Page 5

Types for functions with effects (1)

Page 6

Types for functions with effects (2)

Page 7

Type classes related to monads

Page 8

The do notation

Page 9

Functor vs the do notation

Page 10

Applicative vs the do notation

Page 11

Further reading