[Haskell-cafe] Pesky monads...

Donald Bruce Stewart dons at cse.unsw.edu.au
Sat May 19 22:54:31 EDT 2007


matt:
> It occurred to me that the predicate will generally be a monadic function itself, so here's a
> refined version:
> 
>  :: Monad m => (a -> m Bool) -> (a -> m a) -> a -> m a
> untilM pred f x = do c <- pred x
>                      if c then return x
>                           else f x >>= untilM pred f

Here's a cute example of a loop from xmonad:

    fix $ \again -> do
        more <- checkMaskEvent d enterWindowMask p
        when more again 

But maybe Spencer was just being funny when he wrote that.

-- Don


More information about the Haskell-Cafe mailing list