CSC/ECE 517 Fall 2011/ch1 2a bi: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
Currying is a function transformation supported by some programming languages. It transforms a function that accepts n parameters and generates from it, one of more functions with some parameter values already filled in. These broken down functions can then be called in a chain to obtain a result equivalent to that obtained by calling the original function. | Currying is a function transformation supported by some programming languages. It transforms a function that accepts n parameters and generates from it, one of more functions with some parameter values already filled in. These broken down functions can then be called in a chain to obtain a result equivalent to that obtained by calling the original function. | ||
Currying has its origins in the mathematical study of functions. It was observed by Frege in 1893 that it suffices to restrict attention to functions of a single argument. For example, for any two parameter function f(x,y), there is a one parameter function f' such that f'(x) is a function that can be applied to y to give (f'(x))(y) = f (x,y). This corresponds to the well known fact that the sets (AxB -> C) and (A -> (B -> C)) are isomorphic, where "x" is cartesian product and "->" is function space. It was independently re-discovered by Moses Schönfinkel [REFERENCE] and by Haskell Curry [reference] and made it into the Haskell functional programming language as a primary feature.[] | Currying has its origins in the mathematical study of functions. It was observed by Frege in 1893 that it suffices to restrict attention to functions of a single argument. For example, for any two parameter function f(x,y), there is a one parameter function f' such that f'(x) is a function that can be applied to y to give (f'(x))(y) = f (x,y). This corresponds to the well known fact that the sets (AxB -> C) and (A -> (B -> C)) are isomorphic, where "x" is cartesian product and "->" is function space. It was independently re-discovered by Moses Schönfinkel [REFERENCE] and by Haskell Curry [reference] and made it into the Haskell functional programming language as a primary feature.[Reference] | ||
=== Definition === | === Definition === | ||
Given a function ''f'' of type <math>\scriptstyle f \colon (X \times Y) \to Z </math>, '''currying''' it makes a function <math>\scriptstyle \text{curry}(f) \colon X \to (Y \to Z) </math>. That is, <math>\scriptstyle \text{curry}(f) </math> takes an argument of type <math>\scriptstyle X </math> and returns a function of type <math>\scriptstyle Y \to Z </math>. | |||
Given a function f of type , currying it makes a function . That is, takes an argument of type and returns a function of type . Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, apply. | Given a function f of type , currying it makes a function . That is, takes an argument of type and returns a function of type . Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, apply. | ||
== Currying In Modern Languages == | == Currying In Modern Languages == |
Revision as of 17:54, 22 September 2011
Introduction
Currying is a function transformation supported by some programming languages. It transforms a function that accepts n parameters and generates from it, one of more functions with some parameter values already filled in. These broken down functions can then be called in a chain to obtain a result equivalent to that obtained by calling the original function.
Currying has its origins in the mathematical study of functions. It was observed by Frege in 1893 that it suffices to restrict attention to functions of a single argument. For example, for any two parameter function f(x,y), there is a one parameter function f' such that f'(x) is a function that can be applied to y to give (f'(x))(y) = f (x,y). This corresponds to the well known fact that the sets (AxB -> C) and (A -> (B -> C)) are isomorphic, where "x" is cartesian product and "->" is function space. It was independently re-discovered by Moses Schönfinkel [REFERENCE] and by Haskell Curry [reference] and made it into the Haskell functional programming language as a primary feature.[Reference]
Definition
Given a function f of type <math>\scriptstyle f \colon (X \times Y) \to Z </math>, currying it makes a function <math>\scriptstyle \text{curry}(f) \colon X \to (Y \to Z) </math>. That is, <math>\scriptstyle \text{curry}(f) </math> takes an argument of type <math>\scriptstyle X </math> and returns a function of type <math>\scriptstyle Y \to Z </math>. Given a function f of type , currying it makes a function . That is, takes an argument of type and returns a function of type . Uncurrying is the reverse transformation, and is most easily understood in terms of its right adjoint, apply.
Currying In Modern Languages
Currying In Ruby
Implementation, proc objects
Currying In C
Currying In Perl
Currying In Scala
Usage
Most programming languages such as C++ allow function parameters to be assigned to default values. However, the default values are restricted