[Haskell-cafe] Short circuiting and the Maybe monad

Abhay Parvate abhay.parvate at gmail.com
Tue May 13 01:13:33 EDT 2008


Yes, I had always desired that the operator >>= should have been right
associative for this short cut even when written without the 'do' notation.

On Tue, May 13, 2008 at 3:39 AM, John Hamilton <jlhamilton at gmail.com> wrote:

> 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
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080513/3e2d4f68/attachment.htm


More information about the Haskell-Cafe mailing list