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

Henk-Jan van Tuyl hjgtuyl at chello.nl
Thu Nov 23 17:22:36 EST 2006


You can use 'when' or 'unless' from the module Control.Monad, but they  
each have only one branch, see:
http://members.chello.nl/hjgtuyl/tourdemonad.html#unless
and
http://members.chello.nl/hjgtuyl/tourdemonad.html#when

You can create a monadic 'if' like this (in an interactive session):
Control.Monad> let ifM p a b = do { p' <- p; if p' then return a else  
return b } in ifM (Just True) 1 2
Just 1

Met vriendelijke groet,
Henk-Jan van Tuyl


--
  On Thu, 23 Nov 2006 22:34:49 +0100, Dougal Stanton <ithika at gmail.com>  
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:
>
>> do bool <- doesFileExist filename
>>    if bool
>>    then sth
>>    else sth'
>
> Is there a more compact way of writing that? Something akin to:
>
>> condM (doesFileExist filename) (sth) (sth')
>
> Or maybe there's a more sensible way of doing the above that I've
> missed. I seem to use endless alternate conditions sometimes and there's
> bound to be a better way.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



-- 
http://Van.Tuyl.eu/
--

Using Opera's revolutionary e-mail client:
https://secure.bmtmicro.com/opera/buy-opera.html?AID=789433



More information about the Haskell-Cafe mailing list