simple Haskell functions in point-free style

后端 未结 4 1902
日久生厌
日久生厌 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:43

    $ has a very low precedence. So, f x = (+) 5 $ (/) 8 x actually means f x = (+) 5 $ ((/) 8 x). Instead, rewrite that as

    f x = (+) 5 ( (/) 8 x)
    f x = ((+) 5) ( ((/) 8) x)
    f x = ((+) 5) .  ( ((/) 8) ) x
    f = ((+) 5) . ( (/) 8 )
    f = (5+) . (8/)
    

    The last expression makes sense: f is the composition of two operations, first divide 8 by what one has, and then add 5 to the result. Remember, g.h means "apply h, then apply g the the result of that".

提交回复
热议问题