· f haskell

Haskell vs F#: Function composition

I’m reading through John Hughes' 'Why functional programming matters' paper and one thing I’ve come across which is a bit counter intuitive to me is the Haskell function composition operator.

let inline (>>) f g x = g(f x)

To write a function which doubled all the values in a list and then returned the odd values we’d do this:

let doubleThenOdd = List.map (fun x -> x*2) >> List.filter (fun x -> x % 2 <> 0)

Of course it’s not possible for there to be any values!

doubleThenOdd [1..5];;
val it : int list = []

Based on that understanding I would expect the Haskell function composition operator ('.') to work in the same way:

let doubleThenOdd = map (\ x -> x*2) . filter (\ x -> (mod x 2) /= 0)

But it doesn’t!

Prelude> doubleThenOdd [1..5]
[2,6,10]

In Haskell the functions are applied from right to left rather than left to right as I had expected.

The definition of '.' is therefore:

(f . g) x = f (g x)

So to get what I wanted we’d need to switch around 'map' and 'filter':

let doubleThenOdd = filter (\ x -> (mod x 2) /= 0) . map (\ x -> x*2)
Prelude> doubleThenOdd [1..5]
[]

It’s not too difficult to follow once I worked out that it was different to what I was used to but I was very confused for a while!

Is there a reason why they implement this operator differently?

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket