Haskell operator vs function precedence
Solution 1:
Firstly, application (whitespace) is the highest precedence "operator".
Secondly, in Haskell, there's really no distinction between operators and functions, other than that operators are infix by default, while functions aren't. You can convert functions to infix with backticks
2 `f` x
and convert operators to prefix with parens:
(+) 2 3
So, your question is a bit confused.
Now, specific functions and operators will have declared precedence, which you can find in GHCi with ":info":
Prelude> :info ($)
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $
Prelude> :info (+)
class (Eq a, Show a) => Num a where
(+) :: a -> a -> a
infixl 6 +
Showing both precedence and associativity.
Solution 2:
You are correct. This rule is part of the Haskell syntax defined by the Haskell Report. In particular note in Section 3, Expressions, that the argument to function application (an fexp
) must be an aexp
. An aexp allows operators as part of sections, and also within a parenthesized expression, but not bare operators.
In map foo $ xs
, the Haskell syntax means that this is parsed as two expressions which are applied to the binary operator $
. As sepp2k notes, the syntax (map foo $)
is a left section and has a different meaning.
I have to confess I've never thought much about this and actually had to look it up in the Report to see why operators have the behavior they do.