N-Queen Lisp(1- n)是什么意思?
N-Queen Lisp (1- n) mean?
(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-Queen 算法为什么会有 (1- n)? “1-”部分和 &optional 语法是什么?
谢谢,
1-
是一个从参数中减一的函数。
&optional
表示可选参数将跟在后面。通常默认值为 nil
,但如果您将其指定为列表 - 即 (m n)
,则参数默认为列表中的第二个值(在这种情况下,如果第二个参数为不及格)。
(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-Queen 算法为什么会有 (1- n)? “1-”部分和 &optional 语法是什么?
谢谢,
1-
是一个从参数中减一的函数。
&optional
表示可选参数将跟在后面。通常默认值为 nil
,但如果您将其指定为列表 - 即 (m n)
,则参数默认为列表中的第二个值(在这种情况下,如果第二个参数为不及格)。