Reputation: 1829
Is it possible for operator to be a preceding function argument without enclosing it in parentheses in Haskell?
Those definitions will not parse:
let x f = f .
let x f = f $ .
It seems the only way for an operator to be an argument of preceding function is to be enclosed in parentheses like this:
let x f = f (.)
If this is true, it would be much easier for me to grasp expressions like this:
e f <?> g h
Upvotes: 2
Views: 169
Reputation: 54078
As @RobinGreen has pointed out, you have to wrap an operator in parentheses to use it as an argument. The reason why, though is because wrapping it in parentheses makes the compiler treat it like a prefix function instead of an infix function. So the following two are equivalent:
1 + 2
(+) 1 2
(1 +) 2 -- Partially applied operator is still an operator
-- Note: this is not the same as (+ 1) 2 in general
-- because argument order matters. It would be
-- silly to say that (f .) g is the same as (. f) g
Essentially, the parentheses make an infix operator into a normal prefix function that can be passed around like any other.
Upvotes: 3
Reputation: 33103
Yes, your surmise is correct. An operator cannot be treated as an argument without enclosing it in parentheses.
Upvotes: 1