Skip to content

Instantly share code, notes, and snippets.

@psycotica0
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psycotica0/31ca4ac945f5203fc0e7 to your computer and use it in GitHub Desktop.
Save psycotica0/31ca4ac945f5203fc0e7 to your computer and use it in GitHub Desktop.
Demo of Either as a Monad for annotated return types.
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