Skip to content

Instantly share code, notes, and snippets.

@jmikkola
Created September 9, 2016 04:34
Show Gist options
  • Save jmikkola/812b8e784db8a18b227b07b118106858 to your computer and use it in GitHub Desktop.
Save jmikkola/812b8e784db8a18b227b07b118106858 to your computer and use it in GitHub Desktop.
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