CSC/ECE 517 Fall 2011/ch1 2a bi: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
== Introduction ==
== 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 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]
=== History and Definition ===


=== Definition ===
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] In essence,  the currying process takes a function of two parameters and returns a pointer to a function of one parameter. The function body itself doesn't change. The newly returned function pointer can then be called with the parameter that was left out in the first transformation.  
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 istakes 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.
Another way to think about this, is in terms of the default values for function parameters that most programming languages permit. Default argument is the part of function declaration and it tells compiler what value to pass for an argument if the programmer deliberately misses the argument when calling the function. In the common case that many arguments are almost always the same, this saves the programmer's time and effort required to remember and specify all arguments. Default arguments are useful in situations where some arguments always have the same value. The default parameter values however are constricting, in that the program is only allowed to use those values in case the function call is missing a particular parameter value. Currying enables us to overcome this restriction by essentially generating additional function definitions for the same underlying function with different default values.
Consider a C++ function, sample_test(bool test = false, int data), here the default value of the parameter test is set to 'false' and will remain so for the life of the program. However with currying, we can transform the function to: sample_test_1(1), sample_test_1(1) can then be called either as sample_test_1(true, 1) or  sample_test_1(false, 1).




== Currying In Modern Languages  ==
== Currying In Modern Languages  ==
Currying, as a language feature, has made the cross over the functional programming languages[REFERENCE] to imperative programming languages. Languages such as Scala[REFERENCE], Ruby[REFERENCE], Perl[REFERENCE] and Groovy[REFERENCE] support currying. Functional programs such as Haskell have always supported it. We will look at how to use this feature in a few modern languages in the sections below.


=== Currying In Ruby  ===
=== Currying In Ruby  ===
Implementation, proc objects
 


=== Currying In C  ===
=== Currying In C  ===
Line 23: Line 25:
== Usage ==
== Usage ==
Most programming languages such as C++ allow function parameters to be assigned to default values. However, the default values are restricted  
Most programming languages such as C++ allow function parameters to be assigned to default values. However, the default values are restricted  
== References ==
== References ==


== Links ==
== Links ==

Revision as of 21:56, 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.

History and Definition

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] In essence, the currying process takes a function of two parameters and returns a pointer to a function of one parameter. The function body itself doesn't change. The newly returned function pointer can then be called with the parameter that was left out in the first transformation.

Another way to think about this, is in terms of the default values for function parameters that most programming languages permit. Default argument is the part of function declaration and it tells compiler what value to pass for an argument if the programmer deliberately misses the argument when calling the function. In the common case that many arguments are almost always the same, this saves the programmer's time and effort required to remember and specify all arguments. Default arguments are useful in situations where some arguments always have the same value. The default parameter values however are constricting, in that the program is only allowed to use those values in case the function call is missing a particular parameter value. Currying enables us to overcome this restriction by essentially generating additional function definitions for the same underlying function with different default values. Consider a C++ function, sample_test(bool test = false, int data), here the default value of the parameter test is set to 'false' and will remain so for the life of the program. However with currying, we can transform the function to: sample_test_1(1), sample_test_1(1) can then be called either as sample_test_1(true, 1) or sample_test_1(false, 1).


Currying In Modern Languages

Currying, as a language feature, has made the cross over the functional programming languages[REFERENCE] to imperative programming languages. Languages such as Scala[REFERENCE], Ruby[REFERENCE], Perl[REFERENCE] and Groovy[REFERENCE] support currying. Functional programs such as Haskell have always supported it. We will look at how to use this feature in a few modern languages in the sections below.

Currying In Ruby

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

References

Links