The $
sign isn't magic syntax for replacing parentheses. It's an ordinary infix operator, in every way that an operator like +
is.
Putting brackets around a single name like ( xs )
is always equivalent to just xs
1. So if that's what the $
did, then you'd get the same error either way.
Try to imagine what would happen if you had some other operator you're familiar with there, such as +
:
let findKey key xs = snd . head . filter (\(k,v) -> key == k) + xs
Ignore the fact that +
works on numbers so this makes no sense, and just think about the structure of the expression; which terms are being recognised as functions, and which terms are being passed to them as arguments.
In fact, using +
there does actually parse and typecheck successfully! (It gives you a function with nonsense type class constraints, but if you fulfill them it does mean something). Lets walk through how the infix operators are resolved:
let findKey key xs = snd . head . filter (\(k,v) -> key == k) + xs
The highest precedence thing is always normal function application (just writing terms next to each other, with no infix operators involved). There's only one example of that here, filter
applied to the lambda definition. That gets "resolved", and becomes a single sub-expression as far as parsing the rest of the operators is concerned:
let findKey key xs
= let filterExp = filter (\(k,v) -> key == k)
in snd . head . fileterExp + xs
The next highest precedence thing is the .
operator. We've got several to choose from here, all with the same precedence. The .
is right associative, so we take the rightmost one first (but it wouldn't actually change the result whichever one we pick, because the meaning of the .
is an associative operation, but the parser has no way of knowing that):
let findKey key xs
= let filterExp = filter (\(k,v) -> key == k)
dotExp1 = head . filterExp
in snd . dotExp1 + xs
Note that the .
grabbed the terms immediately to its left and right. This is why precedence is so important. There's still a .
left, which is still higher precedence than +
, so it goes next:
let findKey key xs
= let filterExp = filter (\(k,v) -> key == k)
dotExp1 = head . filterExp
dotExp2 = snd . dotExp1
in dotExp2 + xs
And we're done! +
has lowest precedence of the operators here, so it gets its arguments last, and ends up being the top-most call in the whole expression. Note that the +
being low precedence prevented the xs
being "claimed" as an argument by any of the higher precedence applications to the left. And if any of them had been lower precedence, they would have ended up taking the whole expression dotExp2 + xs
as an argument, so they still couldn't have got to xs
; putting an infix operator before xs
(any infix operator) prevents it from being claimed as an argument by anything to the left.
This is in fact exactly the same way that $
is parsed in this expression, because .
and $
happen to have the same relative precedence that .
and +
have; $
is designed to have extremely low precedence, so it will work this way with almost any other operators involved to the left and right.
If we don't put an infix operator between the filter
call and xs
, then this is what happens:
let findKey key xs = snd . head . filter (\(k,v) -> key == k) xs
Normal function application goes first. Here we've got 3 terms simply next to each other: filter
, (\(k,v) -> key == k)
, and xs
. Function application is left associative, so we take the leftmost pair first:
let findKey key xs
= let filterExp1 = filter (\(k,v) -> key == k)
in snd . head . filterExp1 xs
There's still another normal application left, which is still higher precedence than the .
, so we do that:
let findKey key xs
= let filterExp1 = filter (\(k,v) -> key == k)
filterExp2 = filterExp1 xs
in snd . head . filterExp2
Now the first dot:
let findKey key xs
= let filterExp1 = filter (\(k,v) -> key == k)
filterExp2 = filterExp1 xs
dotExp = head . filterExp2
in snd . dotExp
And we're done, the top-most call in the whole expression this time was the left-most .
operator. This time xs
got sucked in as a second argument to filter
; this is sort-of where we want it since filter
does take two arguments, but it leaves the result of filter
in a function composition chain, and filter
applied to two arguments can't return a function. What we wanted was to apply it to one argument to give a function, have that function be part of the function composition chain, and then apply that entire function to xs
.
With $
there, the final form mirrors that when we used +
:
let findKey key xs
= let filterExp = filter (\(k,v) -> key == k)
dotExp1 = head . filterExp
dotExp2 = snd . dotExp1
in dotExp2 $ xs
It's parsed exactly the same way as when we had +
, so the only difference is that where +
means "add my left argument to my right argument", $
means "apply my left argument as a function to my right argument". Which is what we wanted to happen! Huzzah!
TLDR: The bad news is that $
doesn't work by just wrapping parentheses; it's more complicated than that. The good news is that if you understand how Haskell resolves expressions involving infix operators, then you understand how $
works. There is nothing at all special about it as far as the language is concerned; it's an ordinary operator you could define yourself if it didn't exist.
1 Parenthesising an operator like (+)
also just gives you exactly the same function denoted by +
, but now it doesn't have the special infix syntax, so this does affect how things are parsed in this case. Not so with ( xs )
where it's just a name inside.