Higher-order functions: Difference between revisions

m
 
(7 intermediate revisions by 7 users not shown)
Line 502:
441
</pre>
 
=={{header|Binary Lambda Calculus}}==
Every BLC program uses higher order functions, since the parsed lambda term is applied to the remainder of input, which is, like everything in lambda calculus, itself a function. For example, the empty input is nil = <code>\x\y.y</code>. So the following minimal 4-bit BLC program passes nil to the identity function:
 
<pre>0010</pre>
 
=={{header|BQN}}==
Line 537 ⟶ 542:
 
p doit ->add 1 2 #prints 3</syntaxhighlight>
 
=={{header|Bruijn}}==
Everything in bruijn is a function (including strings and numbers), so even <syntaxhighlight lang="bruijn">main [0]</syntaxhighlight> would be a valid solution since the argument of <code>main</code> is already a functional encoding of stdin.
 
A more obvious example:
<syntaxhighlight lang="bruijn">
first [0 [[0]]]
 
second [first [[1]]]
 
:test (second) ([[[[0]]]])
</syntaxhighlight>
 
=={{header|Burlesque}}==
Line 1,252 ⟶ 1,269:
=={{header|Elena}}==
{{trans|Smalltalk}}
ELENA 46.1x :
<syntaxhighlight lang="elena">import extensions;
Line 1,258 ⟶ 1,275:
{
var first := (f => f());
var second := { ^ "second" };
console.printLine(first(second))
}</syntaxhighlight>
Line 1,557 ⟶ 1,574:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Higher-order_functions}}
 
'''Solution'''
 
'''Case 1 (from Rosetta code)''' Passing a function as an argument to another function.
 
The following function takes a function as its first parameter, and other two parameters, x and y. When this function "Do" is called, it will perform the given function taking as arguments the values x and y.
 
[[File:Fōrmulæ - Higher-order functions 01.png]]
 
[[File:Fōrmulæ - Higher-order functions 02.png]]
 
[[File:Fōrmulæ - Higher-order functions 03.png]]
 
An anonymous function (a lambda expression) can be passed directly:
 
[[File:Fōrmulæ - Higher-order functions 04.png]]
 
[[File:Fōrmulæ - Higher-order functions 05.png]]
 
For the next example, the function to be passed, when invoked, will perform several operation to the same two arguments. It will add them, subtract them, multiply them, divide them and power them. Finally it will return a list with the results.
 
[[File:Fōrmulæ - Higher-order functions 06.png]]
 
[[File:Fōrmulæ - Higher-order functions 07.png]]
 
Up now, however, it is the half of the story. Let us build an example where a function takes a function as parameter, and returns another function.
 
'''Case 2 (from Wikipedia)''' Passing a function as an argument to another function, and returning a function
 
The following function is a higher-order function. It takes as its unique parameter the function f. When the function is invoked, it will apply twice the function f and will return it. In other words, it will return a new function, which is the composition of the function f with itself.
 
[[File:Fōrmulæ - Higher-order functions 08.png]]
 
The next function is an ordinary one. It returns the values given as argument added with 3.
 
[[File:Fōrmulæ - Higher-order functions 09.png]]
 
In the next example, g is a dynamically created function.
 
[[File:Fōrmulæ - Higher-order functions 10.png]]
 
[[File:Fōrmulæ - Higher-order functions 11.png]]
 
Since the '''Apply twice''' function returns a function, it can be immediately invoked:
 
[[File:Fōrmulæ - Higher-order functions 12.png]]
 
[[File:Fōrmulæ - Higher-order functions 11.png]]
 
It can also take a pure symbol, in order to retrrieve a symbolic result:
 
[[File:Fōrmulæ - Higher-order functions 13.png]]
 
[[File:Fōrmulæ - Higher-order functions 14.png]]
 
=={{header|GAP}}==
Line 1,648 ⟶ 1,719:
demonstrate multiplication as "Mul";
end the story.</syntaxhighlight>
 
=={{Header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="insitux">
(function prepend-hello s
(str "Hello, " s))
 
(function modify-string f s
(f s))
 
(modify-string prepend-hello "World!")
</syntaxhighlight>
 
{{out}}
 
<pre>
Hello, World!
</pre>
 
=={{header|J}}==
Line 4,248 ⟶ 4,339:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var first = Fn.new { |f|
System.print("first function called")
f.call()
Line 4,262 ⟶ 4,353:
second function called
</pre>
 
 
=={{header|Z80 Assembly}}==
2,120

edits