Anonymous user
First-class functions/Use numbers analogously: Difference between revisions
First-class functions/Use numbers analogously (view source)
Revision as of 03:08, 20 August 2010
, 13 years ago→{{header|D}}
(rewrite with an attempted understanding of the new problem statement) |
|||
Line 110:
=={{header|D}}==
<lang d>import std.stdio, std.algorithm;▼
void main() {
auto x = 2.0;
auto xi = 0.5;
auto y = 4.0;
auto
auto
auto zi = 1.0 / (x + y);
auto multiplier = (double a, double b) {
return (double m){ return a * b * m; };
};
auto reverse = [xi, yi, zi];
foreach (i, a; forward) {▼
auto b = reverse[i];
}
}</lang>▼
▲</lang>
Output:
<pre>2.000000 * 0.500000 * 0.5 = 0.500000
4.000000 * 0.250000 * 0.5 = 0.500000
Alternative implementation (same output):
▲ (6 * 0.166667)(0.5) = 0.5
▲<lang d>import std.stdio, std.math;
void main() {
auto xi = 0.5;
auto y = 4.0;
auto yi = 0.25;
auto zi = 1.0 / (x + y);
return (double m){ return a * b *
auto reverse = [xi, yi, zi];
▲ };
▲ foreach(i, a; forward) {
</lang>▼
foreach (f; zip(forward, reverse))
writefln("%f * %f * 0.5 = %f", f.at!0, f.at!1,
multiplier(f.at!0, f.at!1)(.5));
▲}</lang>
=={{header|E}}==
|