Files
mercury/tests/hard_coded/fold_tests.m
Zoltan Somogyi 1db00c99aa Add gap_foldl, last_gap_foldl, chunk_foldl{,2,3,4}.
library/list.m:
    Add the above predicates to the list module.

NEWS:
    Announce the new predicates.

tests/hard_coded/fold_tests.{m,exp}:
    Test case for the new functionality.

tests/hard_coded/Mmakefile:
    Enable the new test case.
2022-03-15 12:18:07 +11:00

46 lines
1.3 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module fold_tests.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
TestList = 1 .. 10,
test_write(TestList, !IO),
list.foldl(prepend_int_to_string, TestList, "", Str0),
list.chunk_foldl(3, prepend_int_to_string, TestList, "", Str1),
io.format("using foldl: %s\n", [s(Str0)], !IO),
io.format("using chunk_foldl: %s\n", [s(Str1)], !IO).
:- pred prepend_int_to_string(int::in, string::in, string::out) is det.
prepend_int_to_string(N, !Str) :-
( if !.Str = "" then
!:Str = string.int_to_string(N)
else
!:Str = string.int_to_string(N) ++ "_" ++ !.Str
).
:- pred test_write(list(int)::in, io::di, io::uo) is det.
test_write(TestList, !IO) :-
list.gap_foldl(io.write_int, io.write_string(", "), TestList, !IO),
io.nl(!IO),
list.last_gap_foldl(io.write_int, io.write_string(", "),
io.write_string(" and "), TestList, !IO),
io.nl(!IO).