F# utilities in haskell

Slowly I am getting more familiar with Haskell, but there are some things that really irk me. For example, a lot of the point free functions are right to left, instead of left to right. Coming from an F# background this drives me nuts. I want to see what happens first first not last.

For example, if we wanted to do (x+2)+3 in f#

let chained = (+) 2 >> (+) 3

Compare to haskell:

chained :: Integer -> Integer
chained = (+3) . (+2)

In haskell, the +2 is given the argument 3, then that value is given to +3. In f# you work left to right, which I think is more readable.

Anyways, this is an easy problem to solve by defining a cusotm infix operator

(>>>) :: (a -> b) -> (b -> c) -> (a -> c)
(>>>) a b = b . a

Now we can do the same combinations as in F#.

Another thing that bugs me is pipe operator in haskell. I want to be able to pipe using the |> operator left to right (as in subject followed by verb) instead of the way haskell does it with $which is verb followed by subject.

Again, easy fix though

(|>) :: t -> (t -> b) -> b
(|>) a b = b $ a

Now we can do 1 |> (+1) and get 2. Fun!

2 thoughts on “F# utilities in haskell

  1. Elm, which is an FRP version of Haskell also uses the |> operator for piping. But the way to get used to the dot operator’s direction is to read it as “after.”

    chained = (+3) . (+2)

    would be read as “add 3 after adding 2.”

  2. Haskell’s style is based on function composition in mathematics. Needless to say, there are strong reasons to keep that convention.

    However, when you’re programming with monads, Haskell allows monadic composition in both directions with (>>=) and (=<<).

Leave a Reply

Your email address will not be published. Required fields are marked *