
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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe