All Topics  
Higher-order function

 

   Email Print
   Bookmark   Link






 

Higher-order function



 
 
In mathematics
Mathematics

Mathematics is the study of quantity, structure, space, change, and related topics of pattern and form. Mathematicians seek out patterns whether found in numbers, space, natural science, computers, imaginary abstractions, or elsewhere....
 and computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, higher-order functions or functionals
Functional (mathematics)

In mathematics, a functional is traditionally a map from a vector space to the Field underlying the vector space, which is usually the real numbers....
 are function
Function (mathematics)

The mathematical concept of a function expresses dependence between two quantities, one of which is known and the other which is produced. A function associates a single output to each input element drawn from a fixed Set , such as the real numbers , although different inputs may have the same output....
s which do at least one of the following: In mathematics these are also known as operator
Operator

In mathematics, an operator is a function which operates on another function. Often, an "operator" is a function which acts on functions to produce other functions ; or it may be a generalization of such a function, as in linear algebra, where some of the terminology reflects the origin of the subject in operations on the functions which ar...
s or functionals. The derivative
Derivative

In calculus, a branch of mathematics, the derivative is a measure of how a function changes as its input changes. Loosely speaking, a derivative can be thought of as how much a quantity is changing at a given point....
 in calculus
Calculus

Calculus is a branch of mathematics that includes the study of limit , derivatives, integrals, and infinite series, and constitutes a major part of modern university education....
 is a common example, since it maps a function to another function.

In the untyped lambda calculus
Lambda calculus

In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
, all functions are higher-order; in a typed lambda calculus
Typed lambda calculus

A typed lambda calculus is a typed formalism that uses the lambda-symbol to denote anonymous function abstraction. Typed lambda calculi are foundational programming languages and are the base of typed functional programming languages such as ML programming language and Haskell and, more indirectly, typed imperative programming....
, from which most functional programming language
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
s are derived, higher-order functions are generally those with types containing more than one arrow.






Discussion
Ask a question about 'Higher-order function'
Start a new discussion about 'Higher-order function'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In mathematics
Mathematics

Mathematics is the study of quantity, structure, space, change, and related topics of pattern and form. Mathematicians seek out patterns whether found in numbers, space, natural science, computers, imaginary abstractions, or elsewhere....
 and computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, higher-order functions or functionals
Functional (mathematics)

In mathematics, a functional is traditionally a map from a vector space to the Field underlying the vector space, which is usually the real numbers....
 are function
Function (mathematics)

The mathematical concept of a function expresses dependence between two quantities, one of which is known and the other which is produced. A function associates a single output to each input element drawn from a fixed Set , such as the real numbers , although different inputs may have the same output....
s 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 operator
Operator

In mathematics, an operator is a function which operates on another function. Often, an "operator" is a function which acts on functions to produce other functions ; or it may be a generalization of such a function, as in linear algebra, where some of the terminology reflects the origin of the subject in operations on the functions which ar...
s or functionals. The derivative
Derivative

In calculus, a branch of mathematics, the derivative is a measure of how a function changes as its input changes. Loosely speaking, a derivative can be thought of as how much a quantity is changing at a given point....
 in calculus
Calculus

Calculus is a branch of mathematics that includes the study of limit , derivatives, integrals, and infinite series, and constitutes a major part of modern university education....
 is a common example, since it maps a function to another function.

In the untyped lambda calculus
Lambda calculus

In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
, all functions are higher-order; in a typed lambda calculus
Typed lambda calculus

A typed lambda calculus is a typed formalism that uses the lambda-symbol to denote anonymous function abstraction. Typed lambda calculi are foundational programming languages and are the base of typed functional programming languages such as ML programming language and Haskell and, more indirectly, typed imperative programming....
, from which most functional programming language
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
s 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
Currying

In computer science, currying, invented by Moses Sch?nfinkel and Gottlob Frege, and independently by Haskell Curry, is the technique of transforming a function that takes multiple parameter in such a way that it can be called as a chain of functions each with a single argument....
.

The map
Map (higher-order function)

In many programming languages, map is the name of a higher-order function that applies a Procedural parameter to a sequence of elements and returns a sequence of results....
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
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 standard function, qsort, is an example of this.

Other examples of higher-order functions include fold
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....
, function composition
Function composition (computer science)

In computer science, function composition is an act or mechanism to combine simple subroutines to build more complicated ones. Like the usual function composition in mathematics, the result of the composed function is passed to the composing one via a parameter....
, integration
Integral

Integration is an important concept in mathematics, specifically in the field of calculus and, more broadly, mathematical analysis. Given a function ƒ of a Real number variable x and an interval [ab] of the real line, the integral...
, and the constant-function function λxy.x.

Example

This Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 script
Scripting language

A scripting language, script language or extension language, is a programming language that allows some control of a single or many Application software....
 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 + 3 def g(function, x): return function(x) * function(x) print g(f, 7)

In this Scheme example the higher-order function g takes a number and returns a function. The function a takes a number and returns that number plus 7. (e.g a(3)=10). (define (g x) (lambda (y) (+ x y))) (define a (g 7)) (display (a 3))

Alternatives

In limited imperative programming
Imperative programming

In computer science, imperative programming is a programming paradigm that describes computation in terms of statement s that change a program state ....
 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
    Data type

    A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it....
    ; these languages generally rely on dynamic typing
    Data type

    A data type in programming languages is an attribute of a data which tells the computer something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it....
     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
    Just-in-time compilation

    In computing, just-in-time compilation , also known as dynamic translation, is a technique for improving the runtime performance of a computer program....
    ) or evaluated by interpretation
    Interpreter (computing)

    In computer science, an interpreter normally means a computer program that execution , i.e. performs, instructions written in a programming language....
    , 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.

Programming languages that support function pointers as function parameters can emulate high-order functions. Such languages include the C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
 and C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 family.

Objects in the object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 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 10 end.

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).

See also

  • Functional analysis
    Functional analysis

    Functional analysis is the branch of mathematics, and specifically of mathematical analysis, concerned with the study of vector spaces and operators acting upon them....
  • Combinatory logic
    Combinatory logic

    Combinatory logic is a notation introduced by Moses Sch?nfinkel and Haskell Curry to eliminate the need for variables in mathematical logic. It has more recently been used in computer science as a theoretical model of computation and also as a basis for the design of functional programming languages....
  • Function-level programming
    Function-level programming

    In computer science, function-level programming refers to one of the two contrasting programming paradigms identified by John Backus in his work on programs as mathematical objects, the other being value-level programming....


List of Higher-order function

  • Map (higher-order function)
    Map (higher-order function)

    In many programming languages, map is the name of a higher-order function that applies a Procedural parameter to a sequence of elements and returns a sequence of results....
  • Fold (higher-order function)
    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....
  • Function composition (computer science)
    Function composition (computer science)

    In computer science, function composition is an act or mechanism to combine simple subroutines to build more complicated ones. Like the usual function composition in mathematics, the result of the composed function is passed to the composing one via a parameter....


External links