Home > OS >  Haskell function composition operator and 'space operator' precedence
Haskell function composition operator and 'space operator' precedence

Time:11-11

First of all, I am very new to Haskell and for the moment I am just trying to prepare for an exam. I have this expression:
reverse . take 3 [1 .. 10] and what I get is an error. Is that because space operator has bigger precedence (10) than . operator (9) and the expression above is equivalent to reverse . (take 3 [1..10]) which is reverse . ([1, 2, 3]) which is a composition between reverse and a list which makes no sense, right? I am trying to make sure I got that right, I didn't really find something similar on the internet.

CodePudding user response:

You're basically correct. Prefix function application (what you called the "space operator") binds more tightly than any infix operator does. And for completeness, the way to fix the error is to do (reverse . take 3) [1 .. 10] or reverse . take 3 $ [1 .. 10] instead.

  • Related