[Haskell-beginners] How to unnest "do"

Daniel Trstenjak daniel.trstenjak at gmail.com
Mon Jan 28 15:27:44 CET 2013


Hi Emmanuel,

On Sun, Jan 27, 2013 at 09:46:30PM +0100, Emmanuel Touzery wrote:
> Thank you. I thought it might be, but it isn't exactly intuitive for me at
> this point. I'll read some more about that monad.

Sometimes it's hard to tell if Haskell is the most beautiful language
or the most abstract nonsense. ;)


Let's look at the Monad instance for the function (r -> a):

instance Monad ((->) r) where
   -- return :: a -> (r -> a)
   return a = \_ -> a

   -- (>>=) :: (r -> a) -> (a -> r -> b) -> (r -> b)
   left >>= right = \r -> right (left r) r 


'return' creates a function ignoring its argument and just returning 'a'.

'>>=' creates a function with the argument 'r'. The function 'left' is
called with 'r' and the function 'right' is called with the result of
'left' and 'r'.


Now let's look at the 'sequence' function:

sequence ms = foldr k (return []) ms
   where
      k m ms = do
         x  <- m
         xs <- ms
         return (x : xs)


It's easier to see what happens if we rewrite 'k':

k m ms = m >>= (\x -> (ms >>= \xs -> return (x : xs)))


We saw that '>>=' creates a function with one argument, that argument
is the String containing the file contents, 'x' is the return value
of one "sequenced" function which is combined (:) with the previous
ones.

At the end we have the function (String -> [String]).


Greetings,
Daniel



More information about the Beginners mailing list