simple Haskell functions in point-free style

后端 未结 4 1903
日久生厌
日久生厌 2021-02-08 02:00

I am trying to understand how to convert functions to point-free notation in Haskell. I saw this example, but it is more complicated than what I am looking for. I feel like I un

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 02:27

    The "pointfree" program can be installed with cabal install pointfree, and shows you how to write an expression in pointfree style. For example:

    $ pointfree "f x = 5 + 8/x"
    f = (5 +) . (8 /)
    

    Explanation of this conversion:

    1. You can use "sections" for infix/operator functions. (a +) == \b -> a + b and (+ a) == \b -> b + a
    2. The . function takes the result of the second parameter, which is a one-argument function, and applies it to the first argument.

提交回复
热议问题