6

I was reading here, and I noticed that, for example, if I have the following function definitions:

a :: Integer->Integer->Integer
b :: Integer->Bool

The following expression is invalid:

(b . a) 2 3

It's quite strange that the functions of the composition must have only one parameter.

Is this restriction because some problem in defining the most generic one in Haskell or have some other reason?

I'm new to Haskell, so I'm asking maybe useless questions.

Community
  • 1
  • 1
hsknew
  • 153
  • 1
  • 1
  • 3

1 Answers1

7

When you do a 2 3, you're not applying a to 2 arguments. You're actually applying a to it's only argument, resulting in a function, then take that function and apply it to 3. So you actually do 2 applications. So in a sense, what you have is not equivalent to this:

a :: (Integer, Integer) -> Integer
b :: Integer -> Integer
(b . a) (2, 3)

You could've done this, btw

(b . a 2) 3
Phil
  • 5,595
  • 5
  • 35
  • 55
  • And if a has Integer->Integer->Integer->Integer, how could I set the third parameter, and let the other two 'variable'? – hsknew Dec 29 '10 at 04:34
  • You can do `(b . a 1 2) 3`, say. The point is the 'thing' produced by `a` has to be in `b`'s domain (or roughly speaking, of the same type) – Phil Dec 29 '10 at 04:35
  • 1
    PS: So here we're not composing `a` and `b`. We're composing `a 1 2` and `b`, just to make it clear. – Phil Dec 29 '10 at 04:42
  • Yes, I guess this happens because of the currlness (?) definition of the function. (And that 'even' leaves no other option) – hsknew Dec 29 '10 at 04:44
  • @hsknew: The term for replacing multi-argument functions with higher-order functions and multiple applications is called "currying", is that the word you were looking for? If it helps, it's named after a person, the logician [Haskell Curry](http://en.wikipedia.org/wiki/Haskell_Curry). – C. A. McCann Dec 30 '10 at 15:06
  • 3
    Also, you could use your original `a` and `b` functions, and `uncurry` the former. `(b . uncurry a) (2, 3)` – Dan Burton Dec 30 '10 at 19:05