% sendmore_prolog.pl


mm1([S,E,N,D,M,O,R,Y]) :-
     generate1([S,E,N,D,M,O,R,Y]),      % generate
     S>0, M >0,                         % test
     alldiff([S,E,N,D,M,O,R,Y]),        % test
     test_sum(S,E,N,D,M,O,R,Y).         % test

test_sum(S, E, N, D, M, O, R, Y) :-
     V1 is             1000*S + 100*E + 10*N + D 
          +            1000*M + 100*O + 10*R + E,
     V2 is 10000*M + 1000*O + 100*N + 10*E + Y,
     V1 = V2.
generate1([]).
generate1([X|Xs]):-
    member(X,[0,1,2,3,4,5,6,7,8,9]),
    generate1(Xs).

alldiff([]).

alldiff([X|L]):-
    diff(X,L),
    alldiff(L).
diff(X,[]).
diff(X,[V|L]):-
     X \= V,
     diff(X,L).
