2016-11-30 55 views
1

我试图创建一个可以解决LISP游戏策略的玩家。我尝试使用以下嵌套循环在一个辅助函数共同嵌套循环LISP

(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) 
) 

while循环应该运行,而全局变量*counter*是小于4内部循环被认为基于钉所需要的长度来猜测颜色(可变长度)。中宏扩展期间,我一直运行到编译错误

(循环while(< COUNTER 4)...)。使用 ;
BREAK-ON-SIGNALS要拦截。

我不熟悉LISP,所以我不确定那个错误代码的含义以及如何解决它。我觉得我用正确的括号正确地嵌套了它,但我并不确定它有什么问题。

Link to Mastermind environment。

+0

整个程序有多大?你可以粘贴它,也可以把它缩小为一个独立的例子吗?我没有看到“分数”或“分析”的定义或者“*计数器*”的声明# –

+0

@GregoryNisbet游戏本身的代码非常大。我编辑了我的帮助函数以及* counter *的声明。分数改为最后回应 – trungnt

+0

我认为'(analyze(last-reponse))))'应该是'(分析last-reponse)))'......似乎还有其他一些错误......我避难无法用'clisp'或'sbcl'重现那个确切的编译错误。 –

回答

1

原则上在另一个循环内嵌套一个循环没有障碍。但是,@ jkiiski指出,COLLECTCOLLECTING子句只能采用单个表达式。

例如,下面的程序

(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))) 

使用dodoing条款作品,并分组与progn一个collecting条款相关的多个表达式。

(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) 
+0

感谢您的回复。经过进一步的思考,我不认为使用嵌套循环会以我想要的方式工作。我已经在下面重写了我的代码。它编译,但是当用print最后一次响应测试时,它会打印所有的NIL – trungnt