Taking one function, providing one or more arguments, and getting back a different function with the provided arguments already figured in. Relatively simple to do in Scheme or LISP, and butt-easy in a language like ML. Discovered by Moses Schönfinkel in 1924, and named after the logician Haskell B. Curry.

An example in Scheme:

Given the function:

(define addsalestax
  (lambda (taxrate)
    (lambda (x)
      (+ x (* x (/ taxrate 100.0))))))
you can use currying to define the following:
(define ny (addsalestax 8))
(define oh (addsalestax 5.5))
and use them like this:
>(ny 10.00)
10.8

Currying is an operation on functions. A functional programming language lets you specify such operations. Here's how to curry the first and second arguments of any binary function in Scheme:


(define (curry-2-arg-1st f x)
  (lambda (y) (f x y)))
(define (curry-2-arg-2nd f x)
(lambda (y) (f y x)))
In most pure functional languages, functions are always curried. So the type of a k-arg function is "function taking one argument and returning a k-1-arg function". Currying the first and second arguments (of any function of at least that many arguments!) is as simple as saying
let curry_fst f x = f x;;
let curry_snd f x = \y.(f y x)


in a contrived syntax where "\" means lambda, and other things are a bit like ML.

C++ has a rather restrictive version of currying, namely the function binders that come with the C++ Standard Library.

People find these unsatisfactory for various reasons, and clamor for "real" function closures directly in the language.
However, this violates a basic C++ design goal: You don't pay for what you don't use. Besides, they can be implemented
in C++; people put out entire libraries devoted to them.

So let's make a real closure in C++.  There are two ways to do it:

The following code is an example of the second method.  It has a lot of issues as to flexibility, thread-safety, and efficiency, but these can be overcome with appropriate safeguards.  That's what the existing lambda libraries do, I hope.

For the sake of clarity, we will present our "naive" version:

template <class R, class P1, class P2>
class lambda2
{
 public:
 typedef R (*func_t)(P1, P2);
 lambda2 (func_t ifunc, P2 ip2): func (ifunc), p2 (ip2) {}
 lambda2 (lambda2 const &ilambda): func (ilambda.func),
                                   p2   (ilambda.p2) {}
 lambda2 &operator= (lambda2<R, P1, P2> const &nlambda)
 {
  func = nlambda.func;
  p2 = nlambda.p2;
  return *this;
 }
 P2 bound() const { return p2; }
 lambda2 &rebind (func_t nfunc) { func=nfunc; return *this; }
 lambda2 &rebind (P2 np2) { p2 = np2; return *this; }
 //
 // Finally, the point to all of this.
 //
 R operator() (P1 p1) { return func (p1, p2); }
 private:
 func_t func;
 P2 p2;
};

The template above may look formidable, but it is the implementation of our closure.  The implementation of
closures can't be any less complex than what appears above.   The difference between C++ and the languages where closures are "built in" is that in those languages, you can't see the implementation.

This being C++, the implementation code would appear in a header you #include in your source code.

All you need to provide is a function to lambda-ize.

Given the following function

double divide (double p1, double p2)
{
 return p1 / p2;
}

You could do the following:

typedef lambda2<double,double,double> lambda2_d3;

//
// Down in your code:
//

lambda2_d3 lam (divide, 2);

cout << lam (8); // prints "4"
cout << lam (7); // prints "3.5"

(I wrote and tested this code with Borland C++ Builder 5. Borland has a version of this compiler you can download for free.)

Currying is a very common little technique in functional programming languages such as Haskell that allow functions to return functions. (That is allow higher-order functions to be written.)

I will now attempt to explain it, here using Haskell for the examples.

First a very simple function

Here's a function, succ, that adds one on to it's input:

    succ :: int -> int
    succ x = x + 1
The 'int -> int' bit says that this is a function mapping an int to an int. To apply this function you can do 'succ 1' for example, which results in 2.

Now what if you wish a function that takes more than one input? The traditional way is....

Using a Cartesian product for the domain

Here's a function that adds two numbers together:

    add :: (int, int) -> int
    add (x,y) = x + y
This says that 'add' is a function mapping a Cartesian product of two ints to another int. Applying add to the ordered pair (x,y) results in x+y. ('x+y' being the rule of the function.)

To call the function, you would do something like:

    add (2,3)
Which (obviously enough) evaluates to 5.

Note that the brackets must be there as the function expects an ordered pair for input.

This is all well and good, except when you wish to do a function call something like:

    foo0( foo1( foo2(2,4,5), foo(3) ) )
Okay, not a terribly good example, but you get the point -- there are lots of annoying parentheses about the place. One of the more mundane uses of currying is simply to reduce the number of parentheses needed.

So how does currying work?

Here's the add function again, this time done using currying:

    add :: int -> (int -> int)
    add a b = a + b
Now to call the function you would do:

    add 2 3
Let's step back a moment; what does the signature of this new add function mean?

'int -> (int -> int)' says that the function takes a single int, and returns a function mapping an int to an int.

This works as follows:

First the 'add 2' bit is evalutated. This returns a function, which we'll name 'add2' for simplicity. Essentially this function has the definition:

    add2 :: int -> int
    add2 y = 2 + y
So now you're left with 'add2 3' to evaluate, which of course gives 5.

So in general....

Even if you don't understand the above waffle, just remember this. Given a function definition such as:

    foo :: int -> int -> int -> int
The type after the last arrow is what the function (eventually) returns when fully evalutated.

You can do currying-like things in Perl, too, using closures. Here is an example.

sub foo
{
        my $x = shift;

        sub {
                my $y = shift;

                sub { my $z = shift; $x * $y + $z }
        }
}

print foo(2)->(3)->(4), "\n";

When foo is called with the argument 2, it returns a function equivalent to

sub foo_1 { my $y = shift; sub { my $z = shift; 2 * $y + $z } }

When this function is called with the argument 3, it returns a function equivalent to

sub foo_2 { my $z = shift; 2 * 3 + $z }

And when this function is called, with the argument 4, it obviously returns the value 10.

You can also do emulation of currying another way, by doing things such as

sub plus_2 { $_[0] + 2 }

such that you have the same effect that you might achieve by writing

plus_2 = (+) 2

or somesuch in Haskell.

Of course, these examples aren't very useful in themselves, but the ideas are conceivably useful.

Disclaimer: not all the code here was tested.

Log in or register to write something here or to contact authors.