loop-while-1.0.0: A monad transformer supporting various styles of while loop



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 Source

A monad transformer for easier looping. See loop and while.

loop :: Monad m => LoopWhileT m a -> m ()Source

Runs the given action in a loop, executing it repeatedly until a while statement inside it has a False condition. If you use loop without while, the effect is the same as forever.

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.