First-class functions/Use numbers analogously: Difference between revisions

From Rosetta Code
Content added Content deleted
(Merge omitted languages and add Processing)
(Added FreeBASIC)
 
(24 intermediate revisions by 16 users not shown)
Line 22: Line 22:
<small>To paraphrase the task description: Do what was done before, but with numbers rather than functions</small>
<small>To paraphrase the task description: Do what was done before, but with numbers rather than functions</small>
<br><br>
<br><br>

=={{header|11l}}==
{{trans|Python}}

<syntaxhighlight lang="11l">V (x, xi, y, yi) = (2.0, 0.5, 4.0, 0.25)
V z = x + y
V zi = 1.0 / (x + y)
V multiplier = (n1, n2) -> (m -> @=n1 * @=n2 * m)
V numlist = [x, y, z]
V numlisti = [xi, yi, zi]
print(zip(numlist, numlisti).map((n, inversen) -> multiplier(inversen, n)(.5)))</syntaxhighlight>

{{out}}
<pre>
[0.5, 0.5, 0.5]
</pre>


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;
procedure Firstclass is
procedure Firstclass is
generic
generic
Line 45: Line 61:
end;
end;
end loop;
end loop;
end Firstclass;</lang>
end Firstclass;</syntaxhighlight>
{{out}}
{{out}}
<pre>5.00000E-01
<pre>5.00000E-01
Line 57: Line 73:


Note: Standard ALGOL 68's scoping rules forbids exporting a '''proc'''[edure] (or '''format''') out of it's scope (closure). Hence this specimen will run on [[ELLA ALGOL 68]], but is non-standard. For a discussion of first-class functions in ALGOL 68 consult [http://www.cs.ru.nl/~kees/home/papers/psi96.pdf "The Making of Algol 68"] - [[wp:Cornelis_H.A._Koster|C.H.A. Koster]] (1993). <!-- Retrieved April 28, 2007 -->
Note: Standard ALGOL 68's scoping rules forbids exporting a '''proc'''[edure] (or '''format''') out of it's scope (closure). Hence this specimen will run on [[ELLA ALGOL 68]], but is non-standard. For a discussion of first-class functions in ALGOL 68 consult [http://www.cs.ru.nl/~kees/home/papers/psi96.pdf "The Making of Algol 68"] - [[wp:Cornelis_H.A._Koster|C.H.A. Koster]] (1993). <!-- Retrieved April 28, 2007 -->
<lang algol68>REAL
<syntaxhighlight lang="algol68">REAL
x := 2,
x := 2,
xi := 0.5,
xi := 0.5,
Line 78: Line 94:
inv n = inv num list[key];
inv n = inv num list[key];
print ((multiplier(inv n, n)(.5), new line))
print ((multiplier(inv n, n)(.5), new line))
OD</lang>
OD</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 92: Line 108:
The First Class Functions example uses C. H. Lindsey's partial parameterization extension to Algol 68 which implemented in Algol 68G but not in algol68toc.
The First Class Functions example uses C. H. Lindsey's partial parameterization extension to Algol 68 which implemented in Algol 68G but not in algol68toc.
This example uses an alternative (technically, invalid Algol 68 as the author notes) accepted by algol68toc but not Algol 68G.
This example uses an alternative (technically, invalid Algol 68 as the author notes) accepted by algol68toc but not Algol 68G.

=={{header|Arturo}}==

<syntaxhighlight lang="arturo">x: 2.0
xi: 0.5
y: 4.0
yi: 0.25
z: x + y
zi: 1 / z

multiplier: function [m n][
function [a] with [m n][
a*m*n
]
]

couple @[x y z] @[xi yi zi]
| map 'p -> multiplier p\0 p\1
| map => [call & -> 0.5]
| print</syntaxhighlight>

{{out}}

<pre>0.5 0.5 0.5</pre>


=={{header|Axiom}}==
=={{header|Axiom}}==
<lang Axiom>(x,xi,y,yi) := (2.0,0.5,4.0,0.25)
<syntaxhighlight lang="axiom">(x,xi,y,yi) := (2.0,0.5,4.0,0.25)
(z,zi) := (x+y,1/(x+y))
(z,zi) := (x+y,1/(x+y))
(numbers,invers) := ([x,y,z],[xi,yi,zi])
(numbers,invers) := ([x,y,z],[xi,yi,zi])
multiplier(a:Float,b:Float):(Float->Float) == (m +-> a*b*m)
multiplier(a:Float,b:Float):(Float->Float) == (m +-> a*b*m)
[multiplier(number,inver) 0.5 for number in numbers for inver in invers]
[multiplier(number,inver) 0.5 for number in numbers for inver in invers]
</lang>Output:
</syntaxhighlight>Output:
<lang Axiom> [0.5,0.5,0.5]
<syntaxhighlight lang="axiom"> [0.5,0.5,0.5]
Type: List(Float)</lang>
Type: List(Float)</syntaxhighlight>
We can also curry functions, possibly with function composition, with the same output as before:
We can also curry functions, possibly with function composition, with the same output as before:
<lang Axiom>mult(n:Float):(Float->Float) == curryLeft(*$Float,n)$MAPPKG3(Float,Float,Float)
<syntaxhighlight lang="axiom">mult(n:Float):(Float->Float) == curryLeft(*$Float,n)$MAPPKG3(Float,Float,Float)
[mult(number*inver) 0.5 for number in numbers for inver in invers]
[mult(number*inver) 0.5 for number in numbers for inver in invers]
[(mult(number)*mult(inver)) 0.5 for number in numbers for inver in invers]</lang>
[(mult(number)*mult(inver)) 0.5 for number in numbers for inver in invers]</syntaxhighlight>
Using the Spad code in [[First-class functions#Axiom]], this can be done more economically using:
Using the Spad code in [[First-class functions#Axiom]], this can be done more economically using:
<lang Axiom>(map(mult,numbers)*map(mult,invers)) 0.5</lang>
<syntaxhighlight lang="axiom">(map(mult,numbers)*map(mult,invers)) 0.5</syntaxhighlight>
For comparison, [[First-class functions#Axiom]] gave:
For comparison, [[First-class functions#Axiom]] gave:
<lang Axiom>fns := [sin$Float, cos$Float, (x:Float):Float +-> x^3]
<syntaxhighlight lang="axiom">fns := [sin$Float, cos$Float, (x:Float):Float +-> x^3]
inv := [asin$Float, acos$Float, (x:Float):Float +-> x^(1/3)]
inv := [asin$Float, acos$Float, (x:Float):Float +-> x^(1/3)]
[(f*g) 0.5 for f in fns for g in inv]
[(f*g) 0.5 for f in fns for g in inv]
</syntaxhighlight>
</lang>
- which has the same output.
- which has the same output.


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> REM Create some numeric variables:
<syntaxhighlight lang="bbcbasic"> REM Create some numeric variables:
x = 2 : xi = 1/2
x = 2 : xi = 1/2
y = 4 : yi = 0.25
y = 4 : yi = 0.25
Line 144: Line 184:
DIM p% LEN(f$) + 4
DIM p% LEN(f$) + 4
$(p%+4) = f$ : !p% = p%+4
$(p%+4) = f$ : !p% = p%+4
= p%</lang>
= p%</syntaxhighlight>
'''Output:'''
'''Output:'''
<pre>
<pre>
Line 152: Line 192:
</pre>
</pre>
Compare with the implementation of First-class functions:
Compare with the implementation of First-class functions:
<lang bbcbasic> REM Create some functions and their inverses:
<syntaxhighlight lang="bbcbasic"> REM Create some functions and their inverses:
DEF FNsin(a) = SIN(a)
DEF FNsin(a) = SIN(a)
DEF FNasn(a) = ASN(a)
DEF FNasn(a) = ASN(a)
Line 185: Line 225:
DIM p% LEN(f$) + 4
DIM p% LEN(f$) + 4
$(p%+4) = f$ : !p% = p%+4
$(p%+4) = f$ : !p% = p%+4
= p%</lang>
= p%</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{works with|C#|4.0}}
{{works with|C#|4.0}}
The structure here is exactly the same as the C# entry in [[First-class functions]]. The "var" keyword allows us to use the same initialization code for an array of doubles as an array of functions. Note that variable names have been changed to correspond with the new functionality.
The structure here is exactly the same as the C# entry in [[First-class functions]]. The "var" keyword allows us to use the same initialization code for an array of doubles as an array of functions. Note that variable names have been changed to correspond with the new functionality.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Linq;


Line 217: Line 257:
}
}
}
}
</syntaxhighlight>
</lang>

=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <array>
#include <iostream>

int main()
{
double x = 2.0;
double xi = 0.5;
double y = 4.0;
double yi = 0.25;
double z = x + y;
double zi = 1.0 / ( x + y );

const std::array values{x, y, z};
const std::array inverses{xi, yi, zi};

auto multiplier = [](double a, double b)
{
return [=](double m){return a * b * m;};
};

for(size_t i = 0; i < values.size(); ++i)
{
auto new_function = multiplier(values[i], inverses[i]);
double value = new_function(i + 1.0);
std::cout << value << "\n";
}
}
</syntaxhighlight>
{{out}}
<pre>
1
2
3
</pre>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang clojure>(def x 2.0)
<syntaxhighlight lang="clojure">(def x 2.0)
(def xi 0.5)
(def xi 0.5)
(def y 4.0)
(def y 4.0)
Line 235: Line 311:
> (for [[n i] (zipmap numbers invers)]
> (for [[n i] (zipmap numbers invers)]
((multiplier n i) 0.5))
((multiplier n i) 0.5))
(0.5 0.5 0.5)</lang>
(0.5 0.5 0.5)</syntaxhighlight>
For comparison:
For comparison:
<lang clojure>
<syntaxhighlight lang="clojure">
(use 'clojure.contrib.math)
(use 'clojure.contrib.math)
(let [fns [#(Math/sin %) #(Math/cos %) (fn [x] (* x x x))]
(let [fns [#(Math/sin %) #(Math/cos %) (fn [x] (* x x x))]
inv [#(Math/asin %) #(Math/acos %) #(expt % 1/3)]]
inv [#(Math/asin %) #(Math/acos %) #(expt % 1/3)]]
(map #(% 0.5) (map #(comp %1 %2) fns inv)))
(map #(% 0.5) (map #(comp %1 %2) fns inv)))
</syntaxhighlight>
</lang>
Output:
Output:
<pre>(0.5 0.4999999999999999 0.5000000000000001)</pre>
<pre>(0.5 0.4999999999999999 0.5000000000000001)</pre>
Line 248: Line 324:
=={{header|Common Lisp}}==
=={{header|Common Lisp}}==


<lang lisp>(defun multiplier (f g)
<syntaxhighlight lang="lisp">(defun multiplier (f g)
#'(lambda (x) (* f g x)))
#'(lambda (x) (* f g x)))


Line 267: Line 343:
inverse
inverse
value
value
(funcall multiplier value))))</lang>
(funcall multiplier value))))</syntaxhighlight>


Output:
Output:
Line 277: Line 353:
The code from [[First-class functions]], for comparison:
The code from [[First-class functions]], for comparison:


<lang lisp>(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
<syntaxhighlight lang="lisp">(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
(defun cube (x) (expt x 3))
(defun cube (x) (expt x 3))
(defun cube-root (x) (expt x (/ 3)))
(defun cube-root (x) (expt x (/ 3)))
Line 289: Line 365:
function
function
value
value
(funcall composed value)))</lang>
(funcall composed value)))</syntaxhighlight>


Output:
Output:
Line 298: Line 374:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


auto multiplier(double a, double b)
auto multiplier(double a, double b)
Line 322: Line 398:
writefln("%f * %f * %f == %f", f[i], r[i], 1.0, mult(1));
writefln("%f * %f * %f == %f", f[i], r[i], 1.0, mult(1));
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>2.000000 * 0.500000 * 1.000000 == 1.000000
<pre>2.000000 * 0.500000 * 1.000000 == 1.000000
Line 332: Line 408:
This is written to have identical structure to [[First-class functions#E]], though the variable names are different.
This is written to have identical structure to [[First-class functions#E]], though the variable names are different.


<lang e>def x := 2.0
<syntaxhighlight lang="e">def x := 2.0
def xi := 0.5
def xi := 0.5
def y := 4.0
def y := 4.0
Line 349: Line 425:
def b := reverse[i]
def b := reverse[i]
println(`s = $s, a = $a, b = $b, multiplier($a, $b)($s) = ${multiplier(a, b)(s)}`)
println(`s = $s, a = $a, b = $b, multiplier($a, $b)($s) = ${multiplier(a, b)(s)}`)
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 358: Line 434:


Note: <code>def g := reverse[i]</code> is needed here because E as yet has no defined protocol for iterating over collections in parallel. [http://wiki.erights.org/wiki/Parallel_iteration Page for this issue.]
Note: <code>def g := reverse[i]</code> is needed here because E as yet has no defined protocol for iterating over collections in parallel. [http://wiki.erights.org/wiki/Parallel_iteration Page for this issue.]

=={{header|Elena}}==
=={{header|Elena}}==
{{trans|C#}}
{{trans|C#}}
ELENA 4.1 :
ELENA 6.x :
<lang elena>import system'routines;
<syntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions;
Line 373: Line 450:
real zi := 1.0r / (x + y);
real zi := 1.0r / (x + y);
var numlist := new real[]::( x, y, z );
var numlist := new real[]{ x, y, z };
var numlisti := new real[]::( xi, yi, zi );
var numlisti := new real[]{ xi, yi, zi };
var multiplied := numlist.zipBy(numlisti, (n1,n2 => (m => n1 * n2 * m) )).toArray();
var multiplied := numlist.zipBy(numlisti, (n1,n2 => (m => n1 * n2 * m) )).toArray();
multiplied.forEach:(multiplier){ console.printLine(multiplier(0.5r)) }
multiplied.forEach::(multiplier){ console.printLine(multiplier(0.5r)) }
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 388: Line 465:


=={{header|Erlang}}==
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( first_class_functions_use_numbers ).
-module( first_class_functions_use_numbers ).


Line 402: Line 479:


multiplier( N1, N2 ) -> fun(M) -> N1 * N2 * M end.
multiplier( N1, N2 ) -> fun(M) -> N1 * N2 * M end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 411: Line 488:
</pre>
</pre>


=={{header|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
let x = 2.0
let x = 2.0
let xi = 0.5
let xi = 0.5
Line 426: Line 503:
|> List.map ((|>) 0.5)
|> List.map ((|>) 0.5)
|> printfn "%A"
|> printfn "%A"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 434: Line 511:
=={{header|Factor}}==
=={{header|Factor}}==
Compared to http://rosettacode.org/wiki/First-class_functions, the call to "compose" is replaced with the call to "mutliplier"
Compared to http://rosettacode.org/wiki/First-class_functions, the call to "compose" is replaced with the call to "mutliplier"
<lang factor>USING: arrays kernel literals math prettyprint sequences ;
<syntaxhighlight lang="factor">USING: arrays kernel literals math prettyprint sequences ;
IN: q
IN: q


Line 451: Line 528:
: example ( -- )
: example ( -- )
0.5 A B create-all
0.5 A B create-all
[ call( x -- y ) ] with map . ;</lang>
[ call( x -- y ) ] with map . ;</syntaxhighlight>
{{out}}
{{out}}
<pre>{ 0.5 0.5 0.5 }</pre>
<pre>{ 0.5 0.5 0.5 }</pre>
Line 457: Line 534:
=={{header|Fantom}}==
=={{header|Fantom}}==


<lang fantom>
<syntaxhighlight lang="fantom">
class Main
class Main
{
{
Line 478: Line 555:
}
}
}
}
</syntaxhighlight>
</lang>


The <code>combine</code> function is very similar to the <code>compose</code> function in 'First-class functions'. In both cases a new function is returned:
The <code>combine</code> function is very similar to the <code>compose</code> function in 'First-class functions'. In both cases a new function is returned:


<lang fantom>
<syntaxhighlight lang="fantom">
static |Obj -> Obj| compose (|Obj -> Obj| fn1, |Obj -> Obj| fn2)
static |Obj -> Obj| compose (|Obj -> Obj| fn1, |Obj -> Obj| fn2)
{
{
return |Obj x -> Obj| { fn2 (fn1 (x)) }
return |Obj x -> Obj| { fn2 (fn1 (x)) }
}
}
</syntaxhighlight>
</lang>

=={{header|FreeBASIC}}==
FreeBASIC does not support first-class functions or function closures, which means that you cannot create a function that returns another function or that has a function defined inside it.

However, similar behavior can be achieved with subroutines and global variables.
<syntaxhighlight lang="vbnet">Dim As Double x = 2.0, xi = 0.5
Dim As Double y = 4.0, yi = 0.25
Dim As Double z = x + y, zi = 1.0 / (x + y)
Dim As Double values(2) = {x, y, z}
Dim As Double inverses(2) = {xi, yi, zi}

Dim Shared As Double m = 0.5

Function multiplier(a As Double, d As Double) As Double
Return a * d * m
End Function

For i As Byte = 0 To Ubound(values)
Dim As Double new_function = multiplier(values(i), inverses(i))
Print values(i); " *"; inverses(i); " *"; m; " ="; new_function
Next i

Sleep</syntaxhighlight>
{{out}}
<pre> 2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.1666666666666667 * 0.5 = 0.5</pre>


=={{header|Go}}==
=={{header|Go}}==
Line 503: Line 607:
At point C, numbers and their inverses have been multiplied and bound to first class functions. The ordered collection arrays could be modified at this point and the function objects would be unaffected.
At point C, numbers and their inverses have been multiplied and bound to first class functions. The ordered collection arrays could be modified at this point and the function objects would be unaffected.


<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 538: Line 642:
return n1n2 * m
return n1n2 * m
}
}
}</lang>
}</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 561: Line 665:


[[/Go interface type|This can also be done with an interface type]] rather than the "empty interface" (<code>interface{}</code>) for better type safety and to avoid the <code>eval</code> function and type switch.
[[/Go interface type|This can also be done with an interface type]] rather than the "empty interface" (<code>interface{}</code>) for better type safety and to avoid the <code>eval</code> function and type switch.
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 608: Line 712:
panic("unsupported multiplier type")
panic("unsupported multiplier type")
return 0 // never reached
return 0 // never reached
}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==


<lang groovy>def multiplier = { n1, n2 -> { m -> n1 * n2 * m } }
<syntaxhighlight lang="groovy">def multiplier = { n1, n2 -> { m -> n1 * n2 * m } }


def ε = 0.00000001 // tolerance(epsilon): acceptable level of "wrongness" to account for rounding error
def ε = 0.00000001 // tolerance(epsilon): acceptable level of "wrongness" to account for rounding error
Line 622: Line 726:
}
}
println()
println()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2.000 * 0.500 * 1.000 == 1.000
<pre>2.000 * 0.500 * 1.000 == 1.000
Line 644: Line 748:
=={{header|Haskell}}==
=={{header|Haskell}}==


<lang haskell>module Main
<syntaxhighlight lang="haskell">module Main
where
where


Line 671: Line 775:
in printf "%f * %f * 0.5 = %f\n" number inverse (new_function 0.5)
in printf "%f * %f * 0.5 = %f\n" number inverse (new_function 0.5)
mapM_ print_pair pairs
mapM_ print_pair pairs
</syntaxhighlight>
</lang>


This is very close to the first-class functions example, but given as a full Haskell program rather than an interactive session.
This is very close to the first-class functions example, but given as a full Haskell program rather than an interactive session.
Line 682: Line 786:
[[First-class functions]] task solution. The solution here
[[First-class functions]] task solution. The solution here
is simpler and more direct since it handles a specific function definition.
is simpler and more direct since it handles a specific function definition.
<lang unicon>import Utils
<syntaxhighlight lang="unicon">import Utils


procedure main(A)
procedure main(A)
Line 691: Line 795:
procedure multiplier(n1,n2)
procedure multiplier(n1,n2)
return makeProc { repeat inVal := n1 * n2 * (inVal@&source)[1] }
return makeProc { repeat inVal := n1 * n2 * (inVal@&source)[1] }
end</lang>
end</syntaxhighlight>


A sample run:
A sample run:
Line 707: Line 811:
This seems to satisfy the new problem statement:
This seems to satisfy the new problem statement:


<lang j> x =: 2.0
<syntaxhighlight lang="j"> x =: 2.0
xi =: 0.5
xi =: 0.5
y =: 4.0
y =: 4.0
Line 717: Line 821:
rev =: xi,yi,zi
rev =: xi,yi,zi


multiplier =: 2 : 'm * n * ]'</lang>
multiplier =: 2 : 'm * n * ]'</syntaxhighlight>

An equivalent but perhaps prettier definition of multiplier would be:

<syntaxhighlight lang="j">multiplier=: {{m*n*]}}</syntaxhighlight>

Or, if J's "right bracket is the right identity function" bothers you, you might prefer the slightly more verbose but still equivalent:

<syntaxhighlight lang="j">multiplier=: {{m*n*{{y}}}}</syntaxhighlight>


Example use:
Example use:


<lang> fwd multiplier rev 0.5
<syntaxhighlight lang="text"> fwd multiplier rev 0.5
0.5 0.5 0.5</lang>
0.5 0.5 0.5</syntaxhighlight>


For contrast, here are the final results from [[First-class functions#J]]:
For contrast, here are the final results from [[First-class functions#J]]:
<lang> BA unqcol 0.5
<syntaxhighlight lang="text"> BA unqcol 0.5
0.5 0.5 0.5 0.5</lang>
0.5 0.5 0.5 0.5</syntaxhighlight>


===Tacit (unorthodox) version===
===Tacit (unorthodox) version===
Although the pseudo-code to generate the numbers can certainly be written (see above [http://rosettacode.org/wiki/First-class_functions/Use_numbers_analogously#Explicit_version Explicit version] ) this is not done for this version because it would destroy part of the analogy (J encourages, from the programming perspective, to process all components at once as opposed to one component at a time). In addition, this version is done in terms of boxed lists of numbers instead of plain list of numbers, again, to preserve the analogy.
Although the pseudo-code to generate the numbers can certainly be written (see above [http://rosettacode.org/wiki/First-class_functions/Use_numbers_analogously#Explicit_version Explicit version] ) this is not done for this version because it would destroy part of the analogy (J encourages, from the programming perspective, to process all components at once as opposed to one component at a time). In addition, this version is done in terms of boxed lists of numbers instead of plain list of numbers, again, to preserve the analogy.


<lang> multiplier=. train@:((;:'&*') ;~ an@: *)
<syntaxhighlight lang="text"> multiplier=. train@:((;:'&*') ;~ an@: *)
]A=. 2 ; 4 ; (2 + 4) NB. Corresponds to ]A=. box (1&o.)`(2&o.)`(^&3)
]A=. 2 ; 4 ; (2 + 4) NB. Corresponds to ]A=. box (1&o.)`(2&o.)`(^&3)
Line 746: Line 858:
└───┴───┴───┘
└───┴───┴───┘
BA of &> 0.5 NB. Corresponds to BA of &> 0.5 (exactly)
BA of &> 0.5 NB. Corresponds to BA of &> 0.5 (exactly)
0.5 0.5 0.5</lang>
0.5 0.5 0.5</syntaxhighlight>


Please refer to [http://rosettacode.org/wiki/First-class_functions#Tacit_.28unorthodox.29_version First-class functions tacit (unorthodox) version] for the definitions of the functions train, an and of.
Please refer to [http://rosettacode.org/wiki/First-class_functions#Tacit_.28unorthodox.29_version First-class functions tacit (unorthodox) version] for the definitions of the functions train, an and of.

=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

public class FirstClassFunctionsUseNumbersAnalogously {

public static void main(String[] args) {
final double x = 2.0, xi = 0.5,
y = 4.0, yi = 0.25,
z = x + y, zi = 1.0 / ( x + y );

List<Double> list = List.of( x, y, z );
List<Double> inverseList = List.of( xi, yi, zi );
BiFunction<Double, Double, Function<Double, Double>> multiplier = (a, b) -> product -> a * b * product;
for ( int i = 0; i < list.size(); i++ ) {
Function<Double, Double> multiply = multiplier.apply(list.get(i), inverseList.get(i));
final double argument = (double) ( i + 1 );
System.out.println(multiply.apply(argument));
}
}

}
</syntaxhighlight>
{{ out }}
<pre>
1.0
2.0
3.0
</pre>


=={{header|jq}}==
=={{header|jq}}==
It may be helpful to compare the following definition of "multiplier" with its Ruby counterpart [[#Ruby|below]]. Whereas the Ruby definition must name all its positional parameters, the jq equivalent is defined as a filter that obtains them implicitly from its input.
It may be helpful to compare the following definition of "multiplier" with its Ruby counterpart [[#Ruby|below]]. Whereas the Ruby definition must name all its positional parameters, the jq equivalent is defined as a filter that obtains them implicitly from its input.
<lang jq># Infrastructure:
<syntaxhighlight lang="jq"># Infrastructure:
# zip this and that
# zip this and that
def zip(that):
def zip(that): [., that] | transpose;
. as $this | reduce range(0;length) as $i ([]; . + [ [$this[$i], that[$i]] ]);


# The task:
# The task:
Line 772: Line 917:
def multiplier(j): .[0] * .[1] * j;
def multiplier(j): .[0] * .[1] * j;


numlist|zip(invlist) | map( multiplier(0.5) )</lang>
numlist | zip(invlist) | map( multiplier(0.5) )</syntaxhighlight>
{{out}}
{{out}}
$ jq -n -c -f First_class_functions_Use_numbers_analogously.jq
$ jq -n -c -f First_class_functions_Use_numbers_analogously.jq
[0.5,0.5,0.5]
[0.5,0.5,0.5]


As of this writing, there is no entry for jq at [[First-class functions]].
<!-- [[First-class functions#jq]]. -->

=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">const x = 2.0;
const xi = 0.5;
const y = 4.0;
const yi = 0.25;
const z = x + y;
const zi = 1.0 / (x + y);
const pairs = [[x, xi], [y, yi], [z, zi]];
const testVal = 0.5;

const multiplier = (a, b) => m => a * b * m;

const test = () => {
return pairs.map(([a, b]) => {
const f = multiplier(a, b);
const result = f(testVal);
return `${a} * ${b} * ${testVal} = ${result}`;
});
}

test().join('\n');</syntaxhighlight>
{{out}}
<pre>
2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.16666666666666666 * 0.5 = 0.5
</pre>


=={{header|Julia}}==
=={{header|Julia}}==
Line 784: Line 957:
In Julia, like Python and R, functions can be treated as like as other Types.
In Julia, like Python and R, functions can be treated as like as other Types.


<lang julia>x, xi = 2.0, 0.5
<syntaxhighlight lang="julia">x, xi = 2.0, 0.5
y, yi = 4.0, 0.25
y, yi = 4.0, 0.25
z, zi = x + y, 1.0 / ( x + y )
z, zi = x + y, 1.0 / ( x + y )
Line 793: Line 966:
numlisti = [xi, yi, zi]
numlisti = [xi, yi, zi]


@show collect(multiplier(n, invn)(0.5) for (n, invn) in zip(numlist, numlisti))</lang>
@show collect(multiplier(n, invn)(0.5) for (n, invn) in zip(numlist, numlisti))</syntaxhighlight>


{{out}}
{{out}}
Line 799: Line 972:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
<syntaxhighlight lang="scala">// version 1.1.2


fun multiplier(n1: Double, n2: Double) = { m: Double -> n1 * n2 * m}
fun multiplier(n1: Double, n2: Double) = { m: Double -> n1 * n2 * m}
Line 816: Line 989:
println("${multiplier(a[i], ai[i])(m)} = multiplier(${a[i]}, ${ai[i]})($m)")
println("${multiplier(a[i], ai[i])(m)} = multiplier(${a[i]}, ${ai[i]})($m)")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 826: Line 999:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang="lua">
<lang Lua>
-- This function returns another function that
-- This function returns another function that
-- keeps n1 and n2 in scope, ie. a closure.
-- keeps n1 and n2 in scope, ie. a closure.
Line 846: Line 1,019:
print(v .. " * " .. invs[k] .. " * 0.5 = " .. new_function(0.5))
print(v .. " * " .. invs[k] .. " * 0.5 = " .. new_function(0.5))
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 855: Line 1,028:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
Module CheckIt {
\\ by default numbers are double
\\ by default numbers are double
Line 889: Line 1,062:
}
}


</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 896: Line 1,069:
6.00 * 0.17 * 0.50 = 0.50
6.00 * 0.17 * 0.50 = 0.50
</pre>
</pre>



=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 902: Line 1,074:
This code demonstrates the example using structure similar to function composition, however the composition function is replace with the multiplier function.
This code demonstrates the example using structure similar to function composition, however the composition function is replace with the multiplier function.


<lang Mathematica>multiplier[n1_,n2_]:=n1 n2 #&
<syntaxhighlight lang="mathematica">multiplier[n1_,n2_]:=n1 n2 #&
num={2,4,2+4};
num={2,4,2+4};
numi=1/num;
numi=1/num;
multiplierfuncs = multiplier @@@ Transpose[{num, numi}];
multiplierfuncs = multiplier @@@ Transpose[{num, numi}];
</syntaxhighlight>
</lang>


The resulting functions are unity multipliers:
The resulting functions are unity multipliers:
Line 914: Line 1,086:


Note that unlike Composition, the above definition of multiplier only allows for exactly two arguments. The definition can be changed to allow any nonzero number of arguments:
Note that unlike Composition, the above definition of multiplier only allows for exactly two arguments. The definition can be changed to allow any nonzero number of arguments:
<lang Mathematica>multiplier[arg__] := Times[arg, #] &
<syntaxhighlight lang="mathematica">multiplier[arg__] := Times[arg, #] &
</syntaxhighlight>
</lang>


=={{header|Nemerle}}==
=={{header|Nemerle}}==
{{trans|Python}}
{{trans|Python}}
<lang Nemerle>using System;
<syntaxhighlight lang="nemerle">using System;
using System.Console;
using System.Console;
using Nemerle.Collections.NCollectionsExtensions;
using Nemerle.Collections.NCollectionsExtensions;
Line 935: Line 1,107:
WriteLine($[multiplier(n, m) (0.5)|(n, m) in ZipLazy(nums, inums)]);
WriteLine($[multiplier(n, m) (0.5)|(n, m) in ZipLazy(nums, inums)]);
}
}
}</lang>
}</syntaxhighlight>


=={{header|Never}}==
=={{header|Never}}==
<syntaxhighlight lang="never">
<lang Never>
func multiplier(a : float, b : float) -> (float) -> float {
func multiplier(a : float, b : float) -> (float) -> float {
let func(m : float) -> float { a * b * m }
let func(m : float) -> float { a * b * m }
Line 963: Line 1,135:
0
0
}
}
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 970: Line 1,142:
6.00 * 0.17 * 1.00 = 1.00
6.00 * 0.17 * 1.00 = 1.00
</pre>
</pre>

=={{header|Nim}}==
<syntaxhighlight lang="nim">
func multiplier(a, b: float): auto =
let ab = a * b
result = func(c: float): float = ab * c

let
x = 2.0
xi = 0.5
y = 4.0
yi = 0.25
z = x + y
zi = 1.0 / ( x + y )

let list = [x, y, z]
let invlist = [xi, yi, zi]

for i in 0..list.high:
# Create a multiplier function...
let f = multiplier(list[i], invlist[i])
# ... and apply it.
echo f(0.5)</syntaxhighlight>

{{out}}
<pre>0.5
0.5
0.5</pre>


=={{header|Objeck}}==
=={{header|Objeck}}==
Similar however this code does not generate a list of functions.
Similar however this code does not generate a list of functions.
<lang objeck>use Collection.Generic;
<syntaxhighlight lang="objeck">use Collection.Generic;


class FirstClass {
class FirstClass {
Line 1,002: Line 1,202:
}
}
}
}
</syntaxhighlight>
</lang>


{{output}}
{{output}}
Line 1,013: Line 1,213:
=={{header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml># let x = 2.0;;
<syntaxhighlight lang="ocaml"># let x = 2.0;;


# let y = 4.0;;
# let y = 4.0;;
Line 1,033: Line 1,233:
(* or just apply the generated function immediately... *)
(* or just apply the generated function immediately... *)
# List.map2 (fun n inv -> (multiplier n inv) 0.5) coll inv_coll;;
# List.map2 (fun n inv -> (multiplier n inv) 0.5) coll inv_coll;;
- : float list = [0.5; 0.5; 0.5]</lang>
- : float list = [0.5; 0.5; 0.5]</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>: multiplier(n1, n2) #[ n1 n2 * * ] ;
<syntaxhighlight lang="oforth">: multiplier(n1, n2) #[ n1 n2 * * ] ;


: firstClassNum
: firstClassNum
Line 1,047: Line 1,247:
x y + ->z
x y + ->z
x y + inv ->zi
x y + inv ->zi
[ x, y, z ] [ xi, yi, zi ] zipWith(#multiplier) map(#[ 0.5 swap perform ] ) . ;</lang>
[ x, y, z ] [ xi, yi, zi ] zipWith(#multiplier) map(#[ 0.5 swap perform ] ) . ;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,054: Line 1,254:


=={{header|Oz}}==
=={{header|Oz}}==
<lang oz>declare
<syntaxhighlight lang="oz">declare


[X Y Z] = [2.0 4.0 Z=X+Y]
[X Y Z] = [2.0 4.0 Z=X+Y]
Line 1,072: Line 1,272:
do
do
{Show {{Multiplier N I} 0.5}}
{Show {{Multiplier N I} 0.5}}
end</lang>
end</syntaxhighlight>


"Multiplier" is like "Compose", but with multiplication instead of function application. Otherwise the code is identical except for the argument types (numbers instead of functions).
"Multiplier" is like "Compose", but with multiplication instead of function application. Otherwise the code is identical except for the argument types (numbers instead of functions).
Line 1,078: Line 1,278:
=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
{{works with|PARI/GP|2.4.2 and above}}
{{works with|PARI/GP|2.4.2 and above}}
<lang parigp>multiplier(n1,n2)={
<syntaxhighlight lang="parigp">multiplier(n1,n2)={
x -> n1 * n2 * x
x -> n1 * n2 * x
};
};
Line 1,087: Line 1,287:
print(multiplier(y,yi)(0.5));
print(multiplier(y,yi)(0.5));
print(multiplier(z,zi)(0.5));
print(multiplier(z,zi)(0.5));
};</lang>
};</syntaxhighlight>
The two are very similar, though as requested the test numbers are in 6 variables instead of two vectors.
The two are very similar, though as requested the test numbers are in 6 variables instead of two vectors.

=={{header|Pascal}}==
Works with FPC (currently only version 3.3.1).
<syntaxhighlight lang="pascal">
program FunTest;
{$mode objfpc}
{$modeswitch functionreferences}
{$modeswitch anonymousfunctions}
uses
SysUtils;

type
TMultiplier = reference to function(n: Double): Double;

function GetMultiplier(a, b: Double): TMultiplier;
var
prod: Double;
begin
prod := a * b;
Result := function(n: Double): Double begin Result := prod * n end;
end;

var
Multiplier: TMultiplier;
I: Integer;
x, xi, y, yi: Double;
Numbers, Inverses: array of Double;
begin
x := 2.0;
xi := 0.5;
y := 4.0;
yi := 0.25;
Numbers := [x, y, x + y];
Inverses := [xi, yi, 1.0 / (x + y)];
for I := 0 to High(Numbers) do begin
Multiplier := GetMultiplier(Numbers[I], Inverses[I]);
WriteLn(Multiplier(0.5));
end;
end.
</syntaxhighlight>
{{out}}
<pre>
5.0000000000000000E-001
5.0000000000000000E-001
5.0000000000000000E-001
</pre>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>sub multiplier {
<syntaxhighlight lang="perl">sub multiplier {
my ( $n1, $n2 ) = @_;
my ( $n1, $n2 ) = @_;
sub {
sub {
Line 1,111: Line 1,357:
print multiplier( $number, $inverse )->(0.5), "\n";
print multiplier( $number, $inverse )->(0.5), "\n";
}
}
</syntaxhighlight>
</lang>
Output:
Output:
<pre>0.5
<pre>0.5
Line 1,118: Line 1,364:
</pre>
</pre>
The entry in first-class functions uses the same technique:
The entry in first-class functions uses the same technique:
<lang perl>sub compose {
<syntaxhighlight lang="perl">sub compose {
my ($f, $g) = @_;
my ($f, $g) = @_;
Line 1,127: Line 1,373:
...
...
compose($flist1[$_], $flist2[$_]) -> (0.5)
compose($flist1[$_], $flist2[$_]) -> (0.5)
</syntaxhighlight>
</lang>

=={{header|Perl 6}}==
{{works with|Rakudo|2015-09-10}}
<lang perl6>sub multiplied ($g, $f) { return { $g * $f * $^x } }
my $x = 2.0;
my $xi = 0.5;
my $y = 4.0;
my $yi = 0.25;
my $z = $x + $y;
my $zi = 1.0 / ( $x + $y );

my @numbers = $x, $y, $z;
my @inverses = $xi, $yi, $zi;
for flat @numbers Z @inverses { say multiplied($^g, $^f)(.5) }</lang>
Output:
<pre>0.5
0.5
0.5</pre>
The structure of this is identical to first-class function task.


=={{header|Phix}}==
=={{header|Phix}}==
Just as there is no real support for first class functions, not much that is pertinent to this task for numbers either, but the manual way is just as trivial:
Just as there is no real support for first class functions, not much that is pertinent to this task for numbers either, but the manual way is just as trivial:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence mtable = {}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>

<span style="color: #004080;">sequence</span> <span style="color: #000000;">mtable</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function multiplier(atom n1, atom n2)
mtable = append(mtable,{n1,n2})
<span style="color: #008080;">function</span> <span style="color: #000000;">multiplier</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">n2</span><span style="color: #0000FF;">)</span>
return length(mtable)
<span style="color: #000000;">mtable</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mtable</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mtable</span><span style="color: #0000FF;">)</span>

<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function call_multiplier(integer f, atom m)
atom {n1,n2} = mtable[f]
<span style="color: #008080;">function</span> <span style="color: #000000;">call_multiplier</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
return n1*n2*m
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">n1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mtable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">f</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">n1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">m</span>

<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant x = 2,
xi = 0.5,
<span style="color: #008080;">constant</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
y = 4,
<span style="color: #000000;">xi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span>
yi = 0.25,
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span>
z = x + y,
<span style="color: #000000;">yi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.25</span><span style="color: #0000FF;">,</span>
zi = 1 / ( x + y )
<span style="color: #000000;">z</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span>

<span style="color: #000000;">zi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">)</span>
?call_multiplier(multiplier(x,xi),0.5)
?call_multiplier(multiplier(y,yi),0.5)
<span style="color: #0000FF;">?</span><span style="color: #000000;">call_multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xi</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
?call_multiplier(multiplier(z,zi),0.5)</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">call_multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">yi</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">call_multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">multiplier</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zi</span><span style="color: #0000FF;">),</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,183: Line 1,411:


Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient.
Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence ctable = {}
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>

<span style="color: #004080;">sequence</span> <span style="color: #000000;">ctable</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function compose(integer f, integer g)
ctable = append(ctable,{f,g})
<span style="color: #008080;">function</span> <span style="color: #000000;">compose</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">)</span>
return length(ctable)
<span style="color: #000000;">ctable</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ctable</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #004080;">integer</span> <span style="color: #000000;">cdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ctable</span><span style="color: #0000FF;">)</span>

<span style="color: #008080;">return</span> <span style="color: #000000;">cdx</span>
function call_composite(integer f, atom x)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
integer g
{f,g} = ctable[f]
<span style="color: #008080;">function</span> <span style="color: #000000;">call_composite</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">cdx</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
return call_func(f,{call_func(g,{x})})
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">g</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ctable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cdx</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">(</span><span style="color: #000000;">g</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">))</span>

<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function plus1(atom x)
return x+1
<span style="color: #008080;">function</span> <span style="color: #000000;">plus1</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>

<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function halve(atom x)
return x/2
<span style="color: #008080;">function</span> <span style="color: #000000;">halve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span>

<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant m = compose(routine_id("halve"),routine_id("plus1"))

<span style="color: #008080;">constant</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">compose</span><span style="color: #0000FF;">(</span><span style="color: #000000;">halve</span><span style="color: #0000FF;">,</span><span style="color: #000000;">plus1</span><span style="color: #0000FF;">)</span>
?call_composite(m,1) -- displays 1
?call_composite(m,4) -- displays 2.5</lang>
<span style="color: #0000FF;">?</span><span style="color: #000000;">call_composite</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- displays 1</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">call_composite</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- displays 2.5</span>
<!--</syntaxhighlight>-->


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(load "@lib/math.l")
<syntaxhighlight lang="picolisp">(load "@lib/math.l")


(de multiplier (N1 N2)
(de multiplier (N1 N2)
Line 1,221: Line 1,452:
(prinl (format ((multiplier Inv Num) 0.5) *Scl)) )
(prinl (format ((multiplier Inv Num) 0.5) *Scl)) )
(list X Y Z)
(list X Y Z)
(list Xi Yi Zi) ) )</lang>
(list Xi Yi Zi) ) )</syntaxhighlight>
Output:
Output:
<pre>0.500000
<pre>0.500000
Line 1,229: Line 1,460:
that the function 'multiplier' above accepts two numbers, while 'compose'
that the function 'multiplier' above accepts two numbers, while 'compose'
below accepts two functions:
below accepts two functions:
<lang PicoLisp>(load "@lib/math.l")
<syntaxhighlight lang="picolisp">(load "@lib/math.l")


(de compose (F G)
(de compose (F G)
Line 1,245: Line 1,476:
(prinl (format ((compose Inv Fun) 0.5) *Scl)) )
(prinl (format ((compose Inv Fun) 0.5) *Scl)) )
'(sin cos cube)
'(sin cos cube)
'(asin acos cubeRoot) )</lang>
'(asin acos cubeRoot) )</syntaxhighlight>
With a similar output:
With a similar output:
<pre>0.500001
<pre>0.500001
Line 1,253: Line 1,484:
=={{header|Python}}==
=={{header|Python}}==
This new task:
This new task:
<lang python>IDLE 2.6.1
<syntaxhighlight lang="python">IDLE 2.6.1
>>> # Number literals
>>> # Number literals
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25
Line 1,267: Line 1,498:
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)]
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)]
[0.5, 0.5, 0.5]
[0.5, 0.5, 0.5]
>>></lang>
>>></syntaxhighlight>


The Python solution to First-class functions for comparison:
The Python solution to First-class functions for comparison:
<lang python>>>> # Some built in functions and their inverses
<syntaxhighlight lang="python">>>> # Some built in functions and their inverses
>>> from math import sin, cos, acos, asin
>>> from math import sin, cos, acos, asin
>>> # Add a user defined function and its inverse
>>> # Add a user defined function and its inverse
Line 1,284: Line 1,515:
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)]
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)]
[0.5, 0.4999999999999999, 0.5]
[0.5, 0.4999999999999999, 0.5]
>>></lang>
>>></syntaxhighlight>
As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example.
As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example.


=={{header|R}}==
=={{header|R}}==
<lang R>multiplier <- function(n1,n2) { (function(m){n1*n2*m}) }
<syntaxhighlight lang="r">multiplier <- function(n1,n2) { (function(m){n1*n2*m}) }
x = 2.0
x = 2.0
xi = 0.5
xi = 0.5
Line 1,302: Line 1,533:
Output
Output
[1] 0.5 0.5 0.5
[1] 0.5 0.5 0.5
</syntaxhighlight>
</lang>


Compared to original first class functions
Compared to original first class functions
<lang R>sapply(mapply(compose,f1,f2),do.call,list(.5))
<syntaxhighlight lang="r">sapply(mapply(compose,f1,f2),do.call,list(.5))
[1] 0.5 0.5 0.5</lang>
[1] 0.5 0.5 0.5</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==


<syntaxhighlight lang="racket">
<lang Racket>
#lang racket
#lang racket


Line 1,328: Line 1,559:
((multiplier n i) 0.5))
((multiplier n i) 0.5))
;; -> '(0.5 0.5 0.5)
;; -> '(0.5 0.5 0.5)
</syntaxhighlight>
</lang>

=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2015-09-10}}
<syntaxhighlight lang="raku" line>sub multiplied ($g, $f) { return { $g * $f * $^x } }
my $x = 2.0;
my $xi = 0.5;
my $y = 4.0;
my $yi = 0.25;
my $z = $x + $y;
my $zi = 1.0 / ( $x + $y );

my @numbers = $x, $y, $z;
my @inverses = $xi, $yi, $zi;
for flat @numbers Z @inverses { say multiplied($^g, $^f)(.5) }</syntaxhighlight>
Output:
<pre>0.5
0.5
0.5</pre>
The structure of this is identical to first-class function task.


=={{header|REXX}}==
=={{header|REXX}}==
The REXX language doesn't have an easy method to call functions by using a variable name,
The REXX language doesn't have an easy method to call functions by using a variable name,
<br>but the '''interpret''' instruction can be used to provide that capability.
<br>but the '''interpret''' instruction can be used to provide that capability.
<lang rexx>/*REXX program to use a first-class function to use numbers analogously. */
<syntaxhighlight lang="rexx">/*REXX program to use a first-class function to use numbers analogously. */
nums= 2.0 4.0 6.0 /*various numbers, can have fractions.*/
nums= 2.0 4.0 6.0 /*various numbers, can have fractions.*/
invs= 1/2.0 1/4.0 1/6.0 /*inverses of the above (real) numbers.*/
invs= 1/2.0 1/4.0 1/6.0 /*inverses of the above (real) numbers.*/
Line 1,345: Line 1,598:
@: return left( arg(1) / 1, 15) /*format the number, left justified. */
@: return left( arg(1) / 1, 15) /*format the number, left justified. */
multiplier: procedure expose n1n2; parse arg n1,n2; n1n2= n1 * n2; return 'a_new_func'
multiplier: procedure expose n1n2; parse arg n1,n2; n1n2= n1 * n2; return 'a_new_func'
a_new_func: return n1n2 * arg(1)</lang>
a_new_func: return n1n2 * arg(1)</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<pre>
Line 1,354: Line 1,607:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
<syntaxhighlight lang="ruby">multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
numlist = [x=2, y=4, x+y]
numlist = [x=2, y=4, x+y]
invlist = [0.5, 0.25, 1.0/(x+y)]
invlist = [0.5, 0.25, 1.0/(x+y)]
p numlist.zip(invlist).map {|n, invn| multiplier[invn, n][0.5]}
p numlist.zip(invlist).map {|n, invn| multiplier[invn, n][0.5]}
# => [0.5, 0.5, 0.5]</lang>
# => [0.5, 0.5, 0.5]</syntaxhighlight>


This structure is identical to the treatment of Ruby's [[First-class functions#Ruby|first-class functions]] -- create a Proc object that returns a Proc object (a closure). These examples show that 0.5 times number ''n'' (or passed to function ''f'') times inverse of ''n'' (or passed to inverse of ''f'') returns the original number, 0.5.
This structure is identical to the treatment of Ruby's [[First-class functions#Ruby|first-class functions]] -- create a Proc object that returns a Proc object (a closure). These examples show that 0.5 times number ''n'' (or passed to function ''f'') times inverse of ''n'' (or passed to inverse of ''f'') returns the original number, 0.5.


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>#![feature(conservative_impl_trait)]
<syntaxhighlight lang="rust">#![feature(conservative_impl_trait)]
fn main() {
fn main() {
let (x, xi) = (2.0, 0.5);
let (x, xi) = (2.0, 0.5);
Line 1,383: Line 1,636:
move |m| x*y*m
move |m| x*y*m
}
}
</syntaxhighlight>
</lang>


This is very similar to the [[First-class functions#Rust|first-class functions]] implementation save that the type inference works a little bit better here (e.g. when declaring <code>numlist</code> and <code>invlist</code>) and <code>multiplier</code>'s declaration is substantially simpler than <code>compose</code>'s. Both of these boil down to the fact that closures and regular functions are actually different types in Rust so we have to be generic over them but here we are only dealing with 64-bit floats.
This is very similar to the [[First-class functions#Rust|first-class functions]] implementation save that the type inference works a little bit better here (e.g. when declaring <code>numlist</code> and <code>invlist</code>) and <code>multiplier</code>'s declaration is substantially simpler than <code>compose</code>'s. Both of these boil down to the fact that closures and regular functions are actually different types in Rust so we have to be generic over them but here we are only dealing with 64-bit floats.


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>scala> val x = 2.0
<syntaxhighlight lang="scala">scala> val x = 2.0
x: Double = 2.0
x: Double = 2.0


Line 1,421: Line 1,674:
0.5
0.5
0.5
0.5
0.5</lang>
0.5</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
This implementation closely follows the Scheme implementation of the [[First-class functions]] problem.
This implementation closely follows the Scheme implementation of the [[First-class functions]] problem.
<lang scheme>(define x 2.0)
<syntaxhighlight lang="scheme">(define x 2.0)
(define xi 0.5)
(define xi 0.5)
(define y 4.0)
(define y 4.0)
Line 1,443: Line 1,696:
(newline))
(newline))
n1 n2))
n1 n2))
(go number inverse)</lang>
(go number inverse)</syntaxhighlight>
Output:
Output:
0.5
0.5
Line 1,450: Line 1,703:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func multiplier(n1, n2) {
<syntaxhighlight lang="ruby">func multiplier(n1, n2) {
func (n3) {
func (n3) {
n1 * n2 * n3
n1 * n2 * n3
Line 1,468: Line 1,721:
for f,g (numbers ~Z inverses) {
for f,g (numbers ~Z inverses) {
say multiplier(f, g)(0.5)
say multiplier(f, g)(0.5)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,478: Line 1,731:
=={{header|Slate}}==
=={{header|Slate}}==
{{incorrect|Slate|Compare and contrast the resultant program with the corresponding entry in First-class functions.}}
{{incorrect|Slate|Compare and contrast the resultant program with the corresponding entry in First-class functions.}}
<lang slate>define: #multiplier -> [| :n1 :n2 | [| :m | n1 * n2 * m]].
<syntaxhighlight lang="slate">define: #multiplier -> [| :n1 :n2 | [| :m | n1 * n2 * m]].
define: #x -> 2.
define: #x -> 2.
define: #y -> 4.
define: #y -> 4.
Line 1,484: Line 1,737:
define: #numlisti -> (numlist collect: [| :x | 1.0 / x]).
define: #numlisti -> (numlist collect: [| :x | 1.0 / x]).


numlist with: numlisti collect: [| :n1 :n2 | (multiplier applyTo: {n1. n2}) applyWith: 0.5].</lang>
numlist with: numlisti collect: [| :n1 :n2 | (multiplier applyTo: {n1. n2}) applyWith: 0.5].</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
{{works with|Tcl|8.5}}
<lang tcl>package require Tcl 8.5
<syntaxhighlight lang="tcl">package require Tcl 8.5
proc multiplier {a b} {
proc multiplier {a b} {
list apply {{ab m} {expr {$ab*$m}}} [expr {$a*$b}]
list apply {{ab m} {expr {$ab*$m}}} [expr {$a*$b}]
}</lang>
}</syntaxhighlight>
Note that, as with Tcl's solution for [[First-class functions#Tcl|First-class functions]], the resulting term must be expanded on application. For example, study this interactive session:
Note that, as with Tcl's solution for [[First-class functions#Tcl|First-class functions]], the resulting term must be expanded on application. For example, study this interactive session:
<lang tcl>% set mult23 [multiplier 2 3]
<syntaxhighlight lang="tcl">% set mult23 [multiplier 2 3]
apply {{ab m} {expr {$ab*$m}}} 6
apply {{ab m} {expr {$ab*$m}}} 6
% {*}$mult23 5
% {*}$mult23 5
30</lang>
30</syntaxhighlight>
Formally, for the task:
Formally, for the task:
<lang tcl>set x 2.0
<syntaxhighlight lang="tcl">set x 2.0
set xi 0.5
set xi 0.5
set y 4.0
set y 4.0
Line 1,508: Line 1,761:
foreach a $numlist b $numlisti {
foreach a $numlist b $numlisti {
puts [format "%g * %g * 0.5 = %g" $a $b [{*}[multiplier $a $b] 0.5]]
puts [format "%g * %g * 0.5 = %g" $a $b [{*}[multiplier $a $b] 0.5]]
}</lang>
}</syntaxhighlight>
Which produces this output:
Which produces this output:
<pre>2 * 0.5 * 0.5 = 0.5
<pre>2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.166667 * 0.5 = 0.5</pre>
6 * 0.166667 * 0.5 = 0.5</pre>

=={{header|TXR}}==

This solution seeks a non-strawman interpretation of the exercise: to treat functions and literal numeric terms under the same operations. We develop a three-argument function called <code>binop</code> whose argument is an ordinary function which works on numbers, and two arithmetic arguments which are any combination of functions or numbers. The functions may have any arity from 0 to 2. The <code>binop</code> functions handles all the cases.

The basic rules are:

* When all required arguments are given to a function, it is expected that a number will be produced.

* Zero-argument functions are called to force a number out of them.

* When operands are numbers or zero-argument functions, a numeric result is calculated.

* Otherwise the operation is a functional combinator, returning a function.

<syntaxhighlight lang="txrlisp">(defun binop (numop x y)
(typecase x
(number (typecase y
(number [numop x y])
(fun (caseql (fun-fixparam-count y)
(0 [numop x [y]])
(1 (ret [numop x [y @1]]))
(2 (ret [numop x [y @1 @2]]))
(t (error "~s: right argument has too many params"
%fun% y))))
(t (error "~s: right argument must be function or number"
%fun% y))))
(fun (typecase y
(number (caseql (fun-fixparam-count x)
(0 [numop [x] y])
(1 (ret [numop [x @1] y]))
(2 (ret [numop [x @1 @2] y]))
(t (error "~s: left argument has too many params"
%fun% x))))
(fun (macrolet ((pc (x-param-count y-param-count)
^(+ (* 3 ,x-param-count) ,y-param-count)))
(caseql* (pc (fun-fixparam-count x) (fun-fixparam-count y))
(((pc 0 0)) [numop [x] [y]])
(((pc 0 1)) (ret [numop [x] [y @1]]))
(((pc 0 2)) (ret [numop [x] [y @1 @2]]))
(((pc 1 0)) (ret [numop [x @1] [y]]))
(((pc 1 1)) (ret [numop [x @1] [y @1]]))
(((pc 1 2)) (ret [numop [x @1] [y @1 @2]]))
(((pc 2 0)) (ret [numop [x @1 @2] [y]]))
(((pc 2 1)) (ret [numop [x @1 @2] [y @1]]))
(((pc 2 2)) (ret [numop [x @1 @2] [y @1 @2]]))
(t (error "~s: one or both arguments ~s and ~s\ \
have excess arity" %fun% x y)))))))
(t (error "~s: left argument must be function or number"
%fun% y))))

(defun f+ (x y) [binop + x y])
(defun f- (x y) [binop - x y])
(defun f* (x y) [binop * x y])
(defun f/ (x y) [binop / x y])</syntaxhighlight>

With this, the following sort of thing is possible:

<pre>1> [f* 6 4] ;; ordinary arithmetic.
24
2> [f* f+ f+] ;; product of additions
#<interpreted fun: lambda (#:arg-1-0062 #:arg-2-0063 . #:arg-rest-0061)>
3> [*2 10 20] ;; i.e. (* (+ 10 20) (+ 10 20)) -> (* 30 30)
900
4> [f* 2 f+] ;; doubled addition
#<interpreted fun: lambda (#:arg-1-0017 #:arg-2-0018 . #:arg-rest-0016)>
5> [*4 11 19] ;; i.e. (* 2 (+ 11 19))
60
6> [f* (op f+ 2 @1) (op f+ 3 @1)]
#<interpreted fun: lambda (#:arg-1-0047 . #:arg-rest-0046)>
7> [*6 10 10] ;; i.e. (* (+ 2 10) (+ 3 10)) -> (* 12 13)
156
</pre>

So with these definitions, we can solve the task like this, which demonstrates that numbers and functions are handled by the same operations:

<syntaxhighlight lang="txrlisp">(let* ((x 2.0)
(xi 0.5)
(y 4.0)
(yi 0.25)
(z (lambda () (f+ x y))) ;; z is obviously function
(zi (f/ 1 z))) ;; also a function
(flet ((multiplier (a b) (op f* @1 (f* a b))))
(each ((n (list x y z))
(v (list xi yi zi)))
(prinl [[multiplier n v] 42.0]))))</syntaxhighlight>

{{out}}

<pre>42.0
42.0
42.0</pre>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 1,519: Line 1,864:
composition operator (+), and is named in compliance
composition operator (+), and is named in compliance
with the task specification.
with the task specification.
<lang Ursala>#import std
<syntaxhighlight lang="ursala">#import std
#import flo
#import flo


Line 1,529: Line 1,874:
#cast %eL
#cast %eL


main = (gang multiplier*p\numbers inverses) 0.5</lang>
main = (gang multiplier*p\numbers inverses) 0.5</syntaxhighlight>
The multiplier could have been written in pattern
The multiplier could have been written in pattern
matching form like this.
matching form like this.
<lang Ursala>multiplier("a","b") "c" = times(times("a","b"),"c")</lang>
<syntaxhighlight lang="ursala">multiplier("a","b") "c" = times(times("a","b"),"c")</syntaxhighlight>
The main program might also have been written with an
The main program might also have been written with an
anonymous function like this.
anonymous function like this.
<lang Ursala>main = (gang (//times+ times)*p\numbers inverses) 0.5</lang>
<syntaxhighlight lang="ursala">main = (gang (//times+ times)*p\numbers inverses) 0.5</syntaxhighlight>
output:
output:
<pre>
<pre>
<5.000000e-01,5.000000e-01,5.000000e-01>
<5.000000e-01,5.000000e-01,5.000000e-01>
</pre>

=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt

var multiplier = Fn.new { |n1, n2| Fn.new { |m| n1 * n2 * m } }

var orderedCollection = Fn.new {
var x = 2.0
var xi = 0.5
var y = 4.0
var yi = 0.25
var z = x + y
var zi = 1.0 / ( x + y )
return [[x, y, z], [xi, yi, zi]]
}

var oc = orderedCollection.call()
for (i in 0..2) {
var x = oc[0][i]
var y = oc[1][i]
var m = 0.5 // rather than 1 to compare with first-class functions task
Fmt.print("$0.1g * $g * $0.1g = $0.1g", x, y, m, multiplier.call(x, y).call(m))
}</syntaxhighlight>

{{out}}
Agreeing with results of first-class functions task:
<pre>
2.0 * 0.5 * 0.5 = 0.5
4.0 * 0.25 * 0.5 = 0.5
6.0 * 0.166667 * 0.5 = 0.5
</pre>
</pre>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>var x=2.0, y=4.0, z=(x+y), c=T(x,y,z).apply(fcn(n){ T(n,1.0/n) });
<syntaxhighlight lang="zkl">var x=2.0, y=4.0, z=(x+y), c=T(x,y,z).apply(fcn(n){ T(n,1.0/n) });
//-->L(L(2,0.5),L(4,0.25),L(6,0.166667))</lang>
//-->L(L(2,0.5),L(4,0.25),L(6,0.166667))</syntaxhighlight>
<lang zkl>fcn multiplier(n1,n2){ return('*.fp(n1,n2)) }</lang>
<syntaxhighlight lang="zkl">fcn multiplier(n1,n2){ return('*.fp(n1,n2)) }</syntaxhighlight>
This is actually partial evaluation, multiplier returns n1*n2*X where X isn't known yet. So multiplier(2,3)(4) --> (2*3*)4 --> 24. Even better, multiplier(2,3)(4,5) --> 120, multiplier(2,3)(4,5,6) --> 720, multiplier(2,3)() --> 6.
This is actually partial evaluation, multiplier returns n1*n2*X where X isn't known yet. So multiplier(2,3)(4) --> (2*3*)4 --> 24. Even better, multiplier(2,3)(4,5) --> 120, multiplier(2,3)(4,5,6) --> 720, multiplier(2,3)() --> 6.


Alternatively,
Alternatively,
<lang zkl>fcn multiplier(n1,n2){ fcn(n,X){ n*X }.fp(n1*n2) }</lang>
<syntaxhighlight lang="zkl">fcn multiplier(n1,n2){ fcn(n,X){ n*X }.fp(n1*n2) }</syntaxhighlight>
<lang zkl>var ms=c.apply(fcn([(n1,n2)]){ multiplier(n1,n2) });
<syntaxhighlight lang="zkl">var ms=c.apply(fcn([(n1,n2)]){ multiplier(n1,n2) });
//-->L(Deferred,Deferred,Deferred) // lazy eval of n*(1/n)*X
//-->L(Deferred,Deferred,Deferred) // lazy eval of n*(1/n)*X
ms.run(True,1.0) //-->L(1,1,1)
ms.run(True,1.0) //-->L(1,1,1)
ms.run(True,5.0) //-->L(5,5,5)
ms.run(True,5.0) //-->L(5,5,5)
ms.run(True,0.5) //-->L(0.5,0.5,0.5)</lang>
ms.run(True,0.5) //-->L(0.5,0.5,0.5)</syntaxhighlight>
List.run(True,X), for each item in the list, does i(X) and collects the results into another list. Sort of an inverted map or fold.
List.run(True,X), for each item in the list, does i(X) and collects the results into another list. Sort of an inverted map or fold.



Latest revision as of 12:05, 4 March 2024

Task
First-class functions/Use numbers analogously
You are encouraged to solve this task according to the task description, using any language you may know.

In First-class functions, a language is showing how its manipulation of functions is similar to its manipulation of other types.

This tasks aim is to compare and contrast a language's implementation of first class functions, with its normal handling of numbers.


Write a program to create an ordered collection of a mixture of literally typed and expressions producing a real number, together with another ordered collection of their multiplicative inverses. Try and use the following pseudo-code to generate the numbers for the ordered collections:

  x  = 2.0
  xi = 0.5
  y  = 4.0
  yi = 0.25
  z  = x + y
  zi = 1.0 / ( x + y )

Create a function multiplier, that given two numbers as arguments returns a function that when called with one argument, returns the result of multiplying the two arguments to the call to multiplier that created it and the argument in the call:

 new_function = multiplier(n1,n2)
 # where new_function(m) returns the result of n1 * n2 * m

Applying the multiplier of a number and its inverse from the two ordered collections of numbers in pairs, show that the result in each case is one.
Compare and contrast the resultant program with the corresponding entry in First-class functions. They should be close.

To paraphrase the task description: Do what was done before, but with numbers rather than functions

11l

Translation of: Python
V (x, xi, y, yi) = (2.0, 0.5, 4.0, 0.25)
V z = x + y
V zi = 1.0 / (x + y)
V multiplier = (n1, n2) -> (m -> @=n1 * @=n2 * m)
V numlist = [x, y, z]
V numlisti = [xi, yi, zi]
print(zip(numlist, numlisti).map((n, inversen) -> multiplier(inversen, n)(.5)))
Output:
[0.5, 0.5, 0.5]

Ada

with Ada.Text_IO;
procedure Firstclass is
   generic
      n1, n2 : Float;
   function Multiplier (m : Float) return Float;
   function Multiplier (m : Float) return Float is
   begin
      return n1 * n2 * m;
   end Multiplier;
   
   num, inv : array (1 .. 3) of Float;
begin
   num := (2.0, 4.0, 6.0);
   inv := (1.0/2.0, 1.0/4.0, 1.0/6.0);
   for i in num'Range loop
      declare
         function new_function is new Multiplier (num (i), inv (i));
      begin
         Ada.Text_IO.Put_Line (Float'Image (new_function (0.5)));
      end;
   end loop;
end Firstclass;
Output:
5.00000E-01
5.00000E-01
5.00000E-01

ALGOL 68

Translation of: python
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d

Note: Standard ALGOL 68's scoping rules forbids exporting a proc[edure] (or format) out of it's scope (closure). Hence this specimen will run on ELLA ALGOL 68, but is non-standard. For a discussion of first-class functions in ALGOL 68 consult "The Making of Algol 68" - C.H.A. Koster (1993).

REAL
  x  := 2,
  xi := 0.5,
  y  := 4,
  yi := 0.25,
  z  := x + y,
  zi := 1 / ( x + y );

MODE F = PROC(REAL)REAL; 

PROC multiplier = (REAL n1, n2)F: ((REAL m)REAL: n1 * n2 * m);

# Numbers as members of collections #
[]REAL num list = (x, y, z),
       inv num list = (xi, yi, zi);

# Apply numbers from list #
FOR key TO UPB num list DO
  REAL n = num list[key],
       inv n = inv num list[key];
  print ((multiplier(inv n, n)(.5), new line))
OD

Output:

+.500000000000000e +0
+.500000000000000e +0
+.500000000000000e +0

Comparing and contrasting with the First Class Functions example:

As noted above, in order to do what is required by this task and the First Class Functions task, extensions to Algol 68 must be used.

The First Class Functions example uses C. H. Lindsey's partial parameterization extension to Algol 68 which implemented in Algol 68G but not in algol68toc. This example uses an alternative (technically, invalid Algol 68 as the author notes) accepted by algol68toc but not Algol 68G.

Arturo

x: 2.0
xi: 0.5
y: 4.0
yi: 0.25
z: x + y
zi: 1 / z

multiplier: function [m n][
    function [a] with [m n][
        a*m*n
    ]
]

couple @[x y z] @[xi yi zi]
    | map 'p -> multiplier p\0 p\1
    | map => [call & -> 0.5]
    | print
Output:
0.5 0.5 0.5

Axiom

(x,xi,y,yi) := (2.0,0.5,4.0,0.25)
(z,zi) := (x+y,1/(x+y))
(numbers,invers) := ([x,y,z],[xi,yi,zi])
multiplier(a:Float,b:Float):(Float->Float) == (m +-> a*b*m)
[multiplier(number,inver) 0.5 for number in numbers for inver in invers]

Output:

  [0.5,0.5,0.5]
                          Type: List(Float)

We can also curry functions, possibly with function composition, with the same output as before:

mult(n:Float):(Float->Float) == curryLeft(*$Float,n)$MAPPKG3(Float,Float,Float)
[mult(number*inver) 0.5 for number in numbers for inver in invers]
[(mult(number)*mult(inver)) 0.5 for number in numbers for inver in invers]

Using the Spad code in First-class functions#Axiom, this can be done more economically using:

(map(mult,numbers)*map(mult,invers)) 0.5

For comparison, First-class functions#Axiom gave:

fns := [sin$Float, cos$Float, (x:Float):Float +-> x^3]
inv := [asin$Float, acos$Float, (x:Float):Float +-> x^(1/3)]
[(f*g) 0.5 for f in fns for g in inv]

- which has the same output.

BBC BASIC

      REM Create some numeric variables:
      x = 2 : xi = 1/2
      y = 4 : yi = 0.25
      z = x + y : zi = 1 / (x + y)
      
      REM Create the collections (here structures are used):
      DIM c{x, y, z}
      DIM ci{x, y, z}
      c.x = x : c.y = y : c.z = z
      ci.x = xi : ci.y = yi : ci.z = zi
      
      REM Create some multiplier functions:
      multx = FNmultiplier(c.x, ci.x)
      multy = FNmultiplier(c.y, ci.y)
      multz = FNmultiplier(c.z, ci.z)
      
      REM Test applying the compositions:
      x = 1.234567 : PRINT x " ", FN(multx)(x)
      x = 2.345678 : PRINT x " ", FN(multy)(x)
      x = 3.456789 : PRINT x " ", FN(multz)(x)
      END
      
      DEF FNmultiplier(n1,n2)
      LOCAL f$, p%
      f$ = "(m)=" + STR$n1 + "*" + STR$n2 + "*m"
      DIM p% LEN(f$) + 4
      $(p%+4) = f$ : !p% = p%+4
      = p%

Output:

  1.234567            1.234567
  2.345678            2.345678
  3.456789          3.45678901

Compare with the implementation of First-class functions:

      REM Create some functions and their inverses:
      DEF FNsin(a) = SIN(a)
      DEF FNasn(a) = ASN(a)
      DEF FNcos(a) = COS(a)
      DEF FNacs(a) = ACS(a)
      DEF FNcube(a) = a^3
      DEF FNroot(a) = a^(1/3)
      
      dummy = FNsin(1)
      
      REM Create the collections (here structures are used):
      DIM cA{Sin%, Cos%, Cube%}
      DIM cB{Asn%, Acs%, Root%}
      cA.Sin% = ^FNsin() : cA.Cos% = ^FNcos() : cA.Cube% = ^FNcube()
      cB.Asn% = ^FNasn() : cB.Acs% = ^FNacs() : cB.Root% = ^FNroot()
      
      REM Create some function compositions:
      AsnSin% = FNcompose(cB.Asn%, cA.Sin%)
      AcsCos% = FNcompose(cB.Acs%, cA.Cos%)
      RootCube% = FNcompose(cB.Root%, cA.Cube%)
      
      REM Test applying the compositions:
      x = 1.234567 : PRINT x, FN(AsnSin%)(x)
      x = 2.345678 : PRINT x, FN(AcsCos%)(x)
      x = 3.456789 : PRINT x, FN(RootCube%)(x)
      END
      
      DEF FNcompose(f%,g%)
      LOCAL f$, p%
      f$ = "(x)=" + CHR$&A4 + "(&" + STR$~f% + ")(" + \
      \             CHR$&A4 + "(&" + STR$~g% + ")(x))"
      DIM p% LEN(f$) + 4
      $(p%+4) = f$ : !p% = p%+4
      = p%

C#

Works with: C# version 4.0

The structure here is exactly the same as the C# entry in First-class functions. The "var" keyword allows us to use the same initialization code for an array of doubles as an array of functions. Note that variable names have been changed to correspond with the new functionality.

using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        double x, xi, y, yi, z, zi;
        x = 2.0;
        xi = 0.5;
        y = 4.0;
        yi = 0.25;
        z = x + y;
        zi = 1.0 / (x + y);

        var numlist = new[] { x, y, z };
        var numlisti = new[] { xi, yi, zi };
        var multiplied = numlist.Zip(numlisti, (n1, n2) =>
                       {
                           Func<double, double> multiplier = m => n1 * n2 * m;
                           return multiplier;
                       });

        foreach (var multiplier in multiplied)
            Console.WriteLine(multiplier(0.5));
    }
}

C++

#include <array>
#include <iostream>

int main()
{
  double x  = 2.0;
  double xi = 0.5;
  double y  = 4.0;
  double yi = 0.25;
  double z  = x + y;
  double zi = 1.0 / ( x + y );

  const std::array values{x, y, z};
  const std::array inverses{xi, yi, zi};

  auto multiplier = [](double a, double b)
  {
    return [=](double m){return a * b * m;};
  };

  for(size_t i = 0; i < values.size(); ++i)
  {
    auto new_function = multiplier(values[i], inverses[i]);
    double value = new_function(i + 1.0);
    std::cout << value << "\n"; 
  }
}
Output:
1
2
3

Clojure

(def x  2.0)
(def xi 0.5)
(def y  4.0)
(def yi 0.25)
(def z  (+ x y))
(def zi (/ 1.0 (+ x y)))

(def numbers [x y z])
(def invers  [xi yi zi])

(defn multiplier [a b]
      (fn [m] (* a b m)))

> (for [[n i] (zipmap numbers invers)]
       ((multiplier n i) 0.5))
(0.5 0.5 0.5)

For comparison:

(use 'clojure.contrib.math)
(let [fns [#(Math/sin %) #(Math/cos %) (fn [x] (* x x x))]
      inv [#(Math/asin %) #(Math/acos %) #(expt % 1/3)]]
  (map #(% 0.5) (map #(comp %1 %2) fns inv)))

Output:

(0.5 0.4999999999999999 0.5000000000000001)

Common Lisp

(defun multiplier (f g)
  #'(lambda (x) (* f g x)))

(let* ((x 2.0)
       (xi 0.5)
       (y 4.0)
       (yi 0.25)
       (z (+ x y))
       (zi (/ 1.0 (+ x y)))
       (numbers (list x y z))
       (inverses (list xi yi zi)))
  (loop with value = 0.5
        for number in numbers
        for inverse in inverses
        for multiplier = (multiplier number inverse)
        do (format t "~&(~A * ~A)(~A) = ~A~%"
                   number
                   inverse
                   value
                   (funcall multiplier value))))

Output:

(2.0 * 0.5)(0.5) = 0.5
(4.0 * 0.25)(0.5) = 0.5
(6.0 * 0.16666667)(0.5) = 0.5

The code from First-class functions, for comparison:

(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
(defun cube (x) (expt x 3))
(defun cube-root (x) (expt x (/ 3)))

(loop with value = 0.5
      for function in (list #'sin  #'cos  #'cube     )
      for inverse  in (list #'asin #'acos #'cube-root)
      for composed = (compose inverse function)
      do (format t "~&(~A ∘ ~A)(~A) = ~A~%"
                 inverse
                 function
                 value 
                 (funcall composed value)))

Output:

(#<FUNCTION ASIN> ∘ #<FUNCTION SIN>)(0.5) = 0.5
(#<FUNCTION ACOS> ∘ #<FUNCTION COS>)(0.5) = 0.5
(#<FUNCTION CUBE-ROOT> ∘ #<FUNCTION CUBE>)(0.5) = 0.5

D

import std.stdio;

auto multiplier(double a, double b)
{
    return (double c) => a * b * c;
}

void main()
{
    double x = 2.0;
    double xi = 0.5;
    double y = 4.0;
    double yi = 0.25;
    double z = x + y;
    double zi = 1.0 / (z);

    double[3] f = [x, y, z]; 
    double[3] r = [xi, yi, zi];

    foreach (i; 0..3)
    {   
        auto mult = multiplier(f[i], r[i]);
        writefln("%f * %f * %f == %f", f[i], r[i], 1.0, mult(1));
    }   
}

Output:

2.000000 * 0.500000 * 1.000000 == 1.000000
4.000000 * 0.250000 * 1.000000 == 1.000000
6.000000 * 0.166667 * 1.000000 == 1.000000

E

This is written to have identical structure to First-class functions#E, though the variable names are different.

def x := 2.0
def xi := 0.5
def y := 4.0
def yi := 0.25
def z := x + y
def zi := 1.0 / (x + y)
def forward := [x,  y,  z ]
def reverse := [xi, yi, zi]

def multiplier(a, b) {
    return fn x { a * b * x }
}

def s := 0.5
for i => a in forward {
    def b := reverse[i]
    println(`s = $s, a = $a, b = $b, multiplier($a, $b)($s) = ${multiplier(a, b)(s)}`)
}

Output:

s = 0.5, a = 2.0, b = 0.5, multiplier(2.0, 0.5)(0.5) = 0.5
s = 0.5, a = 4.0, b = 0.25, multiplier(4.0, 0.25)(0.5) = 0.5
s = 0.5, a = 6.0, b = 0.16666666666666666, multiplier(6.0, 0.16666666666666666)(0.5) = 0.5

Note: def g := reverse[i] is needed here because E as yet has no defined protocol for iterating over collections in parallel. Page for this issue.

Elena

Translation of: C#

ELENA 6.x :

import system'routines;
import extensions;
 
public program()
{
    real x := 2.0r;
    real xi := 0.5r;
    real y := 4.0r;
    real yi := 0.25r;
    real z := x + y;
    real zi := 1.0r / (x + y);
 
    var numlist := new real[]{  x, y, z };
    var numlisti := new real[]{ xi, yi, zi };
 
    var multiplied := numlist.zipBy(numlisti, (n1,n2 => (m => n1 * n2 * m) )).toArray();
 
    multiplied.forEach::(multiplier){ console.printLine(multiplier(0.5r)) }
}
Output:
0.5
0.5
0.5

Erlang

-module( first_class_functions_use_numbers ).

-export( [task/0] ).

task() ->
	X = 2.0, Xi = 0.5, Y = 4.0, Yi = 0.25, Z = X + Y, Zi = 1.0 / (X + Y),
	As = [X, Y, Z],
	Bs = [Xi, Yi, Zi],
	[io:fwrite( "Value: 2.5 Result: ~p~n", [(multiplier(A, B))(2.5)]) || {A, B} <- lists:zip(As, Bs)].



multiplier( N1, N2 ) -> fun(M) -> N1 * N2 * M end.
Output:
20> first_class_functions_use_numbers:task().
Value: 2.5 Result: 2.5
Value: 2.5 Result: 2.5
Value: 2.5 Result: 2.5

F#

let x = 2.0
let xi = 0.5
let y = 4.0
let yi = 0.25
let z = x + y
let zi = 1.0 / ( x + y )
let multiplier (n1,n2) = fun (m:float) -> n1 * n2 * m 

[x; y; z] 
|> List.zip [xi; yi; zi] 
|> List.map multiplier
|> List.map ((|>) 0.5)
|> printfn "%A"
Output:
[0.5; 0.5; 0.5]

Factor

Compared to http://rosettacode.org/wiki/First-class_functions, the call to "compose" is replaced with the call to "mutliplier"

USING: arrays kernel literals math prettyprint sequences ;
IN: q

CONSTANT: x  2.0
CONSTANT: xi 0.5
CONSTANT: y  4.0
CONSTANT: yi .25
CONSTANT: z $[ $ x $ y + ]
CONSTANT: zi $[ 1 $ x $ y + / ]

CONSTANT: A ${ x y z }
CONSTANT: B ${ xi yi zi } 

: multiplier ( n1 n2 -- q ) [ * * ] 2curry ;
: create-all ( seq1 seq2 -- seq ) [ multiplier ] 2map ;
: example  ( -- )
  0.5 A B create-all
  [ call( x -- y ) ] with map . ;
Output:
{ 0.5 0.5 0.5 }

Fantom

class Main
{
  static |Float -> Float| combine (Float n1, Float n2)
  {
    return |Float m -> Float| { n1 * n2 * m }
  }

  public static Void main ()
  {
    Float x := 2f
    Float xi := 0.5f
    Float y := 4f
    Float yi := 0.25f
    Float z := x + y
    Float zi := 1 / (x + y)
    echo (combine(x, xi)(0.5f))
    echo (combine(y, yi)(0.5f))
    echo (combine(z, zi)(0.5f))
  }
}

The combine function is very similar to the compose function in 'First-class functions'. In both cases a new function is returned:

  static |Obj -> Obj| compose (|Obj -> Obj| fn1, |Obj -> Obj| fn2)
  {
    return |Obj x -> Obj| { fn2 (fn1 (x)) }
  }

FreeBASIC

FreeBASIC does not support first-class functions or function closures, which means that you cannot create a function that returns another function or that has a function defined inside it.

However, similar behavior can be achieved with subroutines and global variables.

Dim As Double x = 2.0,   xi = 0.5
Dim As Double y = 4.0,   yi = 0.25
Dim As Double z = x + y, zi = 1.0 / (x + y)
Dim As Double values(2)   = {x, y, z}
Dim As Double inverses(2) = {xi, yi, zi}

Dim Shared As Double m = 0.5

Function multiplier(a As Double, d As Double) As Double
    Return a * d * m
End Function

For i As Byte = 0 To Ubound(values)
    Dim As Double new_function = multiplier(values(i), inverses(i))
    Print values(i); " *"; inverses(i); " *"; m; " ="; new_function
Next i

Sleep
Output:
 2 * 0.5 * 0.5 = 0.5
 4 * 0.25 * 0.5 = 0.5
 6 * 0.1666666666666667 * 0.5 = 0.5

Go

"Number means value"

Task interpretation 1: "Number" means a numeric value, not any sort of reference. This is the most natural interpretation in Go.

At point A, the six variables have been assigned values, 64 bit floating point numbers, and not references to anything that is evaluated later. Again, at point B, these values have been copied into the array elements. (The arrays being "ordered collections.") The original six variables could be changed at this point and the array values would stay the same.

Multiplier multiplies pairs of values and binds the result to the returned closure. This might be considered a difference from the First-class functions task. In that task, the functions were composed into a new function but not evaluated at the time of composition. They could have been, but that is not the usual meaning of function composition.

This task however, works with numbers, which are not reference types. Specifically, the closure here could could have closed on n1 an n2 individually and delayed multiplication until closure evaluation, but that might seem inconsistent with the task interpretation of working with numbers as values. Also, one would expect that a function named "multiplier" does actually multiply.

Multiplier of this task and compose of the First-class function task are similar in that they both return first class function objects, which are closed on free variables. The free variables in compose held the two (unevaluated) functions being composed. The free variable in multiplier holds the result of multiplication.

At point C, numbers and their inverses have been multiplied and bound to first class functions. The ordered collection arrays could be modified at this point and the function objects would be unaffected.

package main

import "fmt"

func main() {
    x := 2.
    xi := .5
    y := 4.
    yi := .25
    z := x + y
    zi := 1 / (x + y)
    // point A

    numbers := []float64{x, y, z}
    inverses := []float64{xi, yi, zi}
    // point B

    mfs := make([]func(float64) float64, len(numbers))
    for i := range mfs {
        mfs[i] = multiplier(numbers[i], inverses[i])
    }
    // point C

    for _, mf := range mfs {
        fmt.Println(mf(1))
    }
}

func multiplier(n1, n2 float64) func(float64) float64 {
    // compute product of n's, store in a new variable
    n1n2 := n1 * n2
    // close on variable containing product
    return func(m float64) float64 {
        return n1n2 * m
    }
}

Output:

1
1
1

"Number means reference"

Task interpretation 2: "Number" means something abstract, evaluation of which is delayed as long as possible. This interpretation is suggested by the task wording that this program and and the corresponding First-class functions program "should be close" and "do what was done before...."

To implement this behavior, reference types are used for "numbers" and a polymorphic array is used for the ordered collection, allowing both static and computed objects to stored.

At point A, the variables z and zi are assigned function literals, first class function objects closed on x and y. Changing the values of x or y at this point will cause z and zi to return different results. The x and y in these function literals reference the same storage as the x and y assigned values 2 and 4. This is more like the the First-class functions task in that we now have functions which we can compose.

At point B, we have filled the polymorphic arrays with all reference types. References to numeric typed variables x, xi, y, and yi were created with the & operator. z and zi can already be considered reference types in that they reference x and y. Changes to any of x, xi, y, or yi at this point would still affect later results.

Multiplier, in this interpretation of the task, simply composes multiplication of three reference objects, which may be variables or functions. It does not actually multiply and does not even evaluate the three objects. This is very much like the compose function of the First-class functions task.

Pursuant to the task description, this version of multiplier "returns the result of n1 * n2 * m" in the sense that it sets up evaluation of n1, n2, and m to be done at the same time, even if that time is not quite yet.

At point C, changes to x, xi, y, and yi will still propagate through and affect the results returned by the mfs objects. This can be seen as like the First-class functions task in that nothing (nothing numberic anyway) is evaluated until the final composed objects are evaluated.

This can also be done with an interface type rather than the "empty interface" (interface{}) for better type safety and to avoid the eval function and type switch.

package main

import "fmt"

func main() {
    x := 2.
    xi := .5
    y := 4.
    yi := .25
    z := func() float64 { return x + y }
    zi := func() float64 { return 1 / (x + y) }
    // point A

    numbers := []interface{}{&x, &y, z}
    inverses := []interface{}{&xi, &yi, zi}
    // point B

    mfs := make([]func(n interface{}) float64, len(numbers))
    for i := range mfs {
        mfs[i] = multiplier(numbers[i], inverses[i])
    }
    // pointC

    for _, mf := range mfs {
        fmt.Println(mf(1.))
    }
}

func multiplier(n1, n2 interface{}) func(interface{}) float64 {
    return func(m interface{}) float64 {
        // close on interface objects n1, n2, and m
        return eval(n1) * eval(n2) * eval(m)
    }
}

// utility function for evaluating multiplier interface objects
func eval(n interface{}) float64 {
    switch n.(type) {
    case float64:
        return n.(float64)
    case *float64:
        return *n.(*float64)
    case func() float64:
        return n.(func() float64)()
    }
    panic("unsupported multiplier type")
    return 0 // never reached
}

Groovy

def multiplier = { n1, n2 -> { m -> n1 * n2 * m } }

def ε = 0.00000001  // tolerance(epsilon): acceptable level of "wrongness" to account for rounding error
[(2.0):0.5, (4.0):0.25, (6.0):(1/6.0)].each { num, inv ->
    def new_function = multiplier(num, inv)
    (1.0..5.0).each { trial ->
        assert (new_function(trial) - trial).abs() < ε
        printf('%5.3f * %5.3f * %5.3f == %5.3f\n', num, inv, trial, trial)
    }
    println()
}
Output:
2.000 * 0.500 * 1.000 == 1.000
2.000 * 0.500 * 2.000 == 2.000
2.000 * 0.500 * 3.000 == 3.000
2.000 * 0.500 * 4.000 == 4.000
2.000 * 0.500 * 5.000 == 5.000

4.000 * 0.250 * 1.000 == 1.000
4.000 * 0.250 * 2.000 == 2.000
4.000 * 0.250 * 3.000 == 3.000
4.000 * 0.250 * 4.000 == 4.000
4.000 * 0.250 * 5.000 == 5.000

6.000 * 0.167 * 1.000 == 1.000
6.000 * 0.167 * 2.000 == 2.000
6.000 * 0.167 * 3.000 == 3.000
6.000 * 0.167 * 4.000 == 4.000
6.000 * 0.167 * 5.000 == 5.000

Haskell

module Main
  where

import Text.Printf

-- Pseudo code happens to be valid Haskell
x  = 2.0
xi = 0.5
y  = 4.0
yi = 0.25
z  = x + y
zi = 1.0 / ( x + y )

-- Multiplier function
multiplier :: Double -> Double -> Double -> Double
multiplier a b = \m -> a * b * m

main :: IO ()
main = do
  let
    numbers = [x, y, z]
    inverses = [xi, yi, zi]
    pairs = zip numbers inverses
    print_pair (number, inverse) =
      let new_function = multiplier number inverse
      in printf "%f * %f * 0.5 = %f\n" number inverse (new_function 0.5)
  mapM_ print_pair pairs

This is very close to the first-class functions example, but given as a full Haskell program rather than an interactive session.

Icon and Unicon

The following is a Unicon solution. It can be recast in Icon, but only at the cost of losing the "function-call" syntax on the created "procedure". The solution uses the same basic foundation that is buried in the "compose" procedure in the First-class functions task solution. The solution here is simpler and more direct since it handles a specific function definition.

import Utils

procedure main(A)
    mult := multiplier(get(A),get(A))   # first 2 args define function
    every write(mult(!A))     # remaining are passed to new function
end

procedure multiplier(n1,n2)
    return makeProc { repeat inVal := n1 * n2 * (inVal@&source)[1] }
end

A sample run:

->mu 2 3 4 5 6 7
24
30
36
42
->

J

Explicit version

This seems to satisfy the new problem statement:

   x          =:  2.0
   xi         =:  0.5
   y          =:  4.0
   yi         =:  0.25
   z          =:  x + y
   zi         =:  1.0 % (x + y)  NB. / is spelled % in J

   fwd        =:  x ,y ,z
   rev        =:  xi,yi,zi

   multiplier =:  2 : 'm * n * ]'

An equivalent but perhaps prettier definition of multiplier would be:

multiplier=: {{m*n*]}}

Or, if J's "right bracket is the right identity function" bothers you, you might prefer the slightly more verbose but still equivalent:

multiplier=: {{m*n*{{y}}}}

Example use:

   fwd multiplier rev 0.5
0.5 0.5 0.5

For contrast, here are the final results from First-class functions#J:

   BA unqcol 0.5
0.5 0.5 0.5 0.5

Tacit (unorthodox) version

Although the pseudo-code to generate the numbers can certainly be written (see above Explicit version ) this is not done for this version because it would destroy part of the analogy (J encourages, from the programming perspective, to process all components at once as opposed to one component at a time). In addition, this version is done in terms of boxed lists of numbers instead of plain list of numbers, again, to preserve the analogy.

   multiplier=. train@:((;:'&*') ;~ an@: *)
   
   ]A=. 2  ; 4  ; (2 + 4)   NB. Corresponds to  ]A=. box (1&o.)`(2&o.)`(^&3)
┌─┬─┬─┐
│2│4│6│
└─┴─┴─┘
   ]B=. %&.> A              NB. Corresponds to  ]B =. inverse&.> A
┌───┬────┬────────┐
│0.5│0.25│0.166667│
└───┴────┴────────┘
   ]BA=. B multiplier&.> A  NB. Corresponds to  B compose&.> A
┌───┬───┬───┐
│1&*│1&*│1&*│
└───┴───┴───┘
   BA of &> 0.5             NB. Corresponds to  BA of &> 0.5  (exactly)
0.5 0.5 0.5

Please refer to First-class functions tacit (unorthodox) version for the definitions of the functions train, an and of.

Java

import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

public class FirstClassFunctionsUseNumbersAnalogously {

	public static void main(String[] args) {
		final double x = 2.0,   xi = 0.5,
				 	 y = 4.0,   yi = 0.25,
				 	 z = x + y, zi = 1.0 / ( x + y );

		List<Double> list = List.of( x, y, z );
		List<Double> inverseList = List.of( xi, yi, zi );		
		
		BiFunction<Double, Double, Function<Double, Double>> multiplier = (a, b) -> product -> a * b * product;
		
		for ( int i = 0; i < list.size(); i++ ) {
			Function<Double, Double> multiply = multiplier.apply(list.get(i), inverseList.get(i));
			final double argument = (double) ( i + 1 );
			System.out.println(multiply.apply(argument));
		}		
	}

}
Output:
1.0
2.0
3.0

jq

It may be helpful to compare the following definition of "multiplier" with its Ruby counterpart below. Whereas the Ruby definition must name all its positional parameters, the jq equivalent is defined as a filter that obtains them implicitly from its input.

# Infrastructure:
# zip this and that
def zip(that): [., that] | transpose;

# The task:
def x:  2.0;
def xi: 0.5;
def y:  4.0;
def yi: 0.25;
def z:  x + y;
def zi: 1.0 / (x + y);

def numlist: [x,y,z];

def invlist: [xi, yi, zi];

# Input: [x,y]
def multiplier(j): .[0] * .[1] * j;

numlist | zip(invlist) | map( multiplier(0.5) )
Output:
$ jq -n -c -f First_class_functions_Use_numbers_analogously.jq
[0.5,0.5,0.5]


JavaScript

const x = 2.0;
const xi = 0.5;
const y = 4.0;
const yi = 0.25;
const z = x + y;
const zi = 1.0 / (x + y);
const pairs = [[x, xi], [y, yi], [z, zi]];
const testVal = 0.5;

const multiplier = (a, b) => m => a * b * m;

const test = () => {
  return pairs.map(([a, b]) => {
    const f = multiplier(a, b);
    const result = f(testVal);
    return `${a} * ${b} * ${testVal} = ${result}`;
  });
}

test().join('\n');
Output:
2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.16666666666666666 * 0.5 = 0.5

Julia

Works with: Julia version 0.6

In Julia, like Python and R, functions can be treated as like as other Types.

x, xi  = 2.0, 0.5
y, yi  = 4.0, 0.25
z, zi  = x + y, 1.0 / ( x + y )

multiplier = (n1, n2) -> (m) -> n1 * n2 * m

numlist  = [x ,  y,  z]
numlisti = [xi, yi, zi]

@show collect(multiplier(n, invn)(0.5) for (n, invn) in zip(numlist, numlisti))
Output:
collect(((multiplier(n, invn))(0.5) for (n, invn) = zip(numlist, numlisti))) = [0.5, 0.5, 0.5]

Kotlin

// version 1.1.2

fun multiplier(n1: Double, n2: Double) = { m: Double -> n1 * n2 * m}

fun main(args: Array<String>) {
    val x  = 2.0
    val xi = 0.5
    val y  = 4.0
    val yi = 0.25
    val z  = x + y
    val zi = 1.0 / ( x + y)
    val a  = doubleArrayOf(x, y, z)
    val ai = doubleArrayOf(xi, yi, zi)
    val m  = 0.5
    for (i in 0 until a.size) {
        println("${multiplier(a[i], ai[i])(m)} = multiplier(${a[i]}, ${ai[i]})($m)")
    }
}
Output:
0.5 = multiplier(2.0, 0.5)(0.5)
0.5 = multiplier(4.0, 0.25)(0.5)
0.5 = multiplier(6.0, 0.16666666666666666)(0.5)

Lua

-- This function returns another function that
-- keeps n1 and n2 in scope, ie. a closure.
function multiplier (n1, n2)
    return  function (m)
                return n1 * n2 * m
            end
end

-- Multiple assignment a-go-go
local x, xi, y, yi  = 2.0, 0.5, 4.0, 0.25
local z, zi = x + y, 1.0 / ( x + y )
local nums, invs = {x, y, z}, {xi, yi, zi}

-- 'new_function' stores the closure and then has the 0.5 applied to it
-- (this 0.5 isn't in the task description but everyone else used it)
for k, v in pairs(nums) do
    new_function = multiplier(v, invs[k])
    print(v .. " * " .. invs[k] .. " * 0.5 = " .. new_function(0.5))
end
Output:
2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.16666666666667 * 0.5 = 0.5

M2000 Interpreter

Module CheckIt {
      \\ by default numbers are double
      x  = 2
      xi = 0.5
      y  = 4
      yi = 0.25
      z  = x + y
      zi = 1 / ( x + y )
      Composed=lambda (a, b)-> {
            =lambda a,b (n)->{
                  =a*b*n
            }
       }
      numbers=(x,y,z)
      inverses=(xi,yi,zi)
      Dim Base 0, combo(3)
      combo(0)=Composed(x,xi), Composed(y,yi), Composed(z,zi)
      num=each(numbers)
      inv=each(inverses)
      fun=each(combo())
      While num, inv, fun {
         Print $("0.00"), Array(num);" * ";Array(inv);" * 0.50 = "; combo(fun^)(0.5),$("")
         Print
      }
}
Checkit
\\ for functions  we have this definition
Composed=lambda (f1, f2) -> {
      =lambda f1, f2 (x)->{
            =f1(f2(x))
      }
}
Output:
2.00 * 0.50 * 0.50 = 0.50
4.00 * 0.25 * 0.50 = 0.50
6.00 * 0.17 * 0.50 = 0.50

Mathematica / Wolfram Language

This code demonstrates the example using structure similar to function composition, however the composition function is replace with the multiplier function.

multiplier[n1_,n2_]:=n1 n2 #&
num={2,4,2+4};
numi=1/num;
multiplierfuncs = multiplier @@@ Transpose[{num, numi}];

The resulting functions are unity multipliers:

Table[i[0.666], {i, multiplierfuncs}]

{0.666, 0.666, 0.666}

Note that unlike Composition, the above definition of multiplier only allows for exactly two arguments. The definition can be changed to allow any nonzero number of arguments:

multiplier[arg__] := Times[arg, #] &

Nemerle

Translation of: Python
using System;
using System.Console;
using Nemerle.Collections.NCollectionsExtensions;

module FirstClassNums
{
    Main() : void
    {
        def x = 2.0;   def xi = 0.5;
        def y = 4.0;   def yi = 0.25;
        def z = x + y; def zi = 1.0 / (x + y);
        def multiplier = fun (a, b) {fun (c) {a * b * c}};
        def nums = [x, y, z];
        def inums = [xi, yi, zi];
        WriteLine($[multiplier(n, m) (0.5)|(n, m) in ZipLazy(nums, inums)]);
    }
}

Never

func multiplier(a : float, b : float) -> (float) -> float {
    let func(m : float) -> float { a * b * m }
}

func main() -> int {
    var x = 2.0;
    var xi = 0.5;
    var y = 4.0;
    var yi = 0.25;
    var z = x + y;
    var zi = 1.0 / z;

    var f = [ x, y, z ] : float;
    var i = [ xi, yi, zi ] : float;
    var c = 0;
    var mult = let func(m : float) -> float { 0.0 };
    
    for (c = 0; c < 3; c = c + 1) {
        mult = multiplier(f[c], i[c]);
        prints(f[c] + " * " + i[c] + " * " + 1.0 + " = " + mult(1) + "\n")
    };

    0
}
Output:
2.00 * 0.50 * 1.00 = 1.00
4.00 * 0.25 * 1.00 = 1.00
6.00 * 0.17 * 1.00 = 1.00

Nim

func multiplier(a, b: float): auto =
  let ab = a * b
  result = func(c: float): float = ab * c

let
  x = 2.0
  xi = 0.5
  y  = 4.0
  yi = 0.25
  z  = x + y
  zi = 1.0 / ( x + y )

let list = [x, y, z]
let invlist = [xi, yi, zi]

for i in 0..list.high:
  # Create a multiplier function...
  let f = multiplier(list[i], invlist[i])
  # ... and apply it.
  echo f(0.5)
Output:
0.5
0.5
0.5

Objeck

Similar however this code does not generate a list of functions.

use Collection.Generic;

class FirstClass {
  function : Main(args : String[]) ~ Nil {
    x := 2.0;
    xi := 0.5;
    y := 4.0;
    yi := 0.25;
    z := x + y;
    zi := 1.0 / (x + y);

    numlist := CompareVector->New()<FloatHolder>;
    numlist->AddBack(x); numlist->AddBack(y); numlist->AddBack(z);

    numlisti := Vector->New()<FloatHolder>;
    numlisti->AddBack(xi); numlisti->AddBack(yi); numlisti->AddBack(zi);
    
    each(i : numlist) {
      v := numlist->Get(i); vi := numlisti->Get(i);
      mult := Multiplier(v, vi);
      r := mult(0.5);
      "{$v} * {$vi} * 0.5 = {$r}"->PrintLine();
    };
  }
  
  function : Multiplier(a : FloatHolder, b : FloatHolder) ~ (FloatHolder) ~ FloatHolder {
    return \(FloatHolder) ~ FloatHolder : (c) =>  a * b * c;
  }
}
Output:
2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.166667 * 0.5 = 0.5

OCaml

# let x = 2.0;;

# let y = 4.0;;

# let z = x +. y;;

# let coll = [ x; y; z];;

# let inv_coll = List.map (fun x -> 1.0 /. x) coll;;

# let multiplier n1 n2 = (fun t -> n1 *. n2 *. t);;

(* create a list of new functions *)
# let func_list = List.map2 (fun n inv -> (multiplier n inv)) coll inv_coll;;

# List.map (fun f -> f 0.5) func_list;;
- : float list = [0.5; 0.5; 0.5]

(* or just apply the generated function immediately... *)
# List.map2 (fun n inv -> (multiplier n inv) 0.5) coll inv_coll;;
- : float list = [0.5; 0.5; 0.5]

Oforth

: multiplier(n1, n2)  #[ n1 n2 * * ] ;

: firstClassNum
| x xi y yi z zi | 
   2.0 ->x 
   0.5 ->xi
   4.0 ->y
   0.25 ->yi
   x y + ->z
   x y + inv ->zi
   [ x, y, z ] [ xi, yi, zi ] zipWith(#multiplier) map(#[ 0.5 swap perform ] ) . ;
Output:
[0.5, 0.5, 0.5]

Oz

declare

  [X Y Z] = [2.0  4.0  Z=X+Y]
  [XI YI ZI] = [0.5  0.25  1.0/(X+Y)]

  fun {Multiplier A B}
     fun {$ M}
        A * B * M
     end
  end

in
 
  for
     N in [X  Y  Z]
     I in [XI YI ZI]
  do
     {Show {{Multiplier N I} 0.5}}
  end

"Multiplier" is like "Compose", but with multiplication instead of function application. Otherwise the code is identical except for the argument types (numbers instead of functions).

PARI/GP

Works with: PARI/GP version 2.4.2 and above
multiplier(n1,n2)={
  x -> n1 * n2 * x
};

test()={
  my(x = 2.0, xi = 0.5, y = 4.0, yi = 0.25, z = x + y, zi = 1.0 / ( x + y ));
  print(multiplier(x,xi)(0.5));
  print(multiplier(y,yi)(0.5));
  print(multiplier(z,zi)(0.5));
};

The two are very similar, though as requested the test numbers are in 6 variables instead of two vectors.

Pascal

Works with FPC (currently only version 3.3.1).

program FunTest;
{$mode objfpc}
{$modeswitch functionreferences}
{$modeswitch anonymousfunctions}
uses
  SysUtils;

type
  TMultiplier = reference to function(n: Double): Double;

function GetMultiplier(a, b: Double): TMultiplier;
var
  prod: Double;
begin
  prod := a * b;
  Result := function(n: Double): Double begin Result := prod * n end;
end;

var
  Multiplier: TMultiplier;
  I: Integer;
  x, xi, y, yi: Double;
  Numbers, Inverses:  array of Double;
begin
  x  := 2.0;
  xi := 0.5;
  y  := 4.0;
  yi := 0.25;
  Numbers := [x, y, x + y];
  Inverses := [xi, yi, 1.0 / (x + y)];
  for I := 0 to High(Numbers) do begin
    Multiplier := GetMultiplier(Numbers[I], Inverses[I]);
    WriteLn(Multiplier(0.5));
  end;
end.
Output:
5.0000000000000000E-001
5.0000000000000000E-001
5.0000000000000000E-001

Perl

sub multiplier {
    my ( $n1, $n2 ) = @_;
    sub {
        $n1 * $n2 * $_[0];
    };
}

my $x  = 2.0;
my $xi = 0.5;
my $y  = 4.0;
my $yi = 0.25;
my $z  = $x + $y;
my $zi = 1.0 / ( $x + $y );

my %zip;
@zip{ $x, $y, $z } = ( $xi, $yi, $zi );

while ( my ( $number, $inverse ) = each %zip ) {
    print multiplier( $number, $inverse )->(0.5), "\n";
}

Output:

0.5
0.5
0.5

The entry in first-class functions uses the same technique:

sub compose {
    my ($f, $g) = @_;
 
    sub {
        $f -> ($g -> (@_));
    };
}
...
compose($flist1[$_], $flist2[$_]) -> (0.5)

Phix

Just as there is no real support for first class functions, not much that is pertinent to this task for numbers either, but the manual way is just as trivial:

with javascript_semantics
sequence mtable = {}

function multiplier(atom n1, atom n2)
    mtable = append(mtable,{n1,n2})
    return length(mtable)   
end function

function call_multiplier(integer f, atom m)
atom {n1,n2} = mtable[f]
    return n1*n2*m
end function

constant x = 2,
         xi = 0.5,
         y  = 4,
         yi = 0.25,
         z  = x + y,
         zi = 1 / ( x + y )

?call_multiplier(multiplier(x,xi),0.5)
?call_multiplier(multiplier(y,yi),0.5)
?call_multiplier(multiplier(z,zi),0.5)
Output:
0.5
0.5
0.5

I should perhaps note that output in Phix automatically rounds to the specified precision (10 d.p. if none) so 4.9999 to two decimal places is shown as 5.00, and you can be pretty sure that sort of thing is happening on the last line.

Compared to first class functions, there are (as in my view there should be) significant differences in the treatment of numbers and functions, but as mentioned on that page tagging ctable entries should be quite sufficient.

with javascript_semantics
sequence ctable = {}
 
function compose(integer f, g)
    ctable = append(ctable,{f,g})
    integer cdx = length(ctable)
    return cdx
end function
 
function call_composite(integer cdx, atom x)
    integer {f,g} = ctable[cdx]
    return f(g(x))
end function
 
function plus1(atom x)
    return x+1
end function
 
function halve(atom x)
    return x/2
end function
 
constant m = compose(halve,plus1)
 
?call_composite(m,1)    -- displays 1
?call_composite(m,4)    -- displays 2.5

PicoLisp

(load "@lib/math.l")

(de multiplier (N1 N2)
   (curry (N1 N2) (X)
      (*/ N1 N2 X `(* 1.0 1.0)) ) )

(let (X 2.0  Xi 0.5  Y 4.0  Yi 0.25  Z (+ X Y)  Zi (*/ 1.0 1.0 Z))
   (mapc
      '((Num Inv)
         (prinl (format ((multiplier Inv Num) 0.5) *Scl)) )
      (list X Y Z)
      (list Xi Yi Zi) ) )

Output:

0.500000
0.500000
0.500001

This follows the same structure as First-class functions#PicoLisp, just that the function 'multiplier' above accepts two numbers, while 'compose' below accepts two functions:

(load "@lib/math.l")

(de compose (F G)
   (curry (F G) (X)
      (F (G X)) ) )

(de cube (X)
   (pow X 3.0) )

(de cubeRoot (X)
   (pow X 0.3333333) )

(mapc
   '((Fun Inv)
      (prinl (format ((compose Inv Fun) 0.5) *Scl)) )
   '(sin  cos  cube)
   '(asin acos cubeRoot) )

With a similar output:

0.500001
0.499999
0.500000

Python

This new task:

IDLE 2.6.1      
>>> # Number literals
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25
>>> # Numbers from calculation
>>> z  = x + y
>>> zi = 1.0 / (x + y)
>>> # The multiplier function is similar to 'compose' but with numbers
>>> multiplier = lambda n1, n2: (lambda m: n1 * n2 * m)
>>> # Numbers as members of collections
>>> numlist = [x, y, z]
>>> numlisti = [xi, yi, zi]
>>> # Apply numbers from list
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)]
[0.5, 0.5, 0.5]
>>>

The Python solution to First-class functions for comparison:

>>> # Some built in functions and their inverses
>>> from math import sin, cos, acos, asin
>>> # Add a user defined function and its inverse
>>> cube = lambda x: x * x * x
>>> croot = lambda x: x ** (1/3.0)
>>> # First class functions allow run-time creation of functions from functions
>>> # return function compose(f,g)(x) == f(g(x))
>>> compose = lambda f1, f2: ( lambda x: f1(f2(x)) )
>>> # first class functions should be able to be members of collection types
>>> funclist = [sin, cos, cube]
>>> funclisti = [asin, acos, croot]
>>> # Apply functions from lists as easily as integers
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)]
[0.5, 0.4999999999999999, 0.5]
>>>

As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example.

R

multiplier <- function(n1,n2) { (function(m){n1*n2*m}) }
x  = 2.0
xi = 0.5
y  = 4.0
yi = 0.25
z  = x + y
zi = 1.0 / ( x + y )
num = c(x,y,z)
inv = c(xi,yi,zi)

multiplier(num,inv)(0.5) 

Output
[1] 0.5 0.5 0.5

Compared to original first class functions

sapply(mapply(compose,f1,f2),do.call,list(.5))
[1] 0.5 0.5 0.5

Racket

#lang racket

(define x  2.0)
(define xi 0.5)
(define y  4.0)
(define yi 0.25)
(define z  (+ x y))
(define zi (/ 1.0 (+ x y)))

(define ((multiplier x y) z) (* x y z))

(define numbers  (list x  y  z))
(define inverses (list xi yi zi))

(for/list ([n numbers] [i inverses])
  ((multiplier n i) 0.5))
;; -> '(0.5 0.5 0.5)

Raku

(formerly Perl 6)

Works with: Rakudo version 2015-09-10
sub multiplied ($g, $f) { return { $g * $f * $^x } }
 
my $x  = 2.0;
my $xi = 0.5;
my $y  = 4.0;
my $yi = 0.25;
my $z  = $x + $y;
my $zi = 1.0 / ( $x + $y );

my @numbers = $x, $y, $z;
my @inverses = $xi, $yi, $zi;
 
for flat @numbers Z @inverses { say multiplied($^g, $^f)(.5) }

Output:

0.5
0.5
0.5

The structure of this is identical to first-class function task.

REXX

The REXX language doesn't have an easy method to call functions by using a variable name,
but the interpret instruction can be used to provide that capability.

/*REXX program to use a  first-class function  to  use numbers analogously.             */
nums=   2.0     4.0      6.0                     /*various numbers,  can have fractions.*/
invs= 1/2.0   1/4.0    1/6.0                     /*inverses of the above (real) numbers.*/
   m=   0.5                                      /*multiplier when invoking new function*/
             do j=1  for words(nums);   num= word(nums, j);  inv= word(invs, j)
             nf= multiplier(num, inv);  interpret call nf m       /*sets the var RESULT.*/
             say 'number=' @(num)    'inverse=' @(inv)    'm=' @(m)    'result=' @(result)
             end   /*j*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
@:          return left( arg(1) / 1, 15)         /*format the number,  left justified.  */
multiplier: procedure expose n1n2; parse arg n1,n2;   n1n2= n1 * n2;   return 'a_new_func'
a_new_func: return n1n2 * arg(1)
output   when using the internal default inputs:
number= 2               inverse= 0.5             m= 0.5             result= 0.5
number= 4               inverse= 0.25            m= 0.5             result= 0.5
number= 6               inverse= 0.166666667     m= 0.5             result= 0.5

Ruby

multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}}
numlist = [x=2, y=4, x+y]
invlist = [0.5, 0.25, 1.0/(x+y)]
p numlist.zip(invlist).map {|n, invn| multiplier[invn, n][0.5]}
# => [0.5, 0.5, 0.5]

This structure is identical to the treatment of Ruby's first-class functions -- create a Proc object that returns a Proc object (a closure). These examples show that 0.5 times number n (or passed to function f) times inverse of n (or passed to inverse of f) returns the original number, 0.5.

Rust

#![feature(conservative_impl_trait)]
fn main() {
    let (x, xi) = (2.0, 0.5);
    let (y, yi) = (4.0, 0.25);
    let z = x + y;
    let zi = 1.0/z;

    let numlist = [x,y,z];
    let invlist = [xi,yi,zi];

    let result = numlist.iter()
                        .zip(&invlist)
                        .map(|(x,y)| multiplier(*x,*y)(0.5))
                        .collect::<Vec<_>>();
    println!("{:?}", result);
}

fn multiplier(x: f64, y: f64) -> impl Fn(f64) -> f64 {
    move |m| x*y*m
}

This is very similar to the first-class functions implementation save that the type inference works a little bit better here (e.g. when declaring numlist and invlist) and multiplier's declaration is substantially simpler than compose's. Both of these boil down to the fact that closures and regular functions are actually different types in Rust so we have to be generic over them but here we are only dealing with 64-bit floats.

Scala

scala> val x = 2.0
x: Double = 2.0

scala> val xi = 0.5
xi: Double = 0.5

scala> val y = 4.0
y: Double = 4.0

scala> val yi = 0.25
yi: Double = 0.25

scala> val z = x + y
z: Double = 6.0

scala> val zi = 1.0 / ( x + y )
zi: Double = 0.16666666666666666

scala> val numbers = List(x, y, z)
numbers: List[Double] = List(2.0, 4.0, 6.0)

scala> val inverses = List(xi, yi, zi)
inverses: List[Double] = List(0.5, 0.25, 0.16666666666666666)

scala> def multiplier = (n1: Double, n2: Double) => (m: Double) => n1 * n2 * m
multiplier: (Double, Double) => (Double) => Double

scala> def comp = numbers zip inverses map multiplier.tupled  
comp: List[(Double) => Double]

scala> comp.foreach(f=>println(f(0.5)))                     
0.5
0.5
0.5

Scheme

This implementation closely follows the Scheme implementation of the First-class functions problem.

(define x  2.0)
(define xi 0.5)
(define y  4.0)
(define yi 0.25)
(define z  (+ x y))
(define zi (/ (+ x y)))

(define number (list x y z))
(define inverse (list xi yi zi))

(define (multiplier n1 n2) (lambda (m) (* n1 n2 m)))

(define m 0.5)
(define (go n1 n2)
  (for-each (lambda (n1 n2)
              (display ((multiplier n1 n2) m))
              (newline))
            n1 n2))
(go number inverse)

Output:

0.5
0.5
0.5

Sidef

func multiplier(n1, n2) {
    func (n3) {
        n1 * n2 * n3
    }
}

var x  = 2.0
var xi = 0.5
var y  = 4.0
var yi = 0.25
var z  = (x + y)
var zi = (1 / (x + y))

var numbers = [x, y, z]
var inverses = [xi, yi, zi]

for f,g (numbers ~Z inverses) {
    say multiplier(f, g)(0.5)
}
Output:
0.5
0.5
0.5

Slate

This example is incorrect. Please fix the code and remove this message.

Details: Compare and contrast the resultant program with the corresponding entry in First-class functions.

define: #multiplier -> [| :n1 :n2 | [| :m | n1 * n2 * m]].
define: #x -> 2.
define: #y -> 4.
define: #numlist -> {x. y. x + y}.
define: #numlisti -> (numlist collect: [| :x | 1.0 / x]).

numlist with: numlisti collect: [| :n1 :n2 | (multiplier applyTo: {n1. n2}) applyWith: 0.5].

Tcl

Works with: Tcl version 8.5
package require Tcl 8.5
proc multiplier {a b} {
    list apply {{ab m} {expr {$ab*$m}}} [expr {$a*$b}]
}

Note that, as with Tcl's solution for First-class functions, the resulting term must be expanded on application. For example, study this interactive session:

% set mult23 [multiplier 2 3]
apply {{ab m} {expr {$ab*$m}}} 6
% {*}$mult23 5
30

Formally, for the task:

set x  2.0
set xi 0.5
set y  4.0
set yi 0.25
set z  [expr {$x + $y}]
set zi [expr {1.0 / ( $x + $y )}]
set numlist [list $x $y $z]
set numlisti [list $xi $yi $zi]
foreach a $numlist b $numlisti {
    puts [format "%g * %g * 0.5 = %g" $a $b [{*}[multiplier $a $b] 0.5]]
}

Which produces this output:

2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.166667 * 0.5 = 0.5

TXR

This solution seeks a non-strawman interpretation of the exercise: to treat functions and literal numeric terms under the same operations. We develop a three-argument function called binop whose argument is an ordinary function which works on numbers, and two arithmetic arguments which are any combination of functions or numbers. The functions may have any arity from 0 to 2. The binop functions handles all the cases.

The basic rules are:

  • When all required arguments are given to a function, it is expected that a number will be produced.
  • Zero-argument functions are called to force a number out of them.
  • When operands are numbers or zero-argument functions, a numeric result is calculated.
  • Otherwise the operation is a functional combinator, returning a function.
(defun binop (numop x y)
  (typecase x
    (number (typecase y
              (number [numop x y])
              (fun (caseql (fun-fixparam-count y)
                     (0 [numop x [y]])
                     (1 (ret [numop x [y @1]]))
                     (2 (ret [numop x [y @1 @2]]))
                     (t (error "~s: right argument has too many params"
                               %fun% y))))
              (t (error "~s: right argument must be function or number"
                        %fun% y))))
    (fun (typecase y
           (number (caseql (fun-fixparam-count x)
                     (0 [numop [x] y])
                     (1 (ret [numop [x @1] y]))
                     (2 (ret [numop [x @1 @2] y]))
                     (t (error "~s: left argument has too many params"
                               %fun% x))))
           (fun (macrolet ((pc (x-param-count y-param-count)
                                ^(+ (* 3 ,x-param-count) ,y-param-count)))
                   (caseql* (pc (fun-fixparam-count x) (fun-fixparam-count y))
                     (((pc 0 0)) [numop [x] [y]])
                     (((pc 0 1)) (ret [numop [x] [y @1]]))
                     (((pc 0 2)) (ret [numop [x] [y @1 @2]]))
                     (((pc 1 0)) (ret [numop [x @1] [y]]))
                     (((pc 1 1)) (ret [numop [x @1] [y @1]]))
                     (((pc 1 2)) (ret [numop [x @1] [y @1 @2]]))
                     (((pc 2 0)) (ret [numop [x @1 @2] [y]]))
                     (((pc 2 1)) (ret [numop [x @1 @2] [y @1]]))
                     (((pc 2 2)) (ret [numop [x @1 @2] [y @1 @2]]))
                     (t (error "~s: one or both arguments ~s and ~s\ \
                               have excess arity" %fun% x y)))))))
      (t (error "~s: left argument must be function or number"
              %fun% y))))

(defun f+ (x y) [binop + x y])
(defun f- (x y) [binop - x y])
(defun f* (x y) [binop * x y])
(defun f/ (x y) [binop / x y])

With this, the following sort of thing is possible:

1> [f* 6 4]  ;; ordinary arithmetic.
24
2> [f* f+ f+]  ;; product of additions
#<interpreted fun: lambda (#:arg-1-0062 #:arg-2-0063 . #:arg-rest-0061)>
3> [*2 10 20]  ;; i.e. (* (+ 10 20) (+ 10 20)) -> (* 30 30)
900
4> [f* 2 f+]   ;; doubled addition
#<interpreted fun: lambda (#:arg-1-0017 #:arg-2-0018 . #:arg-rest-0016)>
5> [*4 11 19]  ;; i.e. (* 2 (+ 11 19))
60
6> [f* (op f+ 2 @1) (op f+ 3 @1)]
#<interpreted fun: lambda (#:arg-1-0047 . #:arg-rest-0046)>
7> [*6 10 10]  ;; i.e. (* (+ 2 10) (+ 3 10)) -> (* 12 13)
156

So with these definitions, we can solve the task like this, which demonstrates that numbers and functions are handled by the same operations:

(let* ((x 2.0)
       (xi 0.5)
       (y 4.0)
       (yi 0.25)
       (z (lambda () (f+ x y))) ;; z is obviously function
       (zi (f/ 1 z)))           ;; also a function
  (flet ((multiplier (a b) (op f* @1 (f* a b))))
    (each ((n (list x y z))
           (v (list xi yi zi)))
      (prinl [[multiplier n v] 42.0]))))
Output:
42.0
42.0
42.0

Ursala

The form is very similar to the first class functions task solution in Ursala, except that the multiplier function takes the place of the composition operator (+), and is named in compliance with the task specification.

#import std
#import flo

numbers  = <2.,4.,plus(2.,4.)>
inverses = <0.5,0.25,div(1.,plus(2.,4.))>

multiplier = //times+ times

#cast %eL

main = (gang multiplier*p\numbers inverses) 0.5

The multiplier could have been written in pattern matching form like this.

multiplier("a","b") "c" = times(times("a","b"),"c")

The main program might also have been written with an anonymous function like this.

main = (gang (//times+ times)*p\numbers inverses) 0.5

output:

<5.000000e-01,5.000000e-01,5.000000e-01>

Wren

Library: Wren-fmt
import "./fmt" for Fmt

var multiplier = Fn.new { |n1, n2| Fn.new { |m| n1 * n2 * m } }

var orderedCollection = Fn.new {
    var x  = 2.0
    var xi = 0.5
    var y  = 4.0
    var yi = 0.25
    var z  = x + y
    var zi = 1.0 / ( x + y )
    return [[x, y, z], [xi, yi, zi]]
}

var oc = orderedCollection.call()
for (i in 0..2) {
    var x = oc[0][i]
    var y = oc[1][i]
    var m = 0.5 // rather than 1 to compare with first-class functions task
    Fmt.print("$0.1g * $g * $0.1g = $0.1g", x, y, m, multiplier.call(x, y).call(m))
}
Output:

Agreeing with results of first-class functions task:

2.0 * 0.5      * 0.5 = 0.5
4.0 * 0.25     * 0.5 = 0.5
6.0 * 0.166667 * 0.5 = 0.5

zkl

var x=2.0, y=4.0, z=(x+y), c=T(x,y,z).apply(fcn(n){ T(n,1.0/n) });
  //-->L(L(2,0.5),L(4,0.25),L(6,0.166667))
fcn multiplier(n1,n2){ return('*.fp(n1,n2)) }

This is actually partial evaluation, multiplier returns n1*n2*X where X isn't known yet. So multiplier(2,3)(4) --> (2*3*)4 --> 24. Even better, multiplier(2,3)(4,5) --> 120, multiplier(2,3)(4,5,6) --> 720, multiplier(2,3)() --> 6.

Alternatively,

fcn multiplier(n1,n2){ fcn(n,X){ n*X }.fp(n1*n2) }
var ms=c.apply(fcn([(n1,n2)]){ multiplier(n1,n2) });
   //-->L(Deferred,Deferred,Deferred) // lazy eval of n*(1/n)*X
ms.run(True,1.0)  //-->L(1,1,1)
ms.run(True,5.0)  //-->L(5,5,5)
ms.run(True,0.5)  //-->L(0.5,0.5,0.5)

List.run(True,X), for each item in the list, does i(X) and collects the results into another list. Sort of an inverted map or fold.