Created
July 4, 2013 06:10
-
-
Save xkikeg/5925254 to your computer and use it in GitHub Desktop.
Maybeならguardで例外送出がかけるけどEitherならどうするんだろう?
This file contains hidden or 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 Data.Either | |
import Control.Monad | |
failable :: Int -> Maybe Int | |
failable x = do | |
guard $ x /= 0 | |
guard $ x /= 10 | |
return $ x + 10 | |
doFailable :: Int -> IO () | |
doFailable x = do | |
let y = failable x | |
case y of | |
Nothing -> putStrLn "Error!" | |
Just z -> print z | |
main :: IO () | |
main = do | |
doFailable 10 | |
doFailable 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment