[mercury-users] Doing something with all solutions

Nikolas Everett nik9000 at gmail.com
Thu Jul 19 09:41:34 AEST 2007


I'm sure you get this a lot, but how can I ask Mercury to do something with
all the solutions that it finds for some predicate?  At this point I'd just
like to print out all of my solutions, but I'm sure there are better things
to do then that.

I've started with one of the programs in the Ralph Becket's tutorial and
produced the below.  I've messed around a little with the solutions module,
but haven't gotten any where.

Thanks in advance, and sorry to bother the list with what feels like a
newbie question.  If answers to questions like these can be found somewhere,
please let me know where to look.

:- module first.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is cc_multi.

:- implementation.
:- import_module int, list, string.

main(!IO) :-
    io.format("DOG + ANT = CAT\n", [], !IO),
    ( if
        dogAntCat(DOG, ANT, CAT)
    then
        io.format("%d + %d = %d\n", [i(DOG), i(ANT), i(CAT)], !IO)
    else
        io.format("No result.", [], !IO)
    ).

:- pred dogAntCat(int::out, int::out, int::out) is nondet.
dogAntCat(DOG, ANT, CAT) :-
    ( if
        D0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,
        pick(D0, G, D1),
        pick(D1, T, D2),
        T = (G + T + C0) mod 10, C1 = (G + T + C0) / 10,
        pick(D2, O, D3),
        pick(D3, N, D4),
        A = (O + N + C1) mod 10, A\=0, C2 = (O + N + C1) / 10,
        pick(D4, D, D5),
        pick(D5, A, D6),
        C = (D + A + C2), D \= 0, 0  = (D + A + C2) / 10,
        pick(D6, C, _)
    then
        DOG = 100 * D + 10 * O + G,
        ANT = 100 * A + 10 * N + T,
        CAT = 100 * C + 10 * A + T
    else
        fail
    ).

:- pred pick(list(int)::in, int::out, list(int)::out) is nondet.
pick([X | Xs], X, Xs).
pick([X | Xs], Y, [X | Zs]) :- pick(Xs, Y, Zs).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20070718/a5bf280c/attachment.html>


More information about the users mailing list