Files
mercury/tests/hard_coded/higher_order_func_test.m
Julien Fischer 1f6d83692a Update programming style in tests/hard_coded.
tests/hard_coded/*.m:
    Update programming style, unless doing so would change
    the meaning of the test, in particular:

    - use '.' as a module qualifier in place of '__'
    - use {write,print}_line where appropriate
    - use if-then-else in place of C -> T ; E
    - use state variables in place of DCGs

tests/hard_coded/dir_test.m:
    Document what the expected outputs correspond to.

    Use a uniform module qualifier in the output.

tests/hard_coded/dir_test.exp*:
    Conform to the above change.
2021-01-07 13:58:12 +11:00

37 lines
1.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module higher_order_func_test.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
:- import_module list.
main(!IO) :-
L1 = [1, 2, 3],
L2 = my_map((func(X::in) = (Y::out) is det :- Y = 2*X), L1),
L3 = my_map((func(X2) = Y2 :- Y2 = 5*X2), L2),
L = my_map(func(X3) = 10*X3, L3),
list.foldl(output_int_and_string(" "), L, !IO),
io.nl(!IO).
:- func my_map(func(X) = Y, list(X)) = list(Y).
:- mode my_map(func(in) = out is det, in) = out is det.
:- mode my_map(func(in) = out is semidet, in) = out is semidet.
my_map(_, []) = [].
my_map(F, [H0 | T0]) = [apply(F, H0) | my_map(F, T0)].
:- pred output_int_and_string(string::in, int::in, io::di, io::uo) is det.
output_int_and_string(Str, N, !IO) :-
io.write_int(N, !IO),
io.write_string(Str, !IO).