Haskell composition (.) vs F#'s pipe forward operator (|>)

前端 未结 10 1616
鱼传尺愫
鱼传尺愫 2020-12-04 07:36

In F#, use of the the pipe-forward operator, |>, is pretty common. However, in Haskell I\'ve only ever seen function composition, (.), being us

10条回答
  •  时光取名叫无心
    2020-12-04 07:46

    Left-to-right composition in Haskell

    Some people use left-to-right (message-passing) style in Haskell too. See, for example, mps library on Hackage. An example:

    euler_1 = ( [3,6..999] ++ [5,10..999] ).unique.sum
    

    I think this style looks nice in some situations, but it's harder to read (one needs to know the library and all its operators, the redefined (.) is disturbing too).

    There are also left-to-right as well as right-to-left composition operators in Control.Category, part of the base package. Compare >>> and <<< respectively:

    ghci> :m + Control.Category
    ghci> let f = (+2) ; g = (*3) in map ($1) [f >>> g, f <<< g]
    [9,5]
    

    There is a good reason to prefer left-to-right composition sometimes: evaluation order follows reading order.

提交回复
热议问题