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

Udo Stenzel u.stenzel at web.de
Thu Nov 23 18:57:00 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')

I'd suggest

  cond t f True  = t
  cond t f False = f

and then you use it like

  doesFileExist filename >>= cond sth sth'

I find it strange that we have 'maybe' for 'Maybe', 'either' for
'Either', 'foldr' for '[]', but a special syntactic form for 'Bool'.
Why is there no 'cond' in the Prelude?


-Udo
-- 
There is no snooze button on a cat who wants breakfast.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20061124/62f6547d/attachment.bin


More information about the Haskell-Cafe mailing list