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

15 views
Skip to first unread message

Hilco Wijbenga

unread,
Jun 22, 2012, 12:54:10 AM6/22/12
to Haskell Cafe
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

_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Conrad Parker

unread,
Jun 22, 2012, 1:02:16 AM6/22/12
to Hilco Wijbenga, Haskell Cafe
On 22 June 2012 12:54, Hilco Wijbenga <hilco.w...@gmail.com> wrote:
> 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.

Peter Gammie

unread,
Jun 22, 2012, 1:04:42 AM6/22/12
to Hilco Wijbenga, Haskell Cafe
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/

Brandon Allbery

unread,
Jun 22, 2012, 1:10:15 AM6/22/12
to Hilco Wijbenga, Haskell Cafe
On Fri, Jun 22, 2012 at 12:54 AM, Hilco Wijbenga <hilco.w...@gmail.com> wrote:
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                                      allb...@gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms

Hilco Wijbenga

unread,
Jun 22, 2012, 1:27:45 AM6/22/12
to Peter Gammie, Haskell Cafe
On 21 June 2012 22:04, Peter Gammie <pet...@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. :-)
Reply all
Reply to author
Forward
0 new messages