[Haskell-cafe] Cute code [was: The C Equiv of != in Haskell miscommunication thread]

Donald Bruce Stewart dons at cse.unsw.edu.au
Tue May 29 09:33:34 EDT 2007


vincent:
> i see that the definition of fix (from Control.Monad.Fix) could not be
> any simpler:
> 
> > fix f = let x = f x in x
> 
> same goes for the type:
> 
> Prelude> :t Control.Monad.Fix.fix
> Control.Monad.Fix.fix :: (a -> a) -> a
> 
> it's just that i find it difficult to get concrete intellectual mileage
> out of it.
> i can reproduce results for specific examples (and even manipulate them
> a bit), but feel like i'm missing something deep yet simple. say, i
> would not know where and how to apply it. so obviously true
> understanding is still missing. reminds me of my first encounters with
> $H \psi = E \psi$. ;-)
> 
> most likely, i should just more carefully read the references i cited
> myself ;-)
> 
> anyhow. if someone has a "pedestrian's guide to the fixed point
> operator" lying around, a link would be much appreciated.

I use it when I need a local loop expression, maybe once every couple of
months. A real world example from xmonad,

 f = fix $ \again -> do
            more <- checkMaskEvent d enterWindowMask ev
            when more again 

That is, keep sucking up X events till there's no 'more'.
Of course, you can always just name your loop with 'where' and use that.

 f = go
   where
     go = do
        more <- checkMaskEvent d enterWindowMask ev
        when more go

TMTOWTDI with recursion :-)

-- Don


More information about the Haskell-Cafe mailing list