<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.formulasearchengine.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=62.56.70.148</id>
	<title>formulasearchengine - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.formulasearchengine.com/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=62.56.70.148"/>
	<link rel="alternate" type="text/html" href="https://en.formulasearchengine.com/wiki/Special:Contributions/62.56.70.148"/>
	<updated>2026-05-02T06:31:18Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0-wmf.28</generator>
	<entry>
		<id>https://en.formulasearchengine.com/index.php?title=Newton%27s_law_of_universal_gravitation&amp;diff=3336</id>
		<title>Newton&#039;s law of universal gravitation</title>
		<link rel="alternate" type="text/html" href="https://en.formulasearchengine.com/index.php?title=Newton%27s_law_of_universal_gravitation&amp;diff=3336"/>
		<updated>2014-01-31T23:35:57Z</updated>

		<summary type="html">&lt;p&gt;62.56.70.148: /* Extensions */ typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{merge from|Procedural parameter|date=May 2012}}&lt;br /&gt;
{{inline citations|date=September 2013}}&lt;br /&gt;
In [[mathematics]] and [[computer science]], a &#039;&#039;&#039;higher-order function&#039;&#039;&#039; (also &#039;&#039;&#039;functional form&#039;&#039;&#039;, &#039;&#039;&#039;functional&#039;&#039;&#039; or &#039;&#039;&#039;functor&#039;&#039;&#039;) is a [[function (mathematics)|function]] that does at least one of the following:&lt;br /&gt;
*takes one or more functions as an input&lt;br /&gt;
*outputs a function&lt;br /&gt;
All other functions are &#039;&#039;first-order functions&#039;&#039;.  In mathematics higher-order functions are also known as &#039;&#039;[[operator (mathematics)|operators]]&#039;&#039; or &#039;&#039;[[functional (mathematics)|functionals]]&#039;&#039;.  The [[derivative]] in [[calculus]] is a common example, since it maps a function to another function.&lt;br /&gt;
&lt;br /&gt;
In the [[lambda calculus|untyped lambda calculus]], all functions are higher-order; in a [[typed lambda calculus]], from which most [[functional programming]] languages are derived, higher-order functions are values with types of the form &amp;lt;math&amp;gt;(\tau_1\to\tau_2)\to\tau_3&amp;lt;/math&amp;gt;.  &lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;[[map (higher-order function)|map]]&amp;lt;/code&amp;gt; function, found in many functional programming languages, is one example of a higher-order function.  It takes as arguments a function &#039;&#039;f&#039;&#039; and a list of elements, and as the result, returns a new list with &#039;&#039;f&#039;&#039; applied to each element from the list. Another very common kind of higher-order function in those languages which support them are 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.  The [[C (programming language)|C]] standard [[function (computer science)|function]] &amp;lt;code&amp;gt;qsort&amp;lt;/code&amp;gt; is an example of this.&lt;br /&gt;
&lt;br /&gt;
Other examples of higher-order functions include [[fold (higher-order function)|fold]], [[function composition (computer science)|function composition]], [[integral|integration]], and the constant-function function λ&#039;&#039;x&#039;&#039;.λ&#039;&#039;y&#039;&#039;.&#039;&#039;x&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
==Example==&lt;br /&gt;
&#039;&#039;The following examples are not intended to compare and contrast programming languages, since each program performs a different task.&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
This [[Python (programming language)|Python]] program defines the higher-order function &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; which takes a function and an arbitrary object (here a number), and applies the function to the object twice. This example prints 13: twice(f, 7) = f(f(7)) = (7 + 3) + 3.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def f(x):&lt;br /&gt;
    return x + 3&lt;br /&gt;
&lt;br /&gt;
def twice(function, x):&lt;br /&gt;
    return function(function(x))&lt;br /&gt;
&lt;br /&gt;
print(twice(f, 7))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
This [[Haskell (programming language)|Haskell]] code is the equivalent of the Python program above. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;br /&gt;
f = (+3)&lt;br /&gt;
twice function = function . function&lt;br /&gt;
main = print (twice f 7)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
In this [[Scheme (programming language)|Scheme]] example the higher-order function &amp;lt;code&amp;gt;g()&amp;lt;/code&amp;gt; takes a number and returns a function. The function &amp;lt;code&amp;gt;a()&amp;lt;/code&amp;gt; takes a number and returns that number plus 7 (e.g. &#039;&#039;a&#039;&#039;(3)=10). &lt;br /&gt;
&amp;lt;source lang=&amp;quot;scheme&amp;quot;&amp;gt;&lt;br /&gt;
(define (g x)&lt;br /&gt;
  (lambda (y) (+ x y)))&lt;br /&gt;
(define a (g 7))&lt;br /&gt;
(display (a 3))&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this [[Erlang (programming language)|Erlang]] example the higher-order function &amp;lt;code&amp;gt;or_else&amp;lt;/code&amp;gt;/2 takes a list of functions (&amp;lt;code&amp;gt;Fs&amp;lt;/code&amp;gt;) and argument (&amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;). It evaluates the function &amp;lt;code&amp;gt;F&amp;lt;/code&amp;gt; with the argument &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt; as argument. If the function &amp;lt;code&amp;gt;F&amp;lt;/code&amp;gt; returns false then the next function in &amp;lt;code&amp;gt;Fs&amp;lt;/code&amp;gt; will be evaluated. If the function &amp;lt;code&amp;gt;F&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;{false,Y}&amp;lt;/code&amp;gt; then the next function in &amp;lt;code&amp;gt;Fs&amp;lt;/code&amp;gt; with argument &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt; will be evaluated. If the function &amp;lt;code&amp;gt;F&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; the higher-order function &amp;lt;code&amp;gt;or_else&amp;lt;/code&amp;gt;/2 will return &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt;. Note that &amp;lt;code&amp;gt;X&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Y&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;R&amp;lt;/code&amp;gt; can be functions. The example returns &amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;erlang&amp;quot;&amp;gt;&lt;br /&gt;
or_else([], _) -&amp;gt; false;&lt;br /&gt;
or_else([F | Fs], X) -&amp;gt; or_else(Fs, X, F(X)).&lt;br /&gt;
&lt;br /&gt;
or_else(Fs, X, false) -&amp;gt; or_else(Fs, X);&lt;br /&gt;
or_else(Fs, _, {false, Y}) -&amp;gt; or_else(Fs, Y);&lt;br /&gt;
or_else(_, _, R) -&amp;gt; R.&lt;br /&gt;
&lt;br /&gt;
or_else([fun erlang:is_integer/1, fun erlang:is_atom/1, fun erlang:is_list/1],3.23).&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this [[JavaScript]] example the higher-order function &amp;lt;code&amp;gt;ArrayForEach&amp;lt;/code&amp;gt; takes an array and a method in as arguments and calls the method on every element in the array. That is, it [[Map (higher-order function)|Map]]s the function over the array elements.&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
function ArrayForEach(array, func) {&lt;br /&gt;
    for (var i = 0; i &amp;lt; array.length; i++) {&lt;br /&gt;
        if (i in array) {&lt;br /&gt;
            func(array[i]);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function log(msg) {&lt;br /&gt;
    console.log(msg);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
ArrayForEach([1,2,3,4,5], log);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This [[Ruby  (programming language)|Ruby ]] code is the equivalent of the Python program above. &lt;br /&gt;
&amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
f1 = -&amp;gt;(x){ x + 3 } &lt;br /&gt;
def twice(f, x); f.call(f.call(x)) end&lt;br /&gt;
&lt;br /&gt;
print twice(f1, 7)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Alternatives==&lt;br /&gt;
&lt;br /&gt;
In programming languages that support [[function pointer]]s, one can emulate higher-order functions to some extent. Such languages include the [[C (programming language)|C]] and [[C++]] family. An example is the following C code which computes an approximation of the integral of an arbitrary function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
// Compute the integral of f() within the interval [a,b]&lt;br /&gt;
double integral(double (*f)(double x), double a, double b)&lt;br /&gt;
{&lt;br /&gt;
    double  sum, dt;&lt;br /&gt;
    int     i;&lt;br /&gt;
&lt;br /&gt;
    // Numerical integration: 0th order approximation&lt;br /&gt;
    sum = 0.0;&lt;br /&gt;
    dt = (b - a) / 100.0;&lt;br /&gt;
    for (i = 0;  i &amp;lt; 100;  i++)&lt;br /&gt;
        sum += (*f)(i * dt + a) * dt;&lt;br /&gt;
&lt;br /&gt;
    return sum;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Another example is the function [[qsort]] from C standard library.&lt;br /&gt;
&lt;br /&gt;
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 &amp;quot;Eval&amp;quot; or &amp;quot;Execute&amp;quot; operations) in the scope of evaluation.  There can be significant drawbacks to this approach:&lt;br /&gt;
*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.&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
[[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.&lt;br /&gt;
&lt;br /&gt;
In [[object-oriented programming]] languages that do not support higher-order functions, [[object (computer science)|objects]] can be an effective substitute. An object&#039;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 [[structs]] can provide more flexibility with this method.&lt;br /&gt;
&lt;br /&gt;
An example of using a simple stack based record in [[Free Pascal]] with a function that returns a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=pascal&amp;gt;&lt;br /&gt;
program example; &lt;br /&gt;
&lt;br /&gt;
type &lt;br /&gt;
  int = integer;&lt;br /&gt;
  Txy = record x, y: int; end;&lt;br /&gt;
  Tf = function (xy: Txy): int;&lt;br /&gt;
     &lt;br /&gt;
function f(xy: Txy): int; &lt;br /&gt;
begin &lt;br /&gt;
  Result := xy.y + xy.x; &lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
function g(func: Tf): Tf; &lt;br /&gt;
begin &lt;br /&gt;
  result := func; &lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
var &lt;br /&gt;
  a: Tf;&lt;br /&gt;
  xy: Txy = (x: 3; y: 7);&lt;br /&gt;
&lt;br /&gt;
begin  &lt;br /&gt;
  a := g(@f);      // return a function to &amp;quot;a&amp;quot;&lt;br /&gt;
  writeln(a(xy)); // prints 10&lt;br /&gt;
end.&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;code&amp;gt;a()&amp;lt;/code&amp;gt; takes a &amp;lt;code&amp;gt;Txy&amp;lt;/code&amp;gt; record as input and returns the integer value of the sum of the record&#039;s &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; fields (3 + 7).&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[First-class function]]&lt;br /&gt;
*[[Combinatory logic]]&lt;br /&gt;
*[[Function-level programming]]&lt;br /&gt;
*[[Functional programming]]&lt;br /&gt;
*[[Kappa calculus]] - a formalism for functions which &#039;&#039;excludes&#039;&#039; higher-order functions&lt;br /&gt;
*[[Strategy pattern]]&lt;br /&gt;
*[[Higher order message]]s&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
*[http://ergodicity.iamganesh.com/2006/08/07/higher-order-functions/ Higher-order functions and variational calculus]&lt;br /&gt;
*[http://boost.org/doc/html/lambda.html Boost Lambda Library for C++]&lt;br /&gt;
*[http://hop.perl.plover.com/book/ Higher Order Perl]&lt;br /&gt;
&lt;br /&gt;
[[Category:Functional programming]]&lt;br /&gt;
[[Category:Lambda calculus]]&lt;br /&gt;
[[Category:Higher-order functions| ]]&lt;br /&gt;
[[Category:Subroutines]]&lt;br /&gt;
[[Category:Articles with example Python code]]&lt;br /&gt;
[[Category:Articles with example Haskell code]]&lt;br /&gt;
[[Category:Articles with example Scheme code]]&lt;br /&gt;
[[Category:Articles with example Erlang code]]&lt;br /&gt;
[[Category:Articles with example JavaScript code]]&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example Pascal code]]&lt;/div&gt;</summary>
		<author><name>62.56.70.148</name></author>
	</entry>
</feed>