A beginners question

Dear All,
banging my head against Haskell, but liking the feeling of hurting
brains. Just a simple question:
If
fmap (^4) [1,2,3] >>= \i -> shows i " "
gives
"1 16 81 "
then why does
let i = fmap (^4) [1,2,3] in shows i " "
give
"[1,16,81] "
Probably very simple, but there must be a delicate difference between
these two expressions. I just don't get it.
--
Harri Kiiskinen

fmap (^4) [1,2,3] >>= \i -> shows i " "
gives
"1 16 81 "
You are in the list comprehension in a monadic expression. shows is called three times (i is int).
then why does
let i = fmap (^4) [1,2,3] in shows i " "
give
"[1,16,81] "
"shows" is called once (i is a list).

On Sat, Feb 23, 2008 at 8:00 AM, Harri Kiiskinen
then why does
let i = fmap (^4) [1,2,3] in shows i " "
give
"[1,16,81] "
I'll probably mess this up somewhere, but if I do, reset assured that someone else here will correct me ;) fmap (^4) [1,2,3] == [1,16,81] so shows " " of that == "[1,16,81] " (note the trailing space), obvious I hope. However, [1,16,81] >>= \i -> shows i " " operates in the list monad. I can't find the actual instance of Monad [] right now, but it will apply 'shows i " "' to each element in the first list and concat the results. Since strings are just [Char], concating them is string concatenation. And so you have "1 16 81 " (again, note the trailing space) AGL -- Adam Langley agl@imperialviolet.org http://www.imperialviolet.org

2008/2/23, Harri Kiiskinen
Dear All,
banging my head against Haskell, but liking the feeling of hurting brains. Just a simple question:
If
fmap (^4) [1,2,3] >>= \i -> shows i " "
gives
"1 16 81 "
In the List Monad, (>>=) is defined as concatMap, so this code can be translated by :
concatMap (\i -> shows i " ") (fmap (^4) [1,2,3])
shows is applied to each elements of the list, then the strings are concatened. Whereas in
let xs = fmap (^4) [1,2,3] in shows xs " " shows is applied to the whole list.
-- Jedaï

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.
participants (5)
-
Adam Langley
-
Chaddaï Fouché
-
Harri Kiiskinen
-
Roberto Zunino
-
Steve Lihn