[Haskell-cafe] Nested tests [Code walking off the right edge of the screen]

Bulat Ziganshin bulat.ziganshin at gmail.com
Sun Jun 21 06:04:31 EDT 2009


Hello Andrew,

Sunday, June 21, 2009, 1:52:22 PM, you wrote:

>   d1x <- doesDirectoryExist d1
>   if d1x
>     then do
>       f1x <- doesFileExist (d1 </> f1)
>       if f1x
>         then do
>           d2x <- doesDirectoryExist d2
>           if d2x
>             then do
>               f2x <- doesFileExist (d2 </> f2)
>               if f2x
>                 then do_stuff d1 d2 f1 f2
>                 else hPutStrLn stderr $ "File " ++ f2 ++ " not found."
>               else hPutStrLn stderr $ "Directory " ++ d2 ++ " not found."
>             else hPutStrLn stderr $ "File " ++ f1 ++ " not found."
>           else hPutStrLn stderr $ "Directory " ++ d1 ++ " not found."

d1x <- doesDirectoryExist d1
if not d1x  then hPutStrLn stderr $ "Directory " ++ d1 ++ " not found."  else do
f1x <- doesFileExist (d1 </> f1)
if not f1x  then hPutStrLn stderr $ "File " ++ f1 ++ " not found."  else do
d2x <- doesDirectoryExist d2
if not d2x  then hPutStrLn stderr $ "Directory " ++ d2 ++ " not found."  else do
f2x <- doesFileExist (d2 </> f2)
if not f2x  then hPutStrLn stderr $ "File " ++ f2 ++ " not found."  else do
do_stuff d1 d2 f1 f2

or, with a little additional combinators:

ifM (not ==<< doesDirectoryExist d1)      (hPutStrLn stderr $ "Directory " ++ d1 ++ " not found.") $ do
ifM (not ==<< doesFileExist (d1 </> f1))  (hPutStrLn stderr $ "File " ++ f1 ++ " not found.")      $ do
ifM (not ==<< doesDirectoryExist d2)      (hPutStrLn stderr $ "Directory " ++ d2 ++ " not found.") $ do
ifM (not ==<< doesFileExist (d2 </> f2))  (hPutStrLn stderr $ "File " ++ f2 ++ " not found.")      $ do
do_stuff d1 d2 f1 f2


-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list