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

Sebastiaan Visser sfvisser at cs.uu.nl
Sun Jun 21 08:54:23 EDT 2009


On Jun 21, 2009, at 2:36 PM, Sebastian Fischer wrote:
> On Jun 21, 2009, at 11:52 AM, Andrew Coppin wrote:
> ...
>
> When using
>
> failUnless boolAction message = lift boolAction >>= (`unless`fail  
> message)
>
> the code becomes
>
> either (hPutStrLn stderr) return =<< runErrorT $
>  do failUnless (doesDirectoryExist d1)     $ "Directory " ++ d1 ++ "  
> not found."
>     failUnless (doesFileExist (d1 </> f1)) $ "File " ++ f1 ++ " not  
> found."
>     failUnless (doesDirectoryExist d2)     $ "Directory " ++ d2 ++ "  
> not found."
>     failUnless (doesFileExist (d2 </> f2)) $ "File " ++ f2 ++ " not  
> found."
>     lift $ doStuff d1 d2 f1 f2

Or make is somewhat prettier (imho) using infix:

orError boolAction message = lift boolAction >>= (`unless`fail message)

either (hPutStrLn stderr) return =<< runErrorT $
  do doesDirectoryExist d1     `orError` ("Directory " ++ d1 ++ " not  
found.")
     doesFileExist (d1 </> f1) `orError` ("File " ++ f1 ++ " not  
found.")
     doesDirectoryExist d2     `orError` ("Directory " ++ d2 ++ " not  
found.")
     doesFileExist (d2 </> f2) `orError` ("File " ++ f2 ++ " not  
found.")
     lift $ doStuff d1 d2 f1 f2

> It's similar to Claus's proposal to use MaybeT with additional  
> support for error messages.
>
> Sebastian
>
>
> -- 
> Underestimating the novelty of the future is a time-honored tradition.
> (D.G.)

--
Sebastiaan Visser



More information about the Haskell-Cafe mailing list