Task

Show how to return more than one value from a function.




:- module addsub.
 
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
 
:- implementation.
:- import_module int, list, string.
 
main(!IO) :-
    command_line_arguments(Args, !IO),
    filter_map(to_int, Args, CleanArgs),
    (length(CleanArgs, 2) ->
        X = det_index1(CleanArgs,1),
        Y = det_index1(CleanArgs,2),
        addsub(X, Y, S, D),
        format("%d + %d = %d\n%d - %d = %d\n", 
               [i(X), i(Y), i(S), i(X), i(Y), i(D)], !IO)
    ;
        write_string("Please pass two integers on the command line.\n", !IO)
    ).
 
:- pred addsub(int::in, int::in, int::out, int::out) is det.
addsub(X, Y, S, D) :-
    S = X + Y,
    D = X - Y.
 
:- end_module addsub.