% much faster solution, uses one built-in append/3 for lists

%coordinates

numberList([1,2,3,4,5,6,7,8]).
letterList([a,b,c,d,e,f,g,h]).

% numbers corresponding to the letters

c(a,1).
c(b,2).
c(c,3).
c(d,4).
c(e,5).
c(f,6).
c(g,7).
c(h,8).

% initializes L and N, computes a combination and start calculating positions

queens(ResultList):-letterList(L),numberList(N),pos(L,N,[],ResultList).


% calculates the positions of the queens and immediately checks consistency

pos([],[],R,R).
pos(LList,NList,OldList,RList):-member_del(LList,L,LRest),
                                member_del(NList,N,NRest),
                                comp([L,N],OldList),
                                append(OldList,[[L,N]],NewList),
                                pos(LRest,NRest,NewList,RList).

% selects a member from the list and remove this element

member_del([L|R],L,R).
member_del([A|R],L,[A|R1]):-A\==L,member_del(R,L,R1).

% calculating for one queen whether she attacks any of the others

comp(_,[]).
comp([L,N],[[L1,N1]|Rest]):-c(L,LN),c(L1,LN1),Z1 is LN-LN1,Z2 is N-N1,
                            abs(Z1,A1),abs(Z2,A2),A1\==A2,
                            comp([L,N],Rest).

