Why is `($ 4) (> 3)` equivalent to `4 > 3`?

后端 未结 3 1915
暖寄归人
暖寄归人 2021-01-04 18:58

I noticed as I was playing around with Haskell today that it is possible to do something like

($ 4) (> 3)

which yields True

3条回答
  •  日久生厌
    2021-01-04 19:28

    ($ 4) is what's called a section. It's a way of partially applying an infix operator, but providing the right-hand side instead of the left. It's exactly equivalent to (flip ($) 4).

    Similarly, (> 3) is a section.

    ($ 4) (> 3)
    

    can be rewritten as

    (flip ($) 4) (> 3)
    

    which is the same as

    flip ($) 4 (> 3)
    

    which is the same as

    (> 3) $ 4
    

    And at this point, it should be clear that this boils down to (4 > 3).

提交回复
热议问题