Currying

Currying (rarely also nice Finkeln ) is the transformation of a function with n arguments in n functions, each with one argument. Although the method was invented by Moses Schönfinkel and thought ahead of Gottlob Frege, it is named after Haskell Brooks Curry, who has the method ultimately worked extensively theoretically.

Method

It is given a function which requires n arguments. If this applied to an argument, it consumes just exactly this and provides a functional value another function, which still requires n-1 arguments. The returned function is then applied to all other arguments.

In terms of types, it is the conversion of a function to a modified function.

Example

An example in lambda notation to illustrate the method in which the function is defined specifically as follows:

So the function takes three arguments and returns the value. This definition is equivalent to:

In applying the function to the arguments a, b, and c the following occurs:

After the initial application of the function on a, b ​​and c x is replaced in the function body by the first argument a. The result is a function that requires more arguments y and z. This is immediately applied to b and c.

Geometric example

The situation for a function with two arguments, one can imagine as follows: fixing a variable argument corresponds to a restriction of the two-dimensional set of definitions to a one-dimensional subset, eg, The resulting function corresponds to the curve of intersection of the graph of the level of all points. All the points of the graph can be achieved therefore by a two-step selection: zurnächst by defining the cutting plane, and then by evaluating the curve of intersection to the point.

Application

Currying is mainly used in programming languages ​​and calculi, in which functions may be allocated only a single argument. These include, for example, ML, unlambda and the lambda calculus and named after Haskell Curry. Many of these languages ​​, however, offer the programmer syntactic ways to disguise this. An example of this is the equivalence of the feature defined in the above example.

In programming languages

The following example shows Currying in JavaScript. First, a function is defined will add, on the one hand as a result, the sum of the two arguments, on the other hand, if it is called with only one argument ( partial application) that returns a defined as a closure function.

Function will add (x, y ) {    if ( typeof y === " undefined ") {      return function ( y) {        return x y;      }    }    return x y; }   will add (2,4); / / Normal call. 6 results   var = addiere_zu_drei will add (3); / / Currying addiere_zu_drei (5); / / Result 8   document.write (add (2,4 ) "
" ) document.write ( addiere_zu_drei (5 ) "
" ) By currying the function is partially applied, where the function arguments are passed in succession and are temporarily held in a new function that can be used further as desired.

Haskell

Currying is essential in Haskell. Each function obtained as mentioned above, only one argument. Become a seemingly more arguments defined, so always put behind currying:

Will add x y = x y   add 1 3 - is equivalent to (add 1) to 3   addiereZu2 = add 2   addiereZu2 1-3 credentials

  • Programming
  • Theoretical computer science
210088
de