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

前端 未结 10 1629
鱼传尺愫
鱼传尺愫 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:56

    This is my first day to try Haskell (after Rust and F#), and I was able to define F#'s |> operator:

    (|>) :: a -> (a -> b) -> b
    (|>) x f = f x
    infixl 0 |>
    

    and it seems to work:

    factorial x =
      case x of
        1 -> 1
        _ -> x * factorial (x-1)
    
    main =     
        5 |> factorial |> print
    

    I bet a Haskell expert can give you an even better solution.

提交回复
热议问题