Higher order function
A higher order function ( English higher-order function ) is in computer science , a function gets that functions as arguments and / or provides functions as a result.
The term is used in particular in the lambda calculus , the theoretical basis of functional programming . There it is closely linked to currying , a procedure that converts functions with several arguments into several one-parameter functions. This transformation is based on the fact that the function spaces and with each other can be identified for any set .
The following function is a higher order function:
This function maps each value to a function that adds a (transferred) natural number . For example . is shown in turn on :
The K combiner comes from the lambda calculus . is constant for everyone .
A well-known example of a higher order function is the differential operator , because it maps functions to functions (derivative and antiderivative). Other important examples are the so-called distributions . In the case with is a - vector space and is therefore a functional .
Example from functional programming
In most functional programming languages such as B. Haskell , the higher order map
function can be defined. It takes a function as an argument f
and returns a function that f
applies to every element of a passed list. It should be noted that map
functions of any type can be given as arguments (indicated by the type variables a
and b
).
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = (f x):map f xs
map (\x -> x ^ 2) [1, 2, 3, 4] -- wird ausgewertet zu [1, 4, 9, 16]
Web links
- Simon Thompson: Type Theory and Functional Programming . (PDF; 1.3 MB)