5
votes

I am trying to understand currying by reading various blogs and stack over flow answers and I think I understood some what. In Haskell, every function is curried, it means, when you have a function like
f x y = x + y
it really is
((f x) y)
in this, the function initially take the first parameter 'x' as the parameter and partially applies it to function f which in turn returns a function for y. where it takes just y a single parameter and applies the function. In both cases the function takes only one parameter and also the process of reducing a function to take single parameter is called 'currying'. Correct me if my understanding wrong here.
So if it is correct, could you please tell me if the functions 'two' and 'three' are curried functions?

three x y z = x + y + z
two = three 1
same = two 1

In this case, I have two specialized functions, 'two' and 'same' which are reduced to take only one parameter so is it curried?

3
You can test it by yourself using the :t command in GHCi (:t three will display the type of three). - Mephy
great, did not know that. thanks. - Nair
@JoshuaHannah - maybe you see this here. I sureley meant no offense or for you to delete your answer - I just hoped you would add a bit usefull information for Nair - sorry man - Random Dev

3 Answers

4
votes

Let's look at two first. It has a signature of

two :: Num a => a -> a -> a

forget the Num a for now (it's only a constraint on a - you can read Int here). Surely this too is a curried function.

The next one is more interesting:

same :: Num a => a -> a

(btw: nice name - it's the same but not exactly id ^^)

TBH: I don't know for sure.

The best definition I know of a curried function is this:

A curried function is a function of N arguments returning another function of (N-1) arguments.

(if you want you can extent this to fully curried functions of course)

This will only fit if you define constants as functions with 0 parameters - which you surely can. So I would say yes(?) this too is a curried function but only in a mathy borderline way (like the sum of 0 numbers is defined to be 0)

4
votes

Best just think about this equationally. The following are all equivalent definitions:

f    x      y      z =  x+y+z
f    x      y =   \z -> x+y+z
f    x =   \y -> (\z -> x+y+z)
f = \x -> (\y -> (\z -> x+y+z))

Partial application is only tangentially relevant here. Most often you don't want the actual partial application to be performed and the actual lambda object to be created in memory - hoping instead that the compiler will employ - and optimize better - the full definition at the final point of full application.

The presence of the functions curry/uncurry is yet another confusing issue. Both f (x,y) = ... and f x y = ... are curried in Haskell, of course, but in our heads we tend to think about the first as a function of two arguments, so the functions translating between the two forms are named curry and uncurry, as a mnemonic.

1
votes

You could think that three function with anonymous functions is:

three = \x -> (\y -> (\z -> x + y + z)))