ArticleslgStudy

mathematics

Higher-order function

Higher-order function is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Higher-order function rather than just read about it. In short: In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself a procedure), returns a function as its result. All other functions are first-order functions.

Key takeaways

  • Higher-order function belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Higher-order function to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Higher-order function from memory before moving on to harder problems.

Reference excerpt

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself a procedure), returns a function as its result. All other functions are first-order functions. In mathematics higher-order functions are also termed operators or functionals. The differential operator in calculus 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 the form ( τ 1 → τ 2 ) → τ 3 {\displaystyle (\tau _{1}\to \tau _{2})\to \tau _{3}} .

General examples map function, found in many functional programming languages, is one example of a higher-order function. It takes arguments as a function f and a collection of elements, and as the result, returns a new collection with f applied to each element from the collection. sort, 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 standard function qsort is an example of this. filter function, that takes a collection and a function that returns true or false, and returns a new collection that is the elements of the parameter collection where the function returned true. fold (including foldl and foldr) scan apply Function composition Integration Callback Tree traversal Montague grammar, a semantic theory of natural language, uses higher-order functions

Support in programming languages

Direct support 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 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.

APL

Or in a tacit manner:

C++

Using std::function in C++11:

Or, with generic lambdas provided by C++14:

C#

Using just delegates:

Or equivalently, with static methods:

Clojure

ColdFusion Markup Language (CFML)

Common Lisp

D

Dart

Elixir

In Elixir, you can mix module definitions and anonymous functions

Alternatively, we can also compose using pure anonymous functions.

Erlang

In this Erlang example, 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 the higher-order function or_else/2 will return R. Note that X, Y, and R can be functions. The example returns false.

F#

Go

Notice a function literal can be defined either with an identifier (twice) or anonymously (assigned to variable plusThree).

Groovy

Haskell

J

Explicitly,

or tacitly,

Java (1.8+)

Using just functional interfaces:

Or equivalently, with static methods:

JavaScript

With arrow functions:

Or with classical syntax:

Julia

Kotlin

Lua

MATLAB

OCaml

PHP

or with all functions in variables:

Note that arrow functions implicitly capture any variables that come from the parent scope, whereas anonymous functions require the use keyword to do the same.

Perl

or with all functions in variables:

Python

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:

R

Raku

In Raku, all code objects are closures and therefore can reference inner "lexical" variables from an outer scope because 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.

Ruby

Rust

Scala

Scheme

Swift

Tcl

Tcl uses apply command to apply an anonymous function (since 8.6).

XACML

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.

XQuery

Alternatives

Function pointers 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 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 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 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.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Higher-order function

Start with the simplest possible case. Write down what Higher-order function claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Higher-order function before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Higher-order function ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Higher-order function

In research
Higher-order function appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Higher-order function in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Higher-order function is common in secondary-school and first-year university syllabi. It links to neighbouring topics Functional programming, Higher-order functions, Lambda calculus, so understanding it makes those chapters shorter.
In everyday life
Look for Higher-order function outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Higher-order function in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Higher-order function means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Higher-order function out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Higher-order function in simple terms?

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following: takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself a procedure), returns a function as its result. All oth…

Why does Higher-order function matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Higher-order function?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Higher-order function.

Tags

  • Functional programming
  • Higher-order functions
  • Lambda calculus
  • Programming language comparisons
  • Subroutines

Keep exploring