by Rahel Lüthy
Learning myself a Haskell should be for great good, so grasping the basics is essential. I am forcing myself to go slowly, taking baby steps in the REPL.
Today, I came across this really simple example:
Figuring out that this will give
[False,True,True] is not exactly rocket science, but what exactly is this
(>10) term? Given that we’re talking about Haskell, it must be a function, right? Also, inferring from the types involved, it must be a function that accepts a
Num and returns a
Bool. But how does this come about?
Back to more basics – here’s another simple function:
Confusingly, the type signature does not distinguish between parameters and the return type. This has been my most important Haskell epiphany yet: Every function in Haskell actually only takes one parameter
greaterThan 10 42 just looks as if we were passing two parameters, but we’re actually calling the function with one parameter, getting a function from
Int -> Bool in return, and then immediately calling this function with the second parameter to get the result. Let’s make this explicit by breaking the call down to the individual steps:
Now back to the
(>10) term: Pretty much everything in Haskell is a function, so
> is a function, too. It is usually called with infix notation, e.g.
10 > 42, but it can actually be converted to a postfix function by putting it in parenthesis:
Obviously, we can also partially apply here:
Back to our initial example – here it is again:
(>10) to 42 correctly gives
True, but applying
(>)10 to 42 did give
False in the example before, right? Things that make you go hmmmm…
Turns out the parenthesis are really crucial here! In Haskell there is a syntax called sectioning, which is special for partial application on infix operators. The term
(>10) corresponds to a right section, which is equivalent to
flip (>) 10. No wonder the semantics seemed flipped!
Long story short: While it is quite intuitive that the
(>10) corresponds to a
\x -> x > 10 in the initial map example, the magic happens through Haskell’s special sectioning syntax.