Demo of Either as a Monad for annotated return types.
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
meow = Right "Meow" | |
greeting = Right "Hi" | |
sub_meow input = do | |
value <- integer_meow input | |
if value > 7 then | |
Right "Big Meow" | |
else | |
Left "Too Small" | |
integer_meow 42 = Right 10 | |
integer_meow value = Left "Wrong input" | |
someThing = do | |
meow | |
greeting | |
sub_meow 42 | |
sub_meow 17 -- Remove this to make things succeed | |
main = case someThing of | |
(Left error) -> putStrLn ("Woah baby, error: " ++ error) | |
(Right result) -> putStrLn ("Success! " ++ result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment