Currying
Transforming a function in such a way that it only takes a single argument / From Wikipedia, the free encyclopedia
In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.
In the prototypical example, one begins with a function that takes two arguments, one from
and one from
and produces objects in
The curried form of this function treats the first argument as a parameter, so as to create a family of functions
The family is arranged so that for each object
in
there is exactly one function
In this example, itself becomes a function, that takes
as an argument, and returns a function that maps each
to
The proper notation for expressing this is verbose. The function
belongs to the set of functions
Meanwhile,
belongs to the set of functions
Thus, something that maps
to
will be of the type
With this notation,
is a function that takes objects from the first set, and returns objects in the second set, and so one writes
This is a somewhat informal example; more precise definitions of what is meant by "object" and "function" are given below. These definitions vary from context to context, and take different forms, depending on the theory that one is working in.
Currying is related to, but not the same as, partial application.[1][2] The example above can be used to illustrate partial application; it is quite similar. Partial application is the function that takes the pair
and
together as arguments, and returns
Using the same notation as above, partial application has the signature
Written this way, application can be seen to be adjoint to currying.
The currying of a function with more than two arguments can be defined by induction.
Currying is useful in both practical and theoretical settings. In functional programming languages, and many others, it provides a way of automatically managing how arguments are passed to functions and exceptions. In theoretical computer science, it provides a way to study functions with multiple arguments in simpler theoretical models which provide only one argument. The most general setting for the strict notion of currying and uncurrying is in the closed monoidal categories, which underpins a vast generalization of the Curry–Howard correspondence of proofs and programs to a correspondence with many other structures, including quantum mechanics, cobordisms and string theory.[3]
The concept of currying was introduced by Gottlob Frege,[4][5] developed by Moses Schönfinkel,[6][5][7][8][9][10][11] and further developed by Haskell Curry.[8][10][12][13]
Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function whose return value is another function
, and yields a new function
that takes as parameters the arguments for both
and
, and returns, as a result, the application of
and subsequently,
, to those arguments. The process can be iterated.