David
David

Reputation: 311

Operator precidence when function is made infix (Haskell)

Does making a function infix change it's precedence somehow? If I write

3 `take` reverse "123456789"

in ghci, it returns 987, but if I write

take 3 reverse "123456789"

I get an error, presumably because it interprets this as (take 3 reverse) "123456789" and I can't pass reverse as an argument to take. Why does it not interpret the first statement this way? Isn't function application right associative?

Upvotes: 2

Views: 62

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476534

Yes, the 3 `take` reverse "123456789" is parsed as (3) `take` (reverse "123456789"), so take (3) (reverse "123456789").

Now take is thus an operator, and operators have lower pecedence. As is specified in the Haskell report:

Any operator lacking a fixity declaration is assumed to be infixl 9.

So take has here fixity 9. If you thus would use this as:

'0' :  3 `take` reverse "1234567890" 

it will be parsed as:

'0' : (3 `take` reverse "1234567890")

since : has fixity infixr 5.

Upvotes: 2

Related Questions