[Haskell-cafe] least fixed points above something

Neil Mitchell ndmitchell at gmail.com
Thu Mar 19 12:21:53 EDT 2009


> I've used a similar function myself, but why write it in such a complicated
> way? How about
>
> lfp :: Eq a => (a -> a) -> a -> a
> lfp f x
>  | f x == x = x
>  | otherwise = lfp f (f x)

I've used a similar function too, but your version computes f x twice
per iteration, I wrote mine as:

fix :: Eq a => (a -> a) -> a -> a
fix f x = if x == x2 then x else fix f x2
    where x2 = f x

I find this fix much more useful than the standard fix.

Thanks

Neil


More information about the Haskell-Cafe mailing list