[Haskell-cafe] The essence of my monad confusion
Paul Keir
pkeir at dcs.gla.ac.uk
Sat May 2 12:31:03 EDT 2009
On the wiki page for Applicative Functors (http://www.haskell.org/haskellwiki/Applicative_functor) a familiar characteristic of monads is quoted; that they "allow you to run actions depending on the outcomes of earlier actions". I feel comfortable with Functors and Applicative Functors, but I don't yet get that "extra power" that monads provide.
An example immediately follows that quotation on the wiki:
do text <- getLine
if null text
then putStrLn "You refuse to enter something?"
else putStrLn ("You entered " ++ text)
For simplicity's sake, I modified it to avoid using the IO monad; the "text" binding is now provided by the first parameter, and (=<<) is used due to its similarity to fmap:
bar :: Monad m => m String -> m String
bar as = (=<<) (\a -> if null a then return "nothing" else return "something") as
This works fine, so bar ["Blah"] gives ["something"], and bar (Just "") gives ["nothing"].
But, I can get the same effect using a Functor (replacing (=<<) with fmap):
bar2 :: Functor f => f String -> f String
bar2 as = fmap (\a -> if null a then "nothing" else "something") as
Can anyone help me out of the swamp?
Cheers,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090502/027487ee/attachment.htm
More information about the Haskell-Cafe
mailing list