[Haskell-cafe] Short circuiting and the Maybe monad

John Hamilton jlhamilton at gmail.com
Mon May 12 18:09:44 EDT 2008


I'm trying to understand how short circuiting works with the Maybe monad.
Take the expression n >>= f >>= g >>= h, which can be written as
(((n >>= f) >>= g) >>= h) because >>= is left associative.  If n is
Nothing, this implies that (n >>= f) is Nothing, and so on, each nested
sub-expression easily evaluating to Nothing, but without there being a
quick way to short circuit at the beginning.

Now take the example

   do x <- xs
      y <- ys
      z <- zs
      return (x, y, z)

which I believe desugars like

    xs >>= (\x -> ys >>= (\y -> zs >>= (\z -> return (x, y, z))))

Here the associativity of >>= no longer matters, and if xs is Nothing the
whole expression can quickly be determined to be Nothing, because Nothing
>>= _ = Nothing.  Am I looking at this correctly?

- John


More information about the Haskell-Cafe mailing list