Scheme (Guile) Source Code
; eight-queens
; Author: Hannes du Plooy
; Date: 20 Sep 2016
; Objective: To solve the following
; Search for a chess board solution with 8 queens and no one
; endangering any other.
(define board (make-vector 8 0))
(define (EightQueens line)
(if (> line 7)
#t
(let ((done #f))
(do ((column 0 (1+ column)))
((or done (> column 7)))
(let ((endangered #f))
(do ((line2 0 (1+ line2)))
((or endangered (>= line2 line)))
(if (or (= (vector-ref board line2) column)
(= (abs (- line line2)) (abs (- column (vector-ref board line2)))))
(set! endangered #t)))
(when (not endangered)
(vector-set! board line column)
(if (EightQueens (1+ line))
(set! done #t)))))
done)))
(if (EightQueens 0)
(do ((i 0 (1+ i)))
((>= i 8))
(do ((j 0 (1+ j)))
((>= j 8))
(if (= (vector-ref board i) j)
(display "Q")
(display ".")))
(newline))
"No Solution")