Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Higher-order function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Alternatives === ====Function pointers==== [[Function pointer]]s in languages such as [[C (programming language)|C]], [[C++]], [[Fortran]], and [[Pascal (programming language)|Pascal]] allow programmers to pass around references to functions. The following C code computes an approximation of the integral of an arbitrary function: <syntaxhighlight lang="c"> #include <stdio.h> double square(double x) { return x * x; } double cube(double x) { return x * x * x; } /* Compute the integral of f() within the interval [a,b] */ double integral(double f(double x), double a, double b, int n) { int i; double sum = 0; double dt = (b - a) / n; for (i = 0; i < n; ++i) { sum += f(a + (i + 0.5) * dt); } return sum * dt; } int main() { printf("%g\n", integral(square, 0, 1, 100)); printf("%g\n", integral(cube, 0, 1, 100)); return 0; } </syntaxhighlight> The [[qsort]] function from the C standard library uses a function pointer to emulate the behavior of a higher-order function. ====Macros==== [[Macro (computer science)|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. ====Dynamic code evaluation==== In other [[imperative programming]] languages, it is possible to achieve some of the same algorithmic results as are obtained via higher-order functions by dynamically executing code (sometimes called ''Eval'' or ''Execute'' operations) in the scope of evaluation. There can be significant drawbacks to this approach: *The argument code to be executed is usually not [[type system#Static typing|statically typed]]; these languages generally rely on [[type system#Dynamic typing|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 [[interpreter (computing)|interpretation]], causing some added overhead at run-time, and usually generating less efficient code. ====Objects==== In [[object-oriented programming]] languages that do not support higher-order functions, [[object (computer science)|objects]] can be an effective substitute. An object's [[method (computer science)|methods]] act in essence like functions, and a method may accept objects as parameters and produce objects as return values. Objects often carry added run-time overhead compared to pure functions, however, and added [[boilerplate code]] for defining and instantiating an object and its method(s). Languages that permit [[stack-based memory allocation|stack]]-based (versus [[dynamic memory allocation|heap]]-based) objects or [[Record (computer science)|structs]] can provide more flexibility with this method. An example of using a simple stack based record in [[Free Pascal]] with a function that returns a function: <syntaxhighlight lang="pascal"> 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. </syntaxhighlight> The function <code>a()</code> takes a <code>Txy</code> record as input and returns the integer value of the sum of the record's <code>x</code> and <code>y</code> fields (3 + 7). ====Defunctionalization==== [[Defunctionalization]] can be used to implement higher-order functions in languages that lack [[first class function|first-class functions]]: <syntaxhighlight lang="cpp"> // Defunctionalized function data structures template<typename T> struct Add { T value; }; template<typename T> struct DivBy { T value; }; template<typename F, typename G> struct Composition { F f; G g; }; // Defunctionalized function application implementations template<typename F, typename G, typename X> auto apply(Composition<F, G> f, X arg) { return apply(f.f, apply(f.g, arg)); } template<typename T, typename X> auto apply(Add<T> f, X arg) { return arg + f.value; } template<typename T, typename X> auto apply(DivBy<T> f, X arg) { return arg / f.value; } // Higher-order compose function template<typename F, typename G> Composition<F, G> compose(F f, G g) { return Composition<F, G> {f, g}; } int main(int argc, const char* argv[]) { auto f = compose(DivBy<float>{ 2.0f }, Add<int>{ 5 }); apply(f, 3); // 4.0f apply(f, 9); // 7.0f return 0; } </syntaxhighlight> In this case, different types are used to trigger different functions via [[function overloading]]. The overloaded function in this example has the signature <code>auto apply</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)