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

Content added Content deleted
(Added Ada (and Removed {{omit From|Ada}}))
(→‎{{header|Mathematica}}: Fixed issue with code not being comparable to first-class functions)
Line 564: Line 564:


=={{header|Mathematica}}==
=={{header|Mathematica}}==
{{incorrect|Mathematica|Compare and contrast the resultant program with the corresponding entry in First-class functions.}}


This code demonstrates the example using structure similar to function composition, however the composition function is replace with the multiplier function.
<lang Mathematica>f[x_, y_] := x*y*# &
f[a, b]
x = 2; xi = 0.5; y = 4; yi = 0.25; z = x + y; zi = 1/(x + y);
f[x, xi][0.5]
f[y, yi][0.5]
f[z, zi][0.5]</lang>


<lang Mathematica>multiplier[n1_,n2_]:=n1 n2 #&
For example:
num={2,4,2+4};
<pre>a b #1 &
numi=1/num;
multiplierfuncs = multiplier @@@ Transpose[{num, numi}];
</lang>


The resulting functions are unity multipliers:
0.5
<pre>Table[i[0.666], {i, multiplierfuncs}]


{0.666, 0.666, 0.666}</pre>
0.5


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:
0.5</pre>
<lang Mathematica>multiplier[arg__] := Times[arg, #] &
</lang>


=={{header|Nemerle}}==
=={{header|Nemerle}}==