Skip to content

Instantly share code, notes, and snippets.

@vito
Created November 8, 2008 05:29
Show Gist options
  • Save vito/23030 to your computer and use it in GitHub Desktop.
Save vito/23030 to your computer and use it in GitHub Desktop.
f :: String -> [String]
f x = [x ++ "!", x ++ "."]
["Hi"] >>= f >>= f
-- is:
(["Hi"] >>= f) >>= f
-- Which, according to the Prelude implementation of the List monad...:
instance Monad [] where
m >>= k = concat (map k m)
return x = [x]
fail s = []
-- ...does:
{- 1 -} concat (map f (["Hi"] >>= f))
{- 2 -} concat (map f (concat (map f ["Hi"])))
{- 3 -} concat (map f (concat [["Hi!", "Hi."]]))
{- 4 -} concat (map f ["Hi!", "Hi."])
{- 5 -} concat [["Hi!!", "Hi!."], ["Hi.!", "Hi.."]]
{- 6 -} ["Hi!!", "Hi!.", "Hi.!", "Hi.."]
f :: String -> [String]
f x = [x ++ "!", x ++ "."]
main = print $ ["Hi"] >>= f >>= f
["Hi!!","Hi!.","Hi.!","Hi.."]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment