[Haskell-cafe] function composition

Mike Burns mike at mike-burns.com
Sun Jan 15 16:30:22 CET 2012


What you've actually defined is function application.

Function composition has a different type:

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

(I'd read this as: compose takes a function from b to c, a function from
a to b, and something of type a, and produces a c.)

You ran into an order of operations issue that masked this bug:

  p . q 4  /= (p . q) 4 

Since you're working through this I don't want to give away the actual
definition of function composition, but be sure to follow the type.

On 2012-01-15 16.17.24 +0100, TP wrote:
> Hi,
> 
> I have a basic question concerning function composition. I have used 
> http://www.haskell.org/tutorial/functions.html
> to write a composition function:
> 
> Prelude> let f°g = f g
> Prelude> let p = (*2)
> Prelude> let q = (+3)
> Prelude> p°q 4
> 14
> Prelude> :t (°)
> (°) :: (t1 -> t) -> t1 -> t
> 
> If I understand well, this means that the infix operator "°" takes a function 
> of type t1, i.e. g in f°g, and applies f on it, which takes a type t1 as input 
> and returns f(g) which is of type t. The final result is of type t. So the first 
> argument is represented above by "(t1->t)", and the second by "t1", the final 
> result being of type t.
> 
> However, I am not able to get the type of p°q
> 
> Prelude> :t p°q
> 
> <interactive>:1:3:
>     Couldn't match expected type `Integer'
>                 with actual type `Integer -> Integer'
>     In the second argument of `(°)', namely `q'
>     In the expression: p ° q
> Prelude> 
> 
> What's the problem here? How can I obtain the type of p°q?
> 
> Thanks in advance,
> 
> TP
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list