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

Luis F. Araujo luis at arjox.org
Fri Nov 24 00:17:25 EST 2006


Dougal Stanton 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.
>
>   

I don't know any existing function like this in the current libs.

Here i paste probably a possible implementation for one.

if' :: (Monad m) => m Bool -> m b -> m b -> m b
if' mb mt mf = mb >>= if''
                        where if'' b = if b then mt else mf

Regards,



More information about the Haskell-Cafe mailing list