Anatomy of the operator
The $
application operator is in the form:
($) :: (a -> b) -> a -> b
It's often seen in situation when you want to avoid a trailing pair of parenthesis:
func a (b + c)
is equal to:
func a $ b + c
The magic behind this is simply explained in its fixity declaration:
infixr 0
This means: everything that is after $
will be grouped into a single entity, just like if they where enclosed in parenthesis.
Of course this can be also "nested" like so:
func a $ b + other $ c - d
which means:
func a (b + other (c - d))
Application operator as function
Your case is very interesting and, in my experience, not used very often.
Let's analyze this:
map ($ 4) [odd, even]
We know that map
's type is:
map :: (a -> b) -> [a] -> [b]
The behavior, if someone forgot, is: take the first argument (a function from a
to b
) and apply it to every a
in the second argument list, finally return the resulting list.
You can see ($ 4)
as "pass 4 as argument to something". Which means that:
($ 4) func
is the same as:
func $ 4
So:
map ($ 4) [odd, even]
means:
[($ 4) odd, ($ 4) even]
[(odd $ 4), (even $ 4)]
[False, True]
Why (func $) is not necessary
You could argue that, just like you can do (/ 4)
and (2 /)
which respectively means "divide something by 4" and "divide 2 by something", you could do ($ 4)
and (func $)
and you would be right.
In fact:
(func $) 4
is the same as:
func $ 4
func 4
which is the same as:
($ 4) func
But the reality is that:
map (func $) [...]
would be unnecessary, since the first argument of map
is always applied to each argument to the list, making the above the same as:
map func [...]