2

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, your surmise is correct. An operator cannot be treated as an argument without enclosing it in parentheses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.