Haskell operator vs function precedence

后端 未结 5 442
难免孤独
难免孤独 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:57

    You are correct. This rule is part of the Haskell syntax defined by the Haskell Report. In particular note in Section 3, Expressions, that the argument to function application (an fexp) must be an aexp. An aexp allows operators as part of sections, and also within a parenthesized expression, but not bare operators.

    In map foo $ xs, the Haskell syntax means that this is parsed as two expressions which are applied to the binary operator $. As sepp2k notes, the syntax (map foo $) is a left section and has a different meaning.

    I have to confess I've never thought much about this and actually had to look it up in the Report to see why operators have the behavior they do.

提交回复
热议问题