
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
Think of a computation in the State monad, a predicate will probably want to consult that state.
Matthew Cox
----- Original Message -----
From: Matthew Cox
To:

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
participants (2)
-
dons@cse.unsw.edu.au
-
Matthew Cox