[Haskell-cafe] A beginners question
Roberto Zunino
zunino at di.unipi.it
Sat Feb 23 11:45:19 EST 2008
Harri Kiiskinen wrote:
> fmap (^4) [1,2,3] >>= \i -> shows i " "
> let i = fmap (^4) [1,2,3] in shows i " "
>
> Probably very simple, but there must be a delicate difference between
> these two expressions. I just don't get it.
First, let's simplify these expressions using the following equation:
fmap (^4) [1,2,3] == [1,16,81]
So, we have
x1 = [1,16,81] >>= \i -> shows i " "
x2 = let i = [1,16,81] in shows i " "
Let's examine the x2 first. We can substitute i, and obtain
shows [1,16,81] " "
note that the whole list is passed to shows. shows then "prints" it to a
string, using the notation for lists, and adds a trailing space. Note
that shows is called here with type:
shows :: [Integer] -> String -> String
Now, we consider x1. Here >>= is invoked for the list monad,
which extracts each member 1,16,81 from the
list and applies shows to them separately, and concatenates all the
results. In other words, we can rewrite x1 as:
shows 1 " " ++ shows 16 " " ++ shows 81 " "
Note that here we pass single elements to shows, and not the whole list.
Indeed, here we are calling shows at a different type:
shows :: Integer -> String -> String
But this is fine, since shows knows how to print Integers as well as
lists of them.
Concluding: the monadic bind operator >>= is not function application.
More information about the Haskell-Cafe
mailing list