fork download
  1. :- module laba.
  2. :- interface.
  3. :- import_module io.
  4. :- pred main(io::di, io::uo) is det.
  5. :- implementation.
  6. :- import_module int, list, string, solutions.
  7.  
  8. main(!IO) :-
  9. io.format("rsrx + zvv = srxs\n", [], !IO),
  10. ( if
  11. solutions.solutions(find, Results),
  12. list.length(Results, Count), Count > 0
  13. then
  14. io.format("found %d results:\n", [i(Count)], !IO),
  15. write_list(Results, "", print_solution, !IO)
  16. else
  17. io.format("has no solutions\n", [], !IO)
  18. ).
  19.  
  20. :- pred print_solution({int,int,int}::in, io::di, io::uo) is det.
  21. print_solution({RSRX, ZVV, SRXS}, !IO) :-
  22. io.format("%d + %d = %d\n", [i(RSRX) , i(ZVV), i(SRXS)], !IO).
  23.  
  24. :- pred find({int, int, int}::out) is nondet.
  25. find({RSRX, ZVV, SRXS}) :-
  26. Ds0 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], C0 = 0,
  27. pick(Ds0, X, Ds1), S1 = X+V+C0, S = S1 mod 10, C1 = S1/10,
  28. pick(Ds1, R, Ds2),
  29. pick(Ds2, V, Ds3), S2 = R+V+C1, X = S2 mod 10, C2 = S2/10, R \= 0,
  30. pick(Ds3, S, Ds4),
  31. pick(Ds4, Z, _), S3 = S+Z+C2, R = S3 mod 10, C3 = S3/10, Z \= 0,
  32. S4 = R + C3, S = S4 mod 10, C4 = S4/10,
  33. C4 = 0,
  34. RSRX = 1000 R + 100 S + 10 R + X,
  35. ZVV = 100 Z + 10 V + V,
  36. SRXS = 1000 S + 100 R + 10 X + S.
  37.  
  38. :- pred pick(list(int)::in, int::out, list(int)::out) is nondet.
  39. pick([X | Xs], X, Xs).
  40. pick([X | Xs], Y, [X | Zs]) :- pick(Xs, Y, Zs).
Success #stdin #stdout #stderr 0.04s 30872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/3sf65j/prog:1:10: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:2:
	catch/3: Undefined procedure: interface/0
Warning: /home/3sf65j/prog:2:
	Goal (directive) failed: user:interface
ERROR: /home/3sf65j/prog:3:17: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:4:8: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:5:
	catch/3: Undefined procedure: implementation/0
Warning: /home/3sf65j/prog:5:
	Goal (directive) failed: user:implementation
ERROR: /home/3sf65j/prog:6:17: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:8:6: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:20:8: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:21:35: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:24:8: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:34:11: Syntax error: Operator expected
ERROR: /home/3sf65j/prog:38:8: Syntax error: Operator expected
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit