4
votes

I'm trying to understand the concept of currying and went to the Haskell documentation. However, it says that

f is the curried form of g

Yet f takes two arguments and g only one. Since currying is converting a function which takes multiple arguments to a function which takes one argument and returns another function, shouldn't 'g' be the curried function?

From the haskell documentation

Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed.

f :: a -> b -> c
is the curried form of
g :: (a, b) -> c

So this does seem contradictory to me and I also don't see any of these 2 functions return a function either.

2
A function that takes one k-tuple argument is sometimes said to take k arguments. This is normal terminology in mathematics.molbdnilo
Possible duplicate of What is 'Currying'?AJF

2 Answers

9
votes

Yet f takes two arguments and g only one.

No, in fact both functions take one parameter. In fact in Haskell all functions take exactly one parameter.

If you write a signature like:

f :: a ->  b -> c

then this is a less verbose form of:

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

How does that work? f is a function that takes one parameter, and then returns another function that again takes a parameter.

So take for example a function add :: Int -> Int -> Int.

If we write add 5 2, we thus calculate 5 + 2. It looks like it takes two parameters, but in fact we have written (add 5) 2. We thus call the add function with 5 as parameter. This returns a function (let us call this function add5 :: Int -> Int). So this add5 function adds 5 to a number. So if we then call add5 2, then we obtain 7, since add5 returns 5 added to the parameter.

We can however construct a function (like g) that takes one parameter that is a 2-tuple, so we can use another type to pass two values as one parameter. In fact you can see g(5, 2) is actually g (5, 2): you call the function with one parameter, a 2-tuple (5, 2).

So the currying aims to transform such g function that takes one parameter (a 2-tuple) into a function f that takes again one parameter, and this will then construct a function that will take the second element of the original 2-tuple.

6
votes

The type a -> b -> c is actually a -> (b -> c).

So f doesn't take two arguments, of type a and a b and return c, it takes one argument of type a, and returns b -> c, a function from b to c.