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

Jason Dagit dagit at eecs.oregonstate.edu
Thu Nov 23 17:00:33 EST 2006


On 11/23/06, 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')

Maybe there is one built in but don't know it or see anything in
hoogle. I'd use something like the following (and then just make it a
standard part of the libraries I use personally):

import Control.Monad

if' b t e = if b then t else e
ifM = liftM3 if'

which gives ifM :: (Monad m) => m Bool -> m t -> m t -> m t

HTH,
Jason


More information about the Haskell-Cafe mailing list