I was trying to write Prolog code for "SEND MODE MONEY". So far, I got the following:
:- use_module(library(clpfd)).
puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]) :-
Vars = [S,E,N,D,M,O,R,Y],
Vars ins 0..9,
all_different(Vars),
S*1000 + E*100 + N*10 + D + M*1000 + O*100 + R*10 + E
#= M*10000 + O*1000 + N*100 + E*10 + Y,
M #\= 0,
S #\= 0,
label([S,E,N,D,M,O,R,Y]),
format('~s ~w ~s ~w ~s ~w ~s ~w ~s ~s ~s ~w ~s ~w ~s ~w ~s ~w ~s ~s ~s ~w ~s ~w ~s ~w ~s ~w ~s ~w ~s ~s',
[ "[", S, ",", E, ",", N, ",", D, "]",
"+", "[", M, ",", O, ",", R, ",", E, "]",
"=", "[", M, ",", O, ",", N, ",", E, ",", Y, "]", ";" ]).
The output is as follows:
?- puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]).
[ 9 , 5 , 6 , 7 ] + [ 1 , 0 , 8 , 5 ] = [ 1 , 0 , 6 , 5 , 2 ] ;
S = 9,
E = 5,
N = 6,
D = 7,
M = 1,
O = 0,
R = 8,
Y = 2.
But I want to print the list at the end not at the beginning how can I do it?
The output I need to get is:
?- puzzle([S,E,N,D] + [M,O,R,E] = [M,O,N,E,Y]).
S = 9,
E = 5,
N = 6,
D = 7,
M = 1,
O = 0,
R = 8,
Y = 2.
[ 9 , 5 , 6 , 7 ] + [ 1 , 0 , 8 , 5 ] = [ 1 , 0 , 6 , 5 , 2 ] ;
Thank you.