I'm working on an iterative solution to the n queens problem. I've decided to represent the state space as an array of 0's and 1's, such that a 1 represents the presence of a queen. The plan is to generate all permutations of the array and then write a verifier to prune incorrect solutions. I'm trying to do this is common lisp, even though I had never touched functional programming before today.
In order to generate the permutations, I chose to try and implement this algorithm, using the first pseudocode example: http://www.quickperm.org/
Here is my attempt:
(defun permute (n)
(setf total (* n n))
(let ((a (make-array total :initial-element 0)) ;list of objects to permute
(p (make-array total :initial-element 0))) ;array to control iteration
(dotimes (i n) (setf (aref a i) 1))
(loop for index from 1 while (< index total) do
(setf (aref p index) (- (aref p index) 1)) ;decrement p[i] by 1
(if (= (rem index 2) 1) ;if index is odd
(setf j (aref p index)) ;j = p[index]
(setf j 0)) ;else j = 0
(rotatef (aref a index) (aref a j)) ;swap a[index] & a[j]
(setf index 1) ;index = 1
(loop while (= (aref p index) 0) do ;while p[index] == 0
(setf (aref p index) index) ;p[index] = i
(setf index (+ index 1))) ;index++
print a)))
(permute 4)
Currently, I'm getting the error: AREF: index -1 for #array
, which seems to be caused by the (setf (aref p index) (- (aref p index) 1))
line. In pseudocode, that line seems to implement p[index] = p[index] - 1
. This is the only subtraction operation I have, but it shouldn't be operating on index itself, just on the value at its location.
What am I missing?
EDIT: I initialized every element of p to 0. Each element is actually supposed to be equal to its index. Will post updated code when completed.