Content deleted Content added
m →C++: linking |
No edit summary |
||
(31 intermediate revisions by 22 users not shown) | |||
Line 1:
{{Short description|Function that takes one or more functions as an input or that outputs a function}}{{More sources|date=November 2024}}{{Distinguish|Functor{{!}}Functor (category theory)}}In [[mathematics]] and [[computer science]], a '''higher-order function''' ('''HOF''') is a [[function (mathematics)|function]] that does at least one of the following:▼
▲In [[mathematics]] and [[computer science]], a '''higher-order function''' is a [[function (mathematics)|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 (computer science)|parameter]] of a [[Subroutine|procedure]] that is itself a procedure),
* returns a function as its result.
All other functions are ''first-order functions''.
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 <math>(\tau_1\to\tau_2)\to\tau_3</math>.
==General examples==
* <code>[[map (higher-order function)|map]]</code> function, found in many functional programming languages, is one example of a higher-order function.
* Sorting functions, which take a comparison function as a parameter, allowing the programmer to separate the sorting algorithm from the comparisons of the items being sorted.
* [[Filter (higher-order function) | filter]]
* [[fold (higher-order function)|fold]]
* [[Prefix sum|scan]]
* [[apply]]
* [[Function composition (computer science)|Function composition]]
Line 65 ⟶ 64:
auto twice = [](const std::function<int(int)>& f)
{
return [
return f(f(x));
};
Line 90 ⟶ 89:
auto twice = [](const auto& f)
{
return [
return f(f(x));
};
Line 254 ⟶ 253:
end
plus_three = fn(i) ->
g = Hof.twice(plus_three)
Line 268 ⟶ 267:
end
plus_three = fn(i) ->
g = twice.(plus_three)
Line 330 ⟶ 329:
Notice a function literal can be defined either with an identifier ({{code|twice}}) or anonymously (assigned to variable {{code|plusThree}}).
====Groovy====
{{further information|Groovy (programming language)}}
<syntaxhighlight lang="groovy">def twice = { f, x -> f(f(x)) }
def plusThree = { it + 3 }
def g = twice.curry(plusThree)
println g(7) // 13
</syntaxhighlight>▼
====Haskell====
{{further information|Haskell
<syntaxhighlight lang="haskell">
Line 421 ⟶ 430:
====JavaScript====
{{further information|JavaScript}}
With arrow functions:
<syntaxhighlight lang="javascript">
Line 428 ⟶ 439:
const plusThree = i => i + 3;
const g = twice(plusThree);
console.log(g(7)); // 13
</syntaxhighlight>
Or with classical syntax:
<syntaxhighlight lang="javascript">
"use strict";
function twice(f) {
}▼
function plusThree(i) {
}▼
const g = twice(plusThree);
Line 493 ⟶ 524:
==== MATLAB ====
{{
<syntaxhighlight lang="matlab">
function result = twice(f)
▲ val = f(f(x));
end
Line 512 ⟶ 539:
==== OCaml ====
{{
<syntaxhighlight lang="ocaml" start="1">
Line 607 ⟶ 634:
my $plusThree = sub {
my ($
$
};
Line 648 ⟶ 675:
<syntaxhighlight lang="R">
twice <-
▲ return(function(x) {
f(f(x))▼
▲ })
▲}
plusThree <- function(i)
▲ return(i + 3)
▲}
g <- twice(plusThree)
>
[1] 13
</syntaxhighlight>
Line 684 ⟶ 705:
====Ruby====
{{further information|
<syntaxhighlight lang="ruby">
Line 740 ⟶ 761:
<syntaxhighlight lang="scheme">
(define (
(
(display ((f 3) 7))▼
(display (add 3 7))▼
▲</syntaxhighlight>
(define (twice f)
(compose f f))
(define (plus-three i)
(define g (twice plus-three))
</syntaxhighlight>
====Swift====
Line 808 ⟶ 835:
=== 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">
Line 846 ⟶ 873:
====Macros====
[[Macro (computer science)|Macros]] can also be used to achieve some of the effects of higher-order functions.
====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.
*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.
====Objects====
Line 940 ⟶ 967:
==References==
{{Reflist}}
{{Functions navbox}}
[[Category:Functional programming]]
Line 947 ⟶ 973:
[[Category:Higher-order functions| ]]
[[Category:Subroutines]]
[[Category:Articles with example
[[Category:Articles with example C++ code]]
[[Category:Articles with example D code]]
[[Category:Articles with example Haskell code]]
[[Category:Articles with example
[[Category:Articles with example JavaScript code]]
[[Category:Articles with example
[[Category:Articles with example Lisp (programming language) code]]
[[Category:Articles with example MATLAB/Octave code]]
[[Category:Articles with example Pascal code]]
[[Category:Articles with example Perl code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example R code]]
[[Category:Articles with example Scala code]]
[[Category:Articles with example Scheme (programming language) code]]
[[Category:Articles with example Tcl code]]
[[Category:Articles with example Swift code]]
|