Currying
From Free net encyclopedia
In computer science, currying is the technique of transforming a function taking multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result. The technique was named by Christopher Strachey after logician Haskell Curry, though it was invented by Moses Schönfinkel and Gottlob Frege.
Intuitively, currying says "if you fix some arguments, you get a function of the remaining arguments". So if you take the function in two variables <math>y^x</math>, and fix <math>y=2</math>, then you get the function in one variable <math>2^x</math>.
In theoretical computer science, currying provides a way to study functions with multiple arguments in very simple theoretical models such as the lambda calculus in which functions only take a single argument.
The practical motivation for currying is that very often the functions you get by supplying some but not all of the arguments to a curried function are useful; for example, many languages have a function or operator similar to plus_one
. Currying makes it easy to define these functions.
Some programming languages have syntactic sugar for currying, notably ML and Haskell. Any language that supports functions as first-class objects, including Lisp, Perl, Ruby, Python, R, S and JavaScript can be used to write curried functions. Python 2.5 will include a standard library module implementing partial function application.
Contents |
Examples
Suppose that plus
is a function taking two arguments x
and y
and returning x + y
. In the ML programming language we would define it as follows:
plus = fn(x, y) => x + y
and plus(1, 2)
returns 3
as we expect.
The curried version of plus
takes a single argument x
and returns a new function which takes a single argument y
and returns x + y
. In ML we would define it as follows:
curried_plus = fn(x) => fn(y) => x + y
and now when we call curried_plus(1)
we get a new function that adds 1 to its argument:
plus_one = curried_plus(1)
and now plus_one(2)
returns 3
and plus_one(7)
returns 8
.
When declaring functions in the strictly-typed OCaml programming language, the type returned by a function shows the Curried form of the function. Typing the function into the OCaml interpreter displays the type immediately:
# let plus x y = x + y ;; val plus : int -> int -> int = <fun>
C++
Currying may be achieved in [[C++]] using the Standard Template Library function object adapters (binder1st
and binder2nd
), and more generically using the Boost bind
mechanism.
Here is another way to do currying in C++ (from this comment on codepost.org):
The plus function:
int plus(int x, int y) { return x + y; }
The curried version of plus
:
class curried_plus { private: int x; public: curried_plus(int _x) : x(_x) {} int operator () (int y) const { return plus(x, y); } };
and the usage:
curried_plus plus_one(1);
now plus_one(2)
returns 3
.
Mathematical view
When viewed in a set-theoretic light, currying becomes the theorem that the set <math>A^{B\times C}</math> of functions from <math>B\times C</math> to <math>A</math>, and the set <math>(A^B)^C</math> of functions from <math>C</math> to the set of functions from <math>B</math> to <math>A</math>, are isomorphic.
In other words, currying is the statement that product and Hom are adjoint functors; this is the key property of being a Cartesian closed category.