Anonymous user
First-class functions: Difference between revisions
→{{header|Mercury}}: Reimplemented using more idiomatic use of higher-order functions for ease of expression.
m (→firstclass.m: Corrected composition order.) |
(→{{header|Mercury}}: Reimplemented using more idiomatic use of higher-order functions for ease of expression.) |
||
Line 864:
# A list of "forward" functions is provided (sin, cosine and a lambda that calls ln).
# A list of "reverse" functions is provided (asin, acosine and a lambda that calls exp).
# The lists are mapped in corresponding members through an anonymous function that compose the resulting pairs of functions and apply them to the value 0.5.
# The results are returned and printed when all function pairs have been processed.
===firstclass.m===
Line 884 ⟶ 880:
Forward = [math.sin, math.cos, (func(X) = math.ln(X))],
Reverse = [math.asin, math.acos, (func(X) = math.exp(X))],
Results = list.map_corresponding(
(func(F, R) =
compose(R, F, 0.5)
), Forward, Reverse),
io.write_list(Results, ", ", io.write_float, !IO),
io.write_string("\n", !IO).</lang>
===Use and output===
|