Higher-order function

Higher-order function

In mathematics and computer science, higher-order functions or functionals are functions which do at least one of the following:
*take one or more functions as an input
*output a function.In mathematics these are also known as operators or functionals.The derivative in calculus is a common example, since it maps a function to another function.

In the untyped lambda calculus, all functions are higher-order; in a typed lambda calculus, from which most functional programming languages are derived, higher-order functions are generally those with types containing more than one arrow. In functional programming, higher-order functions that return other functions are said to be curried.

The map function found in many functional programming languages is one example of a higher-order function. It takes as arguments a function f and a list of elements, and as result, returns a new list with f applied to each element from the list. Another very common kind of higher-order function in those languages which support them are sorting functions which take a comparison function as a parameter, allowing the programmer to separate the sorting algorithm from the comparisons of the items being sorted. The C standard function, qsort, is an example of this.

Other examples of higher-order functions include fold, function composition, integration, and the constant-function function λ"x".λ"y"."x".

Example

This Python script contains the higher-order function g() which takes a function as its first argument and returns a number. This example prints 100 ( "=" (7+3)×(7+3) ).def f(x): return x + 3def g(function, x): return function(x) * function(x)print g(f, 7)And in this example the higher-order function g() takes a number and returns a function.def g(x): return (lambda y: x + y)a = g(7)The function a() takes a number and returns that number plus 7. (e.g a(3)=10)

Similar code in D (programming language) 2.0:int delegate(int) g(int x) { return (int v) { return v + x; };}

void main() { auto a = g(4); auto x = a(3); // x = 7}Function "g" returns "delegate" to anonymous function which in this case becomes closure.Returned delegate is of type int returning int, but in can be simply converted into a template.

In another example, this Pascal program takes a function as its first argument also, and prints 100 :program Example;

type TIntFunc = function(x: Integer): Integer;

function F(X: Integer): Integer; begin Result := X + 3; end;

function G(Func: TIntFunc; X: Integer): Integer; begin Result := Func(X) * Func(X); end;

begin writeln(G(@F, 7)); // prints 100end.

Alternatives

In limited imperative programming languages, software can achieve some of the same algorithmic results as are obtained through use of higher-order functions by dynamically executing code (sometimes called "Eval" or "Execute" operations) in the scope of evaluation. Unfortunately there are significant drawbacks to this approach:
*The argument code to be executed is usually not statically typed; these languages generally rely on dynamic typing to determine the well-formedness and safety of the code to be executed.
*The argument is usually provided as a string, the value of which may not be known until run-time. This string must either be compiled during program execution (using just-in-time compilation) or evaluated by interpretation, causing some additional overhead at run-time, and usually generating less efficient code.

Macros can also be used to achieve some of the effects of higher order functions. However, macros cannot easily avoid the problem of variable capture; they may also result in large amounts of duplicated code, which can be more difficult for a compiler to optimize. Macros are generally not strongly typed, although they may produce strongly typed code.

Objects in the object-oriented programming paradigm can be used as higher order functions – a method of an object acts in many ways like a function, and a method may take objects (containing methods) as arguments or return objects with methods. Objects often carry additional run-time overhead compared to pure functions, however. Language syntax can introduce additional difficulties; an object must be created to hold any parameters that are functions, and any resulting function must also have an associated object. However, languages that offer stack objects or structs (as opposed to just heap based) can make it easier to utilize an object as if it were a function too.

An example of using a simple stack based record in Freepascal with a function that returns a function:

program example;

type int = integer; Txy = record x, y: int; end; Tf = function(xy: Txy): int; function f(xy: Txy): int; begin result:= xy.y + xy.x; end;

function g(func: Tf): Tf; begin result:= func; end;

var a: Tf; xy: Txy = (x: 3; y: 7);begin a:= g(@f); // return a function to "a" writeln(a(xy)); // prints 10end.

The function a() takes a Txy record as input and returns the integer value of the sum of the record's x and y fields (3 + 7).

ee also

*Functional analysis
*Combinatory logic
*Function-level programming

External links

* [http://ergodicity.iamganesh.com/2006/08/07/higher-order-functions/ Higher-order functions and variational calculus]
* [http://boost.org/doc/html/lambda.html Boost Lambda Library for C++]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • higher-order function — noun A function that takes one or more functions as an input, and returns a function as a result …   Wiktionary

  • Fold (higher-order function) — In functional programming, fold, also known variously as reduce, accumulate, compress or inject, is a family of higher order functions that process a data structure in some order and build up a return value. Typically, a fold deals with two… …   Wikipedia

  • Map (higher-order function) — In many programming languages, map is the name of a higher order function that applies a given function to each element of a list, returning a list of results. They are examples of both catamorphisms and anamorphisms. This is often called apply… …   Wikipedia

  • Filter (higher-order function) — In functional programming, filter is a higher order function that processes a data structure (typically a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given… …   Wikipedia

  • Higher order programming — is a style of programming that exploits the theoretical ability to use functions as values; it is usually instantiated with, or borrowed from, models of computation like the lambda calculus which make heavy use of higher order functions.For… …   Wikipedia

  • Higher-Order Perl — (Transforming programs with programs), ISBN 1 55860 701 3 (ISBN 13: 978 1 55860 701 9), is a book written by Mark Jason Dominus with the goal to teach Perl programmers with a strong C and Unix background how to use techniques with roots in… …   Wikipedia

  • Higher-order statistics — (HOS) measures are extensions of second order measures (such as the autocorrelation function and power spectrum) to higher orders.External links*http://www.maths.leeds.ac.uk/Applied/news.dir/issue2/hos intro.html *http://lpce.cnrs orleans.fr/… …   Wikipedia

  • Higher-order abstract syntax — In computer science, higher order abstract syntax (abbreviated HOAS) is a technique for the representation of abstract syntax trees for languages with variable binders.Relation to first order abstract syntaxAn abstract syntax tree is abstract… …   Wikipedia

  • Higher order derivative test — In mathematics, the higher order derivative test is used to find maxima, minima, and points of inflexion in an n th degree polynomial s curve.The testLet f be a differentiable function on the interval I and let c be a point on it such that #f (c) …   Wikipedia

  • Higher-order derivative test — In mathematics, the higher order derivative test is used to find maxima, minima, and points of inflexion for sufficiently differentiable functions. The test Let ƒ be a differentiable function on the interval I and let c be a point on it such that …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”