In mathematics and computer science , a higher-order function ( HOF ) is a function that does at least one of the following:
21-633: Caml (originally an acronym for Categorical Abstract Machine Language ) is a multi-paradigm , general-purpose , high-level , functional programming language which is a dialect of the ML programming language family. Caml was developed in France at French Institute for Research in Computer Science and Automation (INRIA) and École normale supérieure (Paris) (ENS). Caml is statically typed , strictly evaluated , and uses automatic memory management . OCaml ,
42-526: A case statement (Cardelli 1984, p. 210.). The first Caml implementation was written in Lisp by Ascánder Suárez in 1987 at the French Institute for Research in Computer Science and Automation (INRIA). Its successor, Caml Light , was implemented in C by Xavier Leroy and Damien Doligez , and the original was nicknamed "Heavy Caml" because of its higher memory and CPU requirements. Caml Special Light
63-615: A compiler to optimize. Macros are generally not strongly typed, although they may produce strongly typed code. 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: 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
84-444: A function that returns a function: 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). Defunctionalization can be used to implement higher-order functions in languages that lack first-class functions : In this case, different types are used to trigger different functions via function overloading . The overloaded function in this example has
105-627: A function, and applies the function to some value twice. If twice has to be applied several times for the same f it preferably should return a function rather than a value. This is in line with the " don't repeat yourself " principle. Or in a tacit manner: Using std::function in C++11 : Or, with generic lambdas provided by C++14: Using just delegates: Or equivalently, with static methods: In Elixir, you can mix module definitions and anonymous functions Alternatively, we can also compose using pure anonymous functions. In this Erlang example,
126-500: 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
147-399: Is f ′ ( x ) = 3 x 2 − 1 → f ′ ( 3 ) = 27 − 1 = 26 {\displaystyle f'(x)=3x^{2}-1\rightarrow f'(3)=27-1=26} . The function d is called a " higher-order function " because it accepts another function ( f ) as an argument. Going further can create
168-468: Is a common example, since it maps a function to its derivative , also a function. Higher-order functions should not be confused with other uses of the word "functor" throughout mathematics, see Functor (disambiguation) . 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 that take one function as argument are values with types of
189-539: Is an excellent example of the use of pattern matching over lists, taking pairs of elements ( h1 and h2 ) off the front and storing their sums and differences on the lists s and d , respectively: For example: Pattern matching allows complicated transformations to be represented clearly and succinctly. Moreover, the Caml compiler turns pattern matches into very efficient code, at times resulting in programs that are shorter and faster than equivalent code written with
210-558: Is known as currying . In this case, it is useful to partially apply the first argument delta to d , to obtain a more specialised function: Note that the inferred type indicates that the replacement d is expecting a function with the type float -> float as its first argument. We can compute a numerical approximation to the derivative of x 3 − x − 1 {\displaystyle x^{3}-x-1} at x = 3 {\displaystyle x=3} with: The correct answer
231-399: Is the mathematical definition of factorial as a recurrence relation. Note that the compiler inferred the type of this function to be int -> int , meaning that this function maps ints onto ints. For example, 12! is: Since Caml is a functional programming language, it is easy to create and pass around functions in Caml programs. This ability has very many applications. Calculating
SECTION 10
#1732852249452252-496: The (approximate) derivative of f, by applying d while omitting the x argument: The concepts of curried and higher-order functions are clearly useful in mathematical programs. These concepts are equally applicable to most other forms of programming and can be used to factor code much more aggressively, resulting in shorter programs and fewer bugs. The 1D Haar wavelet transform of an integer -power-of-two-length list of numbers can be implemented very succinctly in Caml and
273-409: The form ( τ 1 → τ 2 ) → τ 3 {\displaystyle (\tau _{1}\to \tau _{2})\to \tau _{3}} . The examples are not intended to compare and contrast programming languages, but to serve as examples of higher-order function syntax In the following examples, the higher-order function twice takes
294-447: The higher-order function or_else/2 takes a list of functions ( Fs ) and argument ( X ). It evaluates the function F with the argument X as argument. If the function F returns false then the next function in Fs will be evaluated. If the function F returns {false, Y} then the next function in Fs with argument Y will be evaluated. If the function F returns R
315-545: The higher-order function or_else/2 will return R . Note that X , Y , and R can be functions. The example returns false . Notice a function literal can be defined either with an identifier ( twice ) or anonymously (assigned to variable plusThree ). Explicitly, or tacitly, Using just functional interfaces: Or equivalently, with static methods: With arrow functions: Or with classical syntax: or with all functions in variables: Note that arrow functions implicitly capture any variables that come from
336-411: The integral of an arbitrary function: The qsort function from the C standard library uses a function pointer to emulate the behavior of a higher-order function. 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
357-677: The lexical variable is "closed" inside of the function. Raku also supports "pointy block" syntax for lambda expressions which can be assigned to a variable or invoked anonymously. Tcl uses apply command to apply an anonymous function (since 8.6). The XACML standard defines higher-order functions in the standard to apply a function to multiple values of attribute bags. The list of higher-order functions in XACML can be found here . Function pointers in languages such as C , C++ , Fortran , and Pascal allow programmers to pass around references to functions. The following C code computes an approximation of
378-489: The main descendant of Caml, adds many features to the language, including an object-oriented programming (object) layer. In the following, # represents the Caml prompt. A "Hello, World!" program is: Many mathematical functions, such as factorial, are most naturally represented in a purely functional form. The following recursive, purely functional Caml function implements factorial: The function can be written equivalently using pattern matching : This latter form
399-533: The numerical derivative of a function is one example. The following Caml function d computes the numerical derivative of a given function f at a given point x : This function requires a small value delta . A good choice for delta is the cube root of the machine epsilon .. The type of the function d indicates that it maps a float onto another function with the type ( float -> float ) -> float -> float . This allows us to partially apply arguments. This functional style
420-447: The parent scope, whereas anonymous functions require the use keyword to do the same. or with all functions in variables: Python decorator syntax is often used to replace a function with the result of passing that function through a higher-order function. E.g., the function g could be implemented equivalently: In Raku, all code objects are closures and therefore can reference inner "lexical" variables from an outer scope because
441-643: Was a further complete rewrite that added a powerful module system to the core language. It was augmented with an object-oriented programming (object) layer to become Objective Caml , eventually renamed OCaml . Multi-paradigm programming language Programming languages can be grouped by the number and types of paradigms supported. A concise reference for the programming paradigms listed in this article. Higher-order function All other functions are first-order functions . In mathematics higher-order functions are also termed operators or functionals . The differential operator in calculus
SECTION 20
#1732852249452#451548