我正在尝试创建一个可以在LISP中解决Mastermind游戏的玩家。我尝试在辅助函数中使用以下嵌套循环
(defparameter *counter* 0)
;analyze the score to determine whether points need to be added to counter
(defun analyze (last-response)
(if (> (first last-response) 0)
(setf *counter* (+ (first last-response) *counter*))))
;helper function for finding correct color
(defun guessColor (length colors last-response)
(loop while (< *counter* 4) do
(loop for i from 1 to length
collect (first colors)
(setf colors (rest colors)))
(analyze (last-reponse))))
;baseline Holyguacamole player will guess all colors before trying all combinations of correct color
(defun HolyGuacamole (board colors SCSA last-response)
(declare (ignore SCSA))
;(print last-response)
(guessColor board colors last-response)
(print *counter*)
;(sortColor)
)
当全局变量*counter*
小于4时,应该运行while循环。内部循环应该根据所需的挂钩长度(可变长度)猜测颜色。我一直在遇到编译错误
在宏扩展期间(LOOP WHILE(&lt; COUNTER 4)...)。使用;
BREAK-ON-SIGNALS 拦截。
我不熟悉LISP所以我不确定错误代码是什么意思以及如何解决它。我觉得我用正确的括号正确地嵌套它,但我真的不确定它有什么问题。
Link到Mastermind环境。
答案 0 :(得分:1)
原则上没有障碍将循环嵌套在另一个循环中。但是,正如@jkiiski指出的那样,COLLECT
或COLLECTING
子句只能采用一个表达式。
例如,以下程序
(defun nested-loop ()
(loop for i from 1 to 10 doing
(loop for j from 1 to 10 collecting
(print "some string")
(print (list 'nested-loop i j)))))
(nested-loop)
在CLISP下产生语法错误。
*** - LOOP: illegal syntax near (PRINT (LIST 'NESTED-LOOP I J)) in
(LOOP FOR J FROM 1 TO 10 COLLECTING (PRINT "some string")
(PRINT (LIST 'NESTED-LOOP I J)))
使用do
或doing
子句可以正常工作,将collecting
子句与progn
子句关联的多个表达式分组为(defun nested-loop ()
(loop for i from 1 to 10 doing
(loop for j from 1 to 10 collecting
(progn
(print "some string")
(print (list 'nested-loop i j))))))
(nested-loop)
。
min_score