[Haskell-cafe] Fucntion composing

Chris Smith cdsmith at gmail.com
Mon Apr 11 14:45:23 CEST 2011


On Mon, 2011-04-11 at 11:22 +0200, Adam Krauze wrote:
> f :: (Num a) => [a] -> [a] -> [(a,a)]  // f takes two lists and zips them into one in some special way
> g :: (Num a) => a -> [(a,a)] -> [a]  // g using some Num value calculates list of singletons from list of pairs
> 
> Prelude> let h x y = (g 0 (f x y))
> 
> How to do pointfree definition of h?

You can eliminate the second point, y, pretty easily by just using
function composition:

    let h x = g 0 . f x

To eliminate x, we can first rewrite this expression using a section of
the (.) operator.

    let h x = (g 0 .) (f x)

and then introduce another function composition:

    let h = (g 0 .) . f

Whether that's clearer than the pointed definition is up for debate, but
there it is.  Just keep in mind that sections of (.) are very confusing
to some people.

-- 
Chris Smith




More information about the Haskell-Cafe mailing list