-1

So I managed to write the SEND + MORE = MONEY program for Prolog and I'm having trouble labeling the results. Any ideas on how to do that? I keep using the labeling function but it still wouldn't work. I'm lost here.

:- lib(ic).

puzzle(List) :-
    List = [S, E, N, D, M, O, R, Y],
    List :: 0..9,
    diff_list(List),
                    1000*S + 100*E + 10*N + D
       +           1000*M + 100*O + 10*R + E
      $= 10000*M + 1000*O + 100*N + 10*E + Y,
    S $\= 0, M $\= 0,
    shallow_backtrack(List).

shallow_backtrack(List) :-
    ( foreach(Var, List) do once(indomain(Var)) ).

diff_list(List) :-
    ( fromto(List, [X|Tail], Tail, []) do
            ( foreach(Y, Tail), param(X) do
                        X $\= Y
            )
    ).

Results:

?- puzzle(X).
X = [9, 5, 6, 7, 1, 0, 8, 2]
Yes (0.00s cpu)

Any help would be appreciated! Thanks!

false
  • 10,264
  • 13
  • 101
  • 209

1 Answers1

1

Here is a variant of your program that uses labeling:

:- lib(ic).

puzzle(List) :-
    List = [S, E, N, D, M, O, R, Y],
    List :: 0..9,
    alldifferent(List),
    1000*S + 100*E + 10*N + D
       +           1000*M + 100*O + 10*R + E
      $= 10000*M + 1000*O + 100*N + 10*E + Y,
    S $\= 0, M $\= 0,
    labeling(List).
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • But the answer doesn't give it to me in labeled form. All it does is ?- puzzle(X). X = [9, 5, 6, 7, 1, 0, 8, 2] Yes (0.00s cpu, solution 1, maybe more) No (0.01s cpu) – user3390252 May 18 '14 at 04:46
  • @user3390252 The term "labeling" in constraint programming means assigning concrete values for every variable. Maybe by using the word "labeling" you just want names for each variable in the answer? Then just call using `puzzle([S, E, N, D, M, O, R, Y])`. – Sergii Dymchenko May 18 '14 at 05:13