Currying
From Wikipedia, the free encyclopedia
In mathematics and computer science, currying, invented by Moses Schönfinkel and later re-invented by Haskell Curry,[1] is the technique of transforming a function that takes multiple arguments (or more accurately an n-tuple as argument) in such a way that it can be called as a chain of functions each with a single argument.
Contents |
[edit] Examples
Currying is actually not very different from what we do when we calculate a function for some given values on a piece of paper.
- Take the function f(x,y) = y / x.
- First, we replace x with, say, 2.
- We get g(y) = f(2,y) = y / 2, which is another function (where x has become the constant 2).
- Next, we replace the y argument with some given value, say 3.
- We get g(3) = 3 / 2.
On paper, using classical notation, it's just that we seem to do it all at the same time. But, in fact, when replacing arguments on a piece of paper, it is done sequentially (i.e.partially). Each replacement results in a function within a function. As we sequentially replace each argument, we are currying the function into simpler and simpler versions of the original. Eventually, we end up with a chain of functions as in lambda calculus, where each function takes only one argument, and multi-argument functions are usually represented in curried form.
The practical motivation for currying is that very often the functions obtained by supplying some but not all of the arguments to a curried function (often called partial application) are useful; for example, many languages have a function or operator similar to plus_one. Currying makes it easy to define these functions.
This is similar in computer code: If we let f be a function
f (x,y) = y / x
then the function
g x = (\y -> f (x,y))
is a curried version of f. In particular
(g 2) = (\y -> f (2,y))
is the version from the paper example.
[edit] Etymology
The name "currying", coined by Christopher Strachey in 1967, is a reference to logician Haskell Curry. An alternative name, Schönfinkelisation, has been proposed.[2]
[edit] Definition
Given a function f of type
, then currying it makes a function
. That is, curry(f) takes an argument of type X and returns a function of type
. Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, apply.
The → operator is often considered right-associative, so the curried function type
is often written as
. Conversely, function application is considered to be left-associative, so that
is equivalent to
.
Intuitively, currying says "if you fix the first arguments of the function, you get a function of the remaining arguments". For example, if function div stands for the curried form of the division operation x / y, then div with the parameter x fixed at 1 (i.e. 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.
Some programming languages have built-in syntactic support for currying, where what looks like a multi-argument function is actually syntactic sugar for the function in curried form; notable examples are ML and Haskell, where in both cases all functions have exactly one argument.
Any language that supports closures can be used to write curried functions.
[edit] Mathematical view
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.
When viewed in a set-theoretic light, currying becomes the theorem that the set
of functions from
to A, and the set (AB)C of functions from C to the set of functions from B to A, are isomorphic. In category theory, currying can be found in the universal property of an exponential object, which gives rise to the following adjunction in cartesian closed categories: There is a natural isomorphism between the morphisms from a binary product
and the morphisms to an exponential object
. In other words, currying is the statement that product and Hom are adjoint functors; that is there is a natural transformation:
This is the key property of being a Cartesian closed category.
Under the Curry-Howard correspondence, the existence of currying and uncurrying is equivalent to the logical theorem
, as tuples (product type) corresponds to conjunction in logic, and function type corresponds to implication.
Curry is a continuous function in the Scott topology.
[edit] See also
[edit] Notes
- ^ (Barendregt 2000, p. 8)
- ^ I. Heim and A. Kratzer (1998). Semantics in Generative Grammar. Blackwell.
[edit] References
- Barendregt, Henk; Barendsen, Erik (March 2000), Introduction to Lambda Calculus, ftp://ftp.cs.ru.nl/pub/CompMath.Found/lambda.pdf
[edit] External links
| Look up currying in Wiktionary, the free dictionary. |
- Currying in Javascript
- Currying in Python (despite the name, the article actually describes partial function application, which is different from currying)
- Implicit currying in Scheme
- Currying in Ruby
- Currying in Smalltalk
- Currying in Algol68G
- Currying != Generalized Partial Application! - post at Lambda-the-Ultimate.org
- Currying in Scala
- Currying in Perl
- Currying in CSharp
- Currying in Delphi 2009
- Currying, also called "partial application" in POP-2 and POP-11
- Currying in C


