Why does (++ "!") "bla" return "bla!" and not "!bla"?

Hi all, I'm going through the excellent http://learnyouahaskell.com tutorial. So far it's been pretty easy to follow but now I ran into something that (when I later started reading about maps) do not seem to fully grasp. I think I'm close to understanding why (++ "!") "bla" returns "bla! instead of "!bla" but I seem to be missing the last step. :-) I noticed that ((++) "!") "bla" does indeed return "!bla". So it seems to be related to the infix property of ++? The types of (++) "!", ((++) "!"), and (++ "!") are all the same so that doesn't tell me much. Would someone please nudge me in the right direction? Cheers, Hilco

On 22 June 2012 12:54, Hilco Wijbenga
Hi all,
I'm going through the excellent http://learnyouahaskell.com tutorial. So far it's been pretty easy to follow but now I ran into something that (when I later started reading about maps) do not seem to fully grasp.
I think I'm close to understanding why (++ "!") "bla" returns "bla! instead of "!bla" but I seem to be missing the last step. :-) I noticed that ((++) "!") "bla" does indeed return "!bla". So it seems to be related to the infix property of ++? The types of (++) "!", ((++) "!"), and (++ "!") are all the same so that doesn't tell me much.
Would someone please nudge me in the right direction?
What can you say about ("!" ++) ? Stripping the infix, what is (++) "bla" "!" ? Conrad.

Hilco, On 22/06/2012, at 2:54 PM, Hilco Wijbenga wrote:
I'm going through the excellent http://learnyouahaskell.com tutorial. So far it's been pretty easy to follow but now I ran into something that (when I later started reading about maps) do not seem to fully grasp.
I think I'm close to understanding why (++ "!") "bla" returns "bla! instead of "!bla" but I seem to be missing the last step. :-) I noticed that ((++) "!") "bla" does indeed return "!bla". So it seems to be related to the infix property of ++? The types of (++) "!", ((++) "!"), and (++ "!") are all the same so that doesn't tell me much.
This stuff is in a beginner's tutorial? (!?) This is purely a syntactic issue. These things are called "sections". It might be more obvious if we put in some lambda abstractions, which I hope your tutorial has already introduced: (++ "!") = (\x. x ++ "!") ("!" ++) = (\y. "!" ++ y) Yes, it is related to the infix property of ++. You can get similar things going with arbitrary binary (two argument) functions like so: app = (++) -- or whatever (`app` "!") = (\x. x `app` "!") = (\x. app x "!") (and the other way around) cheers peter -- http://peteg.org/

On 21 June 2012 22:04, Peter Gammie
Hilco,
On 22/06/2012, at 2:54 PM, Hilco Wijbenga wrote:
I'm going through the excellent http://learnyouahaskell.com tutorial. So far it's been pretty easy to follow but now I ran into something that (when I later started reading about maps) do not seem to fully grasp.
I think I'm close to understanding why (++ "!") "bla" returns "bla! instead of "!bla" but I seem to be missing the last step. :-) I noticed that ((++) "!") "bla" does indeed return "!bla". So it seems to be related to the infix property of ++? The types of (++) "!", ((++) "!"), and (++ "!") are all the same so that doesn't tell me much.
This stuff is in a beginner's tutorial? (!?)
I guess it's supposed to be obvious and I'm just a little dense. :-)
This is purely a syntactic issue. These things are called "sections".
It might be more obvious if we put in some lambda abstractions, which I hope your tutorial has already introduced:
(++ "!") = (\x. x ++ "!") ("!" ++) = (\y. "!" ++ y)
Ah, I googled this. Right and left sections. Yes, that makes sense. And no, lambdas have not been introduced yet but it's clear how they work.
Yes, it is related to the infix property of ++. You can get similar things going with arbitrary binary (two argument) functions like so:
app = (++) -- or whatever
(`app` "!") = (\x. x `app` "!") = (\x. app x "!") (and the other way around)
Thanks all, this was the nudge I needed. It's really quite simple. :-)

On Fri, Jun 22, 2012 at 12:54 AM, Hilco Wijbenga
I think I'm close to understanding why (++ "!") "bla" returns "bla! instead of "!bla" but I seem to be missing the last step. :-) I noticed that ((++) "!") "bla" does indeed return "!bla". So it seems to be related to the infix property of ++? The types of (++) "!",
Correct; it is related to (++) being infix. Specifically, think about how partial application would work with an *infix*, as opposed to a *prefix*, expression. It might also help to know that the syntax there is called a "section". -- brandon s allbery allbery.b@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms
participants (4)
-
Brandon Allbery
-
Conrad Parker
-
Hilco Wijbenga
-
Peter Gammie