Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use function composition for `foo (bar x) (bar y)` in Haskell?

Tags:

haskell

I know that in Haskell, there is a awesome feature called function composition so that we could make our Haskell code pithier, like:

  • use (f . g) x instead of f (g x),
  • use foo x $ bar y z instead of foo x (bar y z)

But is it possible that we could use function composition for foo (bar x) (bar y)?

like image 399
Jasin Yip Avatar asked May 18 '16 03:05

Jasin Yip


1 Answers

You can use the on function for this, for instance

import Data.Function

data Person = Person{name::String, age::Int}

compare `on` age --same as `\x y -> compare (age x) (age y)`
like image 87
jamshidh Avatar answered Sep 28 '22 00:09

jamshidh