home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / SOLVE.LF < prev    next >
Text File  |  1996-06-04  |  1KB  |  60 lines

  1. % Copyright 1992 Digital Equipment Corporation
  2. % All Rights Reserved
  3.  
  4. % The SEND+MORE=MONEY puzzle.
  5. % Author: Hassan Ait-Kaci
  6.  
  7. module("solve") ?
  8. public(solve) ?
  9.  
  10. solve :- 
  11.     % Solutions where M=0 are uninteresting:
  12.     M=1,
  13.     
  14.     % The arithmetic constraints:
  15.     C3 + S + M = O + 10*M,
  16.           C2 + E + O = N + 10*C3,
  17.           C1 + N + R = E + 10*C2,
  18.              D + E = Y + 10*C1,
  19.  
  20.     % The all-distinct constraints:
  21.     diff_list([S,E,N,D,M,O,R,Y]),
  22.  
  23.     % Generating binary digits:
  24.     C1=carry,
  25.     C2=carry,
  26.     C3=carry,
  27.  
  28.     % Generating decimal digits:
  29.           S=decimal,
  30.           E=decimal,
  31.           N=decimal,
  32.           D=decimal,
  33.     O=decimal,
  34.           R=decimal,
  35.           Y=decimal,
  36.  
  37.     % Print the result:
  38.     nl,
  39.     write(" SEND     ",S,E,N,D),nl,
  40.     write("+MORE    +",M,O,R,E),nl,
  41.     write("-----    -----"),nl,
  42.     write("MONEY    ",M,O,N,E,Y),nl,
  43.     nl,
  44.     fail.
  45.  
  46. decimal -> {0;1;2;3;4;5;6;7;8;9}.
  47.  
  48. carry -> {0;1}.
  49.  
  50. diff_list([]).
  51. diff_list([H|T]) :-
  52.     generate_diffs(H,T),
  53.     diff_list(T),
  54.     H=<9,
  55.     H>=0.
  56.  
  57. generate_diffs(H,[]).
  58. generate_diffs(H,[A|T]) :-
  59.     generate_diffs(H,T),
  60.     A=\=H.