Higher-order Function - Alternatives

Alternatives

Programming languages that support function pointers as function parameters can emulate higher-order functions. Such languages include the C and C++ family. An example is the following C code which computes an approximation of the integral of an arbitrary function:

// Compute the integral of f within the interval double integral(double (*f)(double x), double a, double b) { double sum, dt; int i; // Numerical integration: 0th order approximation sum = 0.0; dt = (b - a) / 100.0; for (i = 0; i < 100; i++) sum += (*f)(i * dt + a) * dt; return sum; }

Another example is the function qsort from C standard library.

In other imperative programming languages it is possible to 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. There can be 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 added 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.

In object-oriented programming languages that do not support higher-order functions, objects can be an effective substitute. An object's 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 (versus heap-based) objects or 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:

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

Read more about this topic:  Higher-order Function

Famous quotes containing the word alternatives:

    The literal alternatives to [abortion] are suicide, motherhood, and, some would add, madness. Consequently, there is some confusion, discomfort, and cynicism greeting efforts to “find” or “emphasize” or “identify” alternatives to abortion.
    Connie J. Downey (b. 1934)

    The last alternatives they face
    Of face, without the life to save,
    Being from all salvation weaned
    A stag charged both at heel and head:
    Who would come back is turned a fiend
    Instructed by the fiery dead.
    Allen Tate (1899–1979)

    Clearly, society has a tremendous stake in insisting on a woman’s natural fitness for the career of mother: the alternatives are all too expensive.
    Ann Oakley (b. 1944)