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.
Uncurrying is the reverse transformation.
If function f has the typing f : (X × Y) → Z, and g is f after currying, then g has the typing g : X → (Y → Z)
Intuitively, currying says "if you fix some arguments, you get a function of the remaining arguments". For example, if function div stands for the curried form of the division operation /, then div(1) is another function: the same as the function inv that returns the multiplicative inverse of its argument, defined by inv(y) = 1 / y.
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. In category theory, currying is a morphism from an object to an exponential object.
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, Scheme, 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.
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 =
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.
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.
Functional programming | Mathematical logic | Lambda calculus
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Currying".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world