[Haskell-cafe] Function composition questions from a newbie

leledumbo leledumbo_cool at yahoo.co.id
Mon Nov 30 22:07:26 EST 2009


> This doesnt. But why?

Back to the definition of function composition: f . g is possible if g
returns a value that's compatible with f's argument. Now, let's check the
type of square and add3:

square :: Num a => a -> a
add3 :: Num a => a -> a -> a -> a

(square . add3 1 2) is actually seen by the compiler / interpreter as
(square . (add3 1 2)) due to precedence of . operator.

So, what's the type of add3 1 2?

add3 :: Num a => a -> a

Hmm... seems compatible with square.

what about (square . add3) 1 2?

It doesn't work since add3, when curried (arguments of square "blended" with
add3's) with 1 argument becomes:

add3 :: Num a => a -> a -> a

which is a function that accepts 2 arguments and it's not compatible with
square's a.
-- 
View this message in context: http://old.nabble.com/Function-composition-questions-from-a-newbie-tp26570201p26585840.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list