Prolog代码出错(运营商预期)

时间:2015-04-20 07:36:07

标签: prolog

我是Prolog编程的新手,并尝试在prolog中实现3×3魔术广场。但是我在编译程序时遇到操作员预期的错误,请告诉我该语句中有什么问题。 感谢

代码:

  :- use_module(library(clpfd),[]).



    magic_square(Puzzle, Solution, Sum) :-
      Puzzle = [S11, S12, S13,
                S21, S22, S23,
                S31, S32, S33],


        Puzzle ins 1..9,  %Statement where it is saying operator expected 
        all_different(Puzzle),




           label(Puzzle),

      /* Rows */
      R1 = [S11, S12, S13],
      R2 = [S21, S22, S23],
      R3 = [S31, S32, S33],

      /* Columns */
      C1 = [S11, S21, S31],
      C2 = [S12, S22, S32],
      C3 = [S13, S23, S33],

      /* Diagonals */
      Diag1 = [S11, S22, S33],
      Diag2 = [S13, S22, S31],

      sum_list(R1, Sum1),
      sum_list(R2, Sum2),
      sum_list(R3, Sum3),
      sum_list(C1, Sum4),
      sum_list(C2, Sum5),
      sum_list(C3, Sum6),
      sum_list(Diag1, Sum7),
      sum_list(Diag2, Sum8),

      Sum1 = Sum2,
      Sum2 = Sum3,
      Sum3 = Sum4,
      Sum4 = Sum5,
      Sum5 = Sum6,
      Sum6 = Sum7,
      Sum7 = Sum8,

      Sum = Sum8,
      Solution = Puzzle.

1 个答案:

答案 0 :(得分:2)

而不是

:- use_module(library(clpfd),[]).

你应该使用

:- use_module(library(clpfd)).

像这样:

:- use_module(library(clpfd)).

magicSquare3_sum(Zs,Sum) :-
   Zs = [S11,S12,S13,
         S21,S22,S23,
         S31,S32,S33],

   Zs ins 1..9, 
   all_different(Zs),

   S11 + S12 + S13 #= Sum,     % rows
   S21 + S22 + S23 #= Sum,
   S31 + S32 + S33 #= Sum,

   S11 + S21 + S31 #= Sum,     % columns
   S12 + S22 + S32 #= Sum,
   S13 + S23 + S33 #= Sum,

   S11 + S22 + S33 #= Sum,     % diagonals
   S13 + S22 + S31 #= Sum.

示例查询:

?- magicSquare3_sum(Zs,Sum), labeling([],Zs).
Zs = [2, 7, 6, 9, 5, 1, 4, 3, 8], Sum = 15 ;
Zs = [2, 9, 4, 7, 5, 3, 6, 1, 8], Sum = 15 ;
Zs = [4, 3, 8, 9, 5, 1, 2, 7, 6], Sum = 15 ;
Zs = [4, 9, 2, 3, 5, 7, 8, 1, 6], Sum = 15 ;
Zs = [6, 1, 8, 7, 5, 3, 2, 9, 4], Sum = 15 ;
Zs = [6, 7, 2, 1, 5, 9, 8, 3, 4], Sum = 15 ;
Zs = [8, 1, 6, 3, 5, 7, 4, 9, 2], Sum = 15 ;
Zs = [8, 3, 4, 1, 5, 9, 6, 7, 2], Sum = 15 ;
false.