A module containing a monad transformer for performing while loops. There is nothing here that can't be built using if-then-else, but it can allow you to express control more succinctly.
For example, here is a loop that executes until a certain time is reached:
loop $ do lift performAction t <- lift getTime while (t < endTime)
This would commonly be called a do-while loop in other languages. But the while statement does not have to be at the end of the loop:
loop $ do lift performAction t <- lift getTime while (t < endTime) lift $ putStrLn ("Cur Time: " ++ show t)
This is sometimes known as do-while-do. Note that like other monad transformers, you'll either need to explicitly lift the actions from the transformed monad, or use an mtl-style type-class to do so.
- data LoopWhileT m a
- loop :: Monad m => LoopWhileT m a -> m ()
- while :: Monad m => Bool -> LoopWhileT m ()
Documentation
data LoopWhileT m a Source
MonadTrans LoopWhileT | |
Monad m => Monad (LoopWhileT m) | |
Monad m => Functor (LoopWhileT m) | |
Monad m => Applicative (LoopWhileT m) | |
MonadIO m => MonadIO (LoopWhileT m) |
loop :: Monad m => LoopWhileT m a -> m ()Source
while :: Monad m => Bool -> LoopWhileT m ()Source
Continues executing the loop if the given value is True. If the value
is False, the loop is broken immediately, and control returns to the
caller of the loop
statement. Thus you can build pre-condition,
post-condition, and "mid-condition" loops, placing the condition wherever
you like.