Created
September 9, 2016 04:34
-
-
Save jmikkola/812b8e784db8a18b227b07b118106858 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad (liftM) | |
import Control.Monad.Except | |
type MyMonad = ExceptT String IO | |
successVal :: MyMonad Int | |
successVal = return 123 | |
errVal :: MyMonad Int | |
errVal = throwError "this is an error" | |
anotherSuccess :: Either String Integer | |
anotherSuccess = Right 34563456435623423423 | |
doStuff :: MyMonad () | |
doStuff = do | |
foo <- successVal | |
_ <- lift $ putStrLn $ show foo | |
baz <- ExceptT $ return anotherSuccess | |
_ <- lift $ putStrLn $ show baz | |
bar <- errVal | |
_ <- lift $ putStrLn $ show bar | |
return () | |
run :: IO (Either String ()) | |
run = runExceptT doStuff | |
main :: IO () | |
main = do | |
result <- run | |
case result of | |
Right _ -> return () | |
Left err -> putStrLn $ "error: " ++ err |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment