(defun queens (n &optional (m n))
(if (zerop n)
(list nil)
(loop for solution in (queens (1- n) m) ;; <=== what does the (1- n) mean?
nconc (loop for new-col from 1 to m
when (loop for row from 1 to n
for col in solution
always (/= new-col col (+ col row) (- col row)))
collect (cons new-col solution)))))
(defun print-solution (solution)
(loop for queen-col in solution
do (loop for col from 1 to (length solution)
do (write-char (if (= col queen-col) #\Q #\.)))
(terpri))
(terpri))
(defun print-queens (n)
(mapC#'print-solution (queens n)))
嗨,有誰能向我解釋這個N皇后算法爲什麼會有(1 - n)? 「1-」部分和&可選語法是什麼?N-Queen Lisp(1- n)是什麼意思?
謝謝你,
[它只是從n中減去1](http://clhs.lisp.se/Body/f_1pl_1_.htm)。可選參數在'&optional'關鍵字之後。 –
,但是在lisp語言中肩是寫成( - n 1)?你介意多解釋一下嗎?我有點新來聆聽語言,謝謝 – theStress
顯然沒關係。我相信'1 +'和'1-'是特別的,儘管我不完全確定。 –