Haskell operator vs function precedence

后端 未结 5 441
难免孤独
难免孤独 2020-12-01 05:30

I am trying to verify something for myself about operator and function precedence in Haskell. For instance, the following code

list = map foo $ xs
         


        
5条回答
  •  生来不讨喜
    2020-12-01 05:51

    Operators can be passed as function arguments if you surround them with parenthesis (i.e. map foo ($) xs, which would indeed be passed as (map foo ($)) xs). However if you do not surround them with parenthesis, you are correct that they cannot be passed as argument (or assigned to variables).

    Also note that the syntax (someValue $) (where $ could be any operator) actually means something different: it is equivalent to \x -> someValue $ x, i.e. it partially applies the operator to its left operand (which in case of $ is a noop of course). Likewise ($ x) partially applies the operator to the right operand. So map ($ x) [f, g, h] would evaluate to [f x, g x, h x].

提交回复
热议问题