clpfd - n-queens solution not working in Prolog -
i trying run following code n-queens problem..how far can go? find solutions n-queens problem:
generate([],_). generate([h|t],n) :- h in 1..n , generate(t,n). lenlist(l,n) :- lenlist(l,0,n). lenlist([],n,n). lenlist([_|t],p,n) :- p1 p+1 , lenlist(t,p1,n). queens(n,l) :- generate(l,n),lenlist(l,n), safe(l),!, labeling([ffc],l). notattack(x,xs) :- notattack(x,xs,1). notattack(x,[],n). notattack(x,[y|ys],n) :- x #\= y, x #\= y - n, x #\= y + n, n1 n + 1, notattack(x,ys,n1). safe([]). safe([f|t]) :- notattack(f,t), safe(t). i have swi-prolog installed on debian-9 (stable) linux , running above using command "swipl -f nqueens.pl". on loading, error:
syntax error: operator expected (probably on 2nd code line) where problem , how can solved? help.
the question mentions writting in clpfd (a constraint logic programming tool on finite domains). have import library:
:- use_module(library(clpfd)). generate([],_). generate([h|t],n) :- h in 1..n , generate(t,n). lenlist(l,n) :- lenlist(l,0,n). lenlist([],n,n). lenlist([_|t],p,n) :- p1 p+1 , lenlist(t,p1,n). queens(n,l) :- generate(l,n),lenlist(l,n), safe(l),!, labeling([ffc],l). notattack(x,xs) :- notattack(x,xs,1). notattack(x,[],n). notattack(x,[y|ys],n) :- x #\= y, x #\= y - n, x #\= y + n, n1 n + 1, notattack(x,ys,n1). safe([]). safe([f|t]) :- notattack(f,t), safe(t). then works, , produces instance:
?- queens(5,l). l = [1, 3, 5, 2, 4] ; l = [1, 4, 2, 5, 3] ; l = [2, 4, 1, 3, 5] ; l = [2, 5, 3, 1, 4] ; l = [3, 1, 4, 2, 5] ; l = [3, 5, 2, 4, 1] ; l = [4, 1, 3, 5, 2] ; l = [4, 2, 5, 3, 1] ; l = [5, 2, 4, 1, 3] ; l = [5, 3, 1, 4, 2].
Comments
Post a Comment