Skip to content

Instantly share code, notes, and snippets.

@xfire
Created July 27, 2011 11:26
Show Gist options
  • Save xfire/1109175 to your computer and use it in GitHub Desktop.
Save xfire/1109175 to your computer and use it in GitHub Desktop.
ErrorT usage
import Control.Monad.Error
execAna :: Int -> ErrorT String IO (Int, Int)
execAna 0 = throwError "ERROR"
execAna n = return (n - 1, n + 1)
report :: Either String () -> IO ()
report (Left err) = putStrLn $ "failed: " ++ err
report _ = putStrLn "complete"
errorRun :: Int -> IO ()
errorRun n = do
err <- runErrorT $ do
(a, b) <- execAna n
liftIO $ putStrLn $ show a
liftIO $ putStrLn $ show b
report err
main :: IO ()
main = do
errorRun 1 -- should work and return 0 and 2
errorRun 0 -- should produce an error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment