[Haskell-cafe] Equivalent of if/then/else for IO Bool?

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Nov 23 17:22:55 EST 2006


Hello Dougal,

Friday, November 24, 2006, 12:34:49 AM, you wrote:

> Is there some sort of equivalent of the if/then/else construct for use
> in the IO monad? For instance the following can get quite tedious:

just a list of my control structures:

whenM cond action = do
  allow <- cond
  when allow
    action

unlessM = whenM . liftM not

whenJustM x action  =  x >>= maybe (return Nothing) action

whenJustM_ x action  =  x >>= maybe (return ()) (action .>> return ())

foreach = flip mapM

for = flip mapM_

on = flip when

repeat_foreverM action = do
  action
  repeat_foreverM action

repeat_whileM inp cond out = do
  x <- inp
  if (cond x)
    then do out x
            repeat_whileM inp cond out
    else return x

repeat_untilM action = do
  done <- action
  when (not done) $ do
    repeat_untilM action

doChunks size chunk action =
  case size of
    0 -> return ()
    _ -> do let n = minI size chunk
            action (fromIntegral n)
            doChunks (size-n) chunk action

recursiveM action x  =  action x >>= mapM_ (recursiveM action)




-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list