[Haskell-cafe] local loops

Jon Cast jcast at ou.edu
Tue Dec 14 17:04:40 EST 2004


Per Larsson <per at L4i.se> wrote:
> I often use local loops in monadic code, e.g.
> 
>     main = do
>       ...
>      let loop = do 
>               ...
>               if cond then loop else return () 
>      loop
> 
> It seems that I can encode this idiom slightly more concise with the 'fix' 
> operator (from Control.Monad.Fix), i.e.
> 
> main = do
>    ...
>    fix (\loop -> do 
>       ...
>       if ...)
> 
> But is it really semantically equivalent? Is it as efficient? 
> 
> Per

It is semantically equivalent.  I don't know if it is as efficient;
looking at the source code, I would expect GHC to inline fix anywhere it
occurs, which yields your original version.

Jonathan Cast


More information about the Haskell-Cafe mailing list