0

I'm using this code to solve eight queen problem but I have doubt on del predicate. Is there anybody that can help me to understand how it works?

code here:

solution(Queens):- 
  permutation([1,2,3,4,5,6,7,8],Queens), 
  safe(Queens).

permutation([],[]).    
permutation([Head|Tail],PermList):- 
 permutation(Tail,PermTail), 
 del(Head,PermList,PermTail).

del(A,[A|List],List).
del(A,[B|List],[B|List1]):- 
 del(A,List,List1).

safe([]).
safe([Queen|Others]):- 
 safe(Others), 
 noattack(Queen,Others,1).

noattack(_,[],_).
noattack(Y,[Y1|Ylist],Xdist):- 
 Y1-Y=\=Xdist,
 Y-Y1 =\= Xdist,
 Dist1 is Xdist +1, 
 noattack(Y,Ylist,Dist1).
false
  • 10,264
  • 13
  • 101
  • 209
gio
  • 55
  • 8
  • 1
    `Is there anybody that can help me to understand how it works?` Sure many of us can. I did it once for another N Queens [question](https://stackoverflow.com/a/53412988/1243762). Don't plan on doing it again, but some of the methods I used would equally apply here. Here is a hint, this is a generate and test solution. – Guy Coder Jul 21 '20 at 19:13
  • If you are thanking me for the other answer, then why no up-vote on the other answer? – Guy Coder Jul 22 '20 at 13:38

1 Answers1

0

Regarding the del predicate, I tried to explain it over here: Prolog: How does delete/3 works?

Isabelle Newbie
  • 9,258
  • 1
  • 20
  • 32