[Haskell-cafe] new recursive do notation (ghc 6.12.x) spoils
layout
Dan Doel
dan.doel at gmail.com
Sun Jun 20 21:44:34 EDT 2010
On Sunday 20 June 2010 9:24:54 pm Alexander Solla wrote:
> Why can't you just use let notation do deal with the recursion? I
> thought "lets" in do blocks were just a little bit of syntactic sugar
> for "regular" let expressions, which do allow mutual recursion. I
> could be totally wrong though. I'm thinking of something like:
>
> do a <- getChar
> let b = c >>= return . f
> let c = b >>= return . g
> c >>= putChar
> b
This is not what recursive do does. For instance:
do rec l <- (:l) <$> getChar
return l
will read one character, and return an infinite list with that character as
the elements. By contrast:
let l = (:) <$> getChar <*> l in l
will, for one, never complete, and two, call getChar an infinite number of
times.
In general, it is based on the fixed point:
mfix :: (MonadFix m) => (a -> m a) -> m a
where the function given may not be separable into a pure function and return
at all, which is what would be necessary to implement using let, via the
identity:
mfix (return . f) = return (fix f)
The implementation of mfix is specific to the 'm' in question, not something
you can write for all monads.
-- Dan
More information about the Haskell-Cafe
mailing list