[Haskell-cafe] ANNOUNCE: control-monad-loop 0.1

Joey Adams joeyadams3.14159 at gmail.com
Mon Jul 9 07:38:06 CEST 2012


This package provides imperative-style loops supporting "continue" and
"break".  For example:

    import Control.Monad
    import Control.Monad.IO.Class
    import Control.Monad.Trans.Loop
    import Control.Monad.Trans.Class

    main :: IO ()
    main =
        foreach [0..] $ \i ->
        foreach [0..] $ \j -> do
            when (j == 0) $
                continue    -- skip to next iteration
            when (j >= 5) $
                exit        -- exit the loop
            when (i >= 5) $
                lift exit   -- exit the outer loop by calling 'exit'
in the parent monad
            liftIO $ print (i, j)

It works by having the loop body run under the LoopT monad
transformer, which provides early exit functions 'continue' and
'exit'.  Functions like 'foreach' and 'while' run a LoopT callback,
passing it continuations defining 'continue' and 'exit'.

http://hackage.haskell.org/package/control-monad-loop



More information about the Haskell-Cafe mailing list