Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell operator vs function precedence
    primarykey
    data
    text
    <p>I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code </p> <pre><code>list = map foo $ xs </code></pre> <p>can be rewritten as </p> <pre><code>list = (map foo) $ (xs) </code></pre> <p>and will eventually be</p> <pre><code>list = map foo xs </code></pre> <p>My question used to be, why the first formulation would not be rewritten as </p> <pre><code>list = (map foo $) xs </code></pre> <p>since function precedence is always higher than operator precedence, but I think that I have found the answer: operators are simply not allowed to be arguments of functions (except of course, if you surround them with parentheses). Is this right? If so, I find it odd, that there is no mention of this mechanic/rule in RWH or Learn you a Haskell, or any of the other places that I have searched. So if you know a place, where the rule is stated, please link to it.</p> <p>-- edit: Thank you for your quick answers. I think my confusion came from thinking that an operator literal would somehow evaluate to something, that could get consumed by a function as an argument. It helped me to remember, that an infix operator can be mechanically translated to a prefix functions. Doing this to the first formulation yields </p> <pre><code>($) (map foo) (xs) </code></pre> <p>where there is no doubt that ($) is the consuming function, and since the two formulations are equivalent, then the $ literal in the first formulation cannot be consumed by map. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload