Search code examples
syntaxprologformatclpfd

Prolog syntax error on format


I am getting a syntax error on my format line and i have no idea why!

magic3(Variables):-
   Variables[A,B,C,D,E,F,G,H,I],
   fd_domain(Variables,1,9),
   fd_all_different(Variables),
   A+B+C #= A+D+G,
   A+B+C #= A+E+I,
   A+B+C #= C+F+I,
   A+B+C #= B+E+H,
   fd_labeling(Variables),
   format("A=~w, B=~w, C=~w~n, D=~w, E=~w, F=~d~w, G=~w, H=~w, I= ~w", Variables).

The code prints

A B C
D E F
G H I

where all rows and columns are equal when added together I can not figure out why I am getting a syntax error.


Solution

  • In several systems you need to put at the beginning of your file/module:

    :- use_module(library(clpfd)).
    

    And as a tiny improvement you can introduce the sum S:

    S #= A+B+C,
    S #= D+E+F, % this too!
    S #= G+H+I,
    ....
    

    And even better, you can now calculate the sum with is (1+2+ ...+9) / 3 = 15. So add S #= 15. There is a nice animation on the Internet for the actual labeling process.


    Further:

    Variables[A,B,...]
    

    should read

    Variables = [A,B,...]