CSC/ECE 517 Fall 2012/ch1 1w35 sa

From PG_Wiki
Jump to: navigation, search


Contrary to what might seem to be the meaning, Currying is a term which is associated with Mathematics and computer science. Currying is the mathematical process of using a function with multiple arguments such that a string of multiple functions with single argument can be called to realize the same function. In other words, currying is defined as delayed function of incomplete arguments which works normally when arguments are supplied.


Why named currying?

Back in 1924 Moses Ilyich Schönfinkel, a Russian logician, discovered this property of function. But this discovery of his was later re discovered and built upon by Haskell Curry , an American mathematician and logician. In honor of the contributions made by Haskell Curry the technique was named after him.

What is currying?

If we have a function with multiple arguments we can use this technique of currying to transform the function into a chain of function calls having only single argument. This can be better explained with following example: Suppose we need to calculate addition of two integers which would also yield an integer. The function in general can be written as:

For x=3 and y=4, the function would yield z= 7.

Now to curry the above function, argument application should be done from left to right and right most argument should be left off. So the curried function is represented as follows:

sum = add 3 

Suppose we want to calculate sum of 3 and 4, it can be represented as:

sum 4 = add 3 

which would yield us 7. This is the most simple example of currying to understand the concept.In technical terms, Applications of currying will be dealt in next section of this document.

Applications of currying

There are many applications of the concept of currying through out different programming platforms. We can realize the full power of functional programming by using curried functions.

In mathematics currying can be depicted as follows:

If we want to curry a multi-argument function f (a,b) which gives 'c' as the result. then

We will have the curried function as : f:a → b → c

Realizing the same in C# code we have the following:

  class Example
   delegate Curryfn2 Curryop(int a);
   delegate int Curryop2(int b);
   static void main()
   Curryop add =delegate (int a)
    	 return delegate (int b)
    	return a+b;

The above code uses the first 'delegate' to take the value of ' a' and the second delegate takes the value of 'b'. The second delegate does the core calculation as returns the sum of a and b.

Currying in Ruby

In Ruby we can implement the concept of currying by using 'curry' method. This method creates as curried proc. When we call this proc with lesser number of arguments than declared, it creates a new proc with the passed value already bound to the new proc. The example below will demonstrate it clearly.

func_add = lambda{|a,b,c| a+b+c}
curry_proc= func_add.curry
puts proc3=proc2[4]

Output : 9

Here the flow of the curried functions is as shown below.

  curry_proc[2] → proc1 [3] → proc2 [4] → 9 (proc3)

Difference between Partial functions and Currying

The major difference between partial function and currying is that the former takes only two arguments while latter take multiple arguments. Also, there is a difference in return methods of the two applications. The below tree chart explains the above said more clearly:


Figure 1. Flowchart representation of Currying


Figure 2. Flowchart representation of Partial Functions

Currying returns a new function at every stage, after each stage is processed where as in Partial functions, a function is returned which allows you to get the full application of original function with less parameters.

Why and why not Currying ?

The main advantage of currying is that it provides a way to proof read the functions and check whether all have been applied uniformly on all the arguments. There are other advantages as well like decreasing the amount of complicated codes and hence high level of maintainability. This when compared to partials becomes more accurate for same reason. But as the cliche goes, every coin has two sides, there are few disadvantages of currying as well. The major disadvantage of currying is that it has performance issues. The code is though simple but might result in lengthy ones when compared to partials and hence calling the curried functions might fetch slow performance rate.

What is Uncurrying ?

Uncurrying is basically de-functaionlization of curried functions. It takes additional parameters along with curried function to return the actual function with all the arguments. Multiple iterations might be required to get back the original (uncurried) function.


Currying provides us with a full-proof way to code complicated functions ( which need to be applied to arguments uniformly) and increases the maintainability of the code. It also help in making the code more understandable. Hence, currying can be used for functions with multiple arguments when performance is not affected much and can be compromised upon.


External Links

Personal tools