[Haskell-cafe] Why does (++ "!") "bla" return "bla!" and not "!bla"?

Hilco Wijbenga hilco.wijbenga at gmail.com
Fri Jun 22 07:27:45 CEST 2012


On 21 June 2012 22:04, Peter Gammie <peteg42 at gmail.com> wrote:
> 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. :-)



More information about the Haskell-Cafe mailing list