RosettaCodeData/Task/Hailstone-sequence/Mercury/hailstone-sequence-2.mercury
2014-04-02 16:56:35 +00:00

36 lines
895 B
Text

:- module test_hailstone.
:- interface.
:- import_module io.
:- pred main(io.state::di, io.state::uo) is det.
:- implementation.
:- import_module int, list.
:- import_module hailstone.
:- pred longest(int::in, int::out, int::out) is det.
:- pred longest(int::in, int::in, int::in, int::out, int::out) is det.
longest(M, N, L) :- longest(M, 0, 0, N, L).
longest(N, CN, CL, MN, ML) :-
( N > 1 ->
L = list.length(hailstone(N)),
( L > CL -> longest(N - 1, N, L, MN, ML)
; longest(N - 1, CN, CL, MN, ML) )
; MN = CN, ML = CL ).
main(!IO) :-
S = hailstone(27),
( list.length(S) = 112,
list.append([27, 82, 41, 124], _, S),
list.remove_suffix(S, [8, 4, 2, 1], _),
longest(100000, 77031, 351) ->
io.write_string("All tests succeeded.\n", !IO)
; io.write_string("At least one test failed.\n", !IO) ).
:- end_module test_hailstone.