Haskell operator vs function precedence

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

    In addition to the information provided by other answers already, note that different operators can have different precedences over other operators, as well as being left-/right- or non-associative. You can find these properties for the Prelude operators in the Haskell 98 Report fixity section.

    +--------+----------------------+-----------------------+-------------------+
    | Prec-  |   Left associative   |    Non-associative    | Right associative |
    | edence |      operators       |       operators       |    operators      |
    +--------+----------------------+-----------------------+-------------------+
    | 9      | !!                   |                       | .                 |
    | 8      |                      |                       | ^, ^^, **         |
    | 7      | *, /, `div`,         |                       |                   |
    |        | `mod`, `rem`, `quot` |                       |                   |
    | 6      | +, -                 |                       |                   |
    | 5      |                      |                       | :, ++             |
    | 4      |                      | ==, /=, <, <=, >, >=, |                   |
    |        |                      | `elem`, `notElem`     |                   |
    | 3      |                      |                       | &&                |
    | 2      |                      |                       | ||                |
    | 1      | >>, >>=              |                       |                   |
    | 0      |                      |                       | $, $!, `seq`      |
    +--------+----------------------+-----------------------+-------------------+
    

    Any operator lacking a fixity declaration is assumed to be left associative with precedence 9.

    Remember, function application has highest precedence (think of precedence 10 compared to the other precedences in the table) [1].

提交回复
热议问题