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 f : ( X × Y ) → Z {\displaystyle f:(X\times Y)\to Z} that takes two arguments, one from X {\displaystyle X} and one from Y , {\displaystyle Y,} and produces objects in Z . {\displaystyle Z.} The curried form of this function treats the first argument as a parameter, so as to create a family of functions f x : Y → Z . {\displaystyle f_{x}:Y\to Z.} The family is arranged so that for each object x {\displaystyle x} in X , {\displaystyle X,} there is exactly one function f x . {\displaystyle f_{x}.}
In this example, curry {\displaystyle {\mbox{curry}}} itself becomes a function, that takes f {\displaystyle f} as an argument, and returns a function that maps each x {\displaystyle x} to f x . {\displaystyle f_{x}.} The proper notation for expressing this is verbose. The function f {\displaystyle f} belongs to the set of functions ( X × Y ) → Z . {\displaystyle (X\times Y)\to Z.} Meanwhile, f x {\displaystyle f_{x}} belongs to the set of functions Y → Z . {\displaystyle Y\to Z.} Thus, something that maps x {\displaystyle x} to f x {\displaystyle f_{x}} will be of the type X → [ Y → Z ] . {\displaystyle X\to [Y\to Z].} With this notation, curry {\displaystyle {\mbox{curry}}} is a function that takes objects from the first set, and returns objects in the second set, and so one writes curry : [ ( X × Y ) → Z ] → ( X → [ Y → Z ] ) . {\displaystyle {\mbox{curry}}:[(X\times Y)\to Z]\to (X\to [Y\to Z]).} 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. The example above can be used to illustrate partial application; it is quite similar. Partial application is the function apply {\displaystyle {\mbox{apply}}} that takes the pair f {\displaystyle f} and x {\displaystyle x} together as arguments, and returns f x . {\displaystyle f_{x}.} Using the same notation as above, partial application has the signature apply : ( [ ( X × Y ) → Z ] × X ) → [ Y → Z ] . {\displaystyle {\mbox{apply}}:([(X\times Y)\to Z]\times X)\to [Y\to Z].} 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.
The concept of currying was introduced by Gottlob Frege, developed by Moses Schönfinkel, and further developed by Haskell Curry.
Uncurrying is the dual transformation to currying, and can be seen as a form of defunctionalization. It takes a function f {\displaystyle f} whose return value is another function g {\displaystyle g} , and yields a new function f ′ {\displaystyle f'} that takes as parameters the arguments for both f {\displaystyle f} and g {\displaystyle g} , and returns, as a result, the application of f {\displaystyle f} and subsequently, g {\displaystyle g} , to those arguments. The process can be iterated.