Skip to content

Instantly share code, notes, and snippets.

@bitmaybewise
Created November 22, 2014 13:18
Show Gist options
  • Save bitmaybewise/56903e8179c476a18282 to your computer and use it in GitHub Desktop.
Save bitmaybewise/56903e8179c476a18282 to your computer and use it in GitHub Desktop.
Playing with Haskell monads and do notation
module DoNotation where
-- without do notation
-- applying only >>=
monadChainSuccess1 = Just "hello" >>= \xs ->
Just (xs ++ " ") >>= \ys ->
Just (ys ++ "world")
-- applying >>= and >>
monadChainSuccess2 = Just "hello" >>
Just " " >>= \ys ->
Just (ys ++ "world")
-- applying only >>=
monadChainFailure1 = Just "hello" >>= \xs ->
Nothing >>= \ys ->
Just (ys ++ "world")
-- applying >>= and >>
monadChainFailure2 = Just "hello" >>
Nothing >>= \ys ->
Just (ys ++ "world")
-- with do notation
-- applying only >>=
doMonadChainSuccess1 = do hello <- Just "hello"
space <- Just " "
world <- Just "world"
Just (hello ++ space ++ world)
-- applying >>= and >>
doMonadChainSuccess2 = do hello <- Just "hello"
Just " "
world <- Just "world"
Just (hello ++ world)
-- applying only >>=
doMonadChainFailure1 = do hello <- Just "hello"
space <- Nothing
world <- Just "world"
Just (hello ++ space ++ world)
-- applying >>= and >>
doMonadChainFailure2 = do hello <- Just "hello"
Nothing
world <- Just "world"
Just (hello ++ world)
-- load on ghci and play with it ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment