a monadic if or case?

David Roundy droundy@abridgegame.org
Thu, 13 Feb 2003 09:23:27 -0500


Hello all.  I was just thinking that there ought to be a better way to
write the following code.  It seems to be a common case that within a 'do'
I bind a variable that I only intend to use once, in an if or case
statement.  It occurred to me that there ought to be a better way to do
this.  For example, it seems like the following code:

whatisit :: String -> IO String
whatisit f = do
  isdir <- doesDirectoryExist f
  if isdir
     then return "dir"
     else do isfile <- doesFileExist f
             if isfile
                then return "file"
                else return "nothing"

could be replaced by something like:

whatisit :: String -> IO String
whatisit f = do
  ifM doesDirectoryExist f
      then return "dir"
      else ifM doesFileExist f
           then return "file"
           else return "nothing"

Is there any way I could do something like this?
-- 
David Roundy
http://civet.berkeley.edu/droundy/