Re: [Haskell-cafe] Nested tests [Code walking off the right edge of the screen]
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Nested tests [Code walking off the right edge of the screen]
Sebastian Fischer wrote: using Control.Monad.Error: either (hPutStrLn stderr) return =<< runErrorT $ do d1x <- lift $ doesDirectoryExist d1 unless d1x $ fail "Directory " ++ d1 ++ " not found." f1x <- lift $ doesFileExist (d1 f1) unless f1x $ fail "File " ++ f1 ++ " not found." d2x <- lift $ doesDirectoryExist d2 unless d2x $ fail "Directory " ++ d2 ++ " not found." f2x <- lift $ doesFileExist (d2 f2) unless f2x $ fail "File " ++ f2 ++ " not found." lift $ doStuff d1 d2 f1 f2 So... essentially, run the ErrorT transformer on top of IO to do the error handling? That seems like a much nicer solution - if I can get it to work. (In general, any code involving monad transformers tends to fail with elaborate type checker failures. Typically it takes me hours to figure out why... Maybe somebody has written a tutorial on this stuff?) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Nested tests [Code walking off the right edge of the screen]
On Jun 21, 2009, at 11:52 AM, Andrew Coppin wrote: In a similar vein: 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." using Control.Monad.Error: either (hPutStrLn stderr) return =<< runErrorT $ do d1x <- lift $ doesDirectoryExist d1 unless d1x $ fail "Directory " ++ d1 ++ " not found." f1x <- lift $ doesFileExist (d1 f1) unless f1x $ fail "File " ++ f1 ++ " not found." d2x <- lift $ doesDirectoryExist d2 unless d2x $ fail "Directory " ++ d2 ++ " not found." f2x <- lift $ doesFileExist (d2 f2) unless f2x $ fail "File " ++ f2 ++ " not found." lift $ doStuff d1 d2 f1 f2 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 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.) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Nested tests [Code walking off the right edge of the screen]
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, Bulatmailto:bulat.zigans...@gmail.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe