[Haskell-cafe] Function composition

Ryan Ingram ryani.spam at gmail.com
Wed Oct 3 13:52:47 EDT 2007


On 10/3/07, Tiago Miguel Laureano Alves <tiagomlalves at gmail.com> wrote:
> Imagine that I have the following functions
>    f :: a -> b -> c -> d
>    g :: d -> e
>
> I want to compose these two functions such that:
>    (g . f) :: a -> b -> c -> e

Here's a pointfree derivation of the composition function you are talking about:

compose g f a b c = g (f a b c)
 = g ((f a b) c)
 = (g . (f a b)) c
compose g f a b = g . f a b
 = (.) g (f a b)
 = ((.) g) ((f a) b)
 = ((.) g . f a) b
compose g f a = ((.) g) . f a
 = (.) ((.) g) (f a)
 = ((.) ((.) g) . f) a
compose g f = (.) ((.) g) . f
 = (.) (g .) . f
 = ((g .) .) . f

In ghci:
Prelude> :set -fglasgow-exts
Prelude> :t ((?g .) .) . ?f
((?g .) .) . ?f :: forall b c a a1 a2.
(?g::b -> c, ?f::a2 -> a1 -> a -> b) =>
a2 -> a1 -> a -> c

  -- ryan


More information about the Haskell-Cafe mailing list