Problem description:
Problem is often generalized to N queens – place N queens on an N × N
board so that no two attack each other (problem has no solution for N < 4). Any solution to the N queens problem will have a queen in each row and a queen in each column. I will use prolog to explain this problem, solutions can be specified as a permutation of the list of numbers 1 to N. The first element in the list is the row number of the queen in the first column, second element in the list is the row number of the queen in the second column and so on …
Prolog code:


The function range
is to generate a list from 1 to N, for example, range(1, 5, Ns), the Ns will be [1,2,3,4,5]. That provides the list of rows we can place our queens. The function attack
considers only diagonals, because if one row has placed a queen, this row will be removed from our list, so the Qs only contains the row which the queens can be placed. Because we just place the queen in one column each time, so in the same column, we will never place two queens. So we only need to consider the diagonals. And for attack(X,N,[Y_]) : X
is Y + N. considers the positive diagonals, the attack(X,N,[Y_]) : X
is Y  N. considers the negative diagonals. 🙂