我正在研究一个函数来计算棋类棋子的有效移动。功能white-pawn-move
的作品。当我试图将其推广到任一玩家的兵(pawn-move
)时,我遇到了一个非法的函数调用。我已经测试了repl中的funcalls,我不认为这是问题。lambda中的非法函数调用
我在做什么错?
(defun white-pawn-move (file rank)
(let ((movelist '()))
(if (and (within-boardp file (+ rank 1))
(eql #\s (aref *board* (+ rank 1) file)))
(push (cons file (+ rank 1)) movelist))
(if (= rank 1)
(push (cons file (+ rank 2)) movelist))
(if (and (within-boardp (- file 1) (+ rank 1))
(belongs-to-opponent (aref *board* (+ rank 1) (- file 1))))
(push (cons (- file 1) (+ rank 1)) movelist))
(if (and (within-boardp (+ file 1) (+ rank 1))
(belongs-to-opponent (aref *board* (+ rank 1) (+ file 1))))
(push (cons (+ file 1) (+ rank 1)) movelist))
movelist))
;refactor:
;file/rank numeric
(defun pawn-move (direction)
(let ((startrank (if (eql direction #'+)
1
6)))
(lambda (file rank)
(let ((movelist '()))
(if (and (within-boardp file (funcall direction rank 1))
(eql #\s (aref *board* (funcall direction rank 1) file)))
(push (cons file (funcall direction rank 1)) movelist))
(if (= rank startrank)
(push (cons file (funcall direction rank 2)) movelist))
(if (and (within-boardp (- file 1) (funcall direction rank 1))
(belongs-to-opponent (aref *board*
(funcall direction rank 1)
(- file 1))))
(push (cons (- file 1) (funcall direction rank 1)) movelist))
(if (and (within-boardp (+ file 1) (funcall direction rank 1))
(belongs-to-opponent (aref *board*
(funcall direction rank 1)
(+ file 1))))
(push (cons (+ file 1) (funcall direction rank 1)) movelist))
movelist))))
;desired usage
(setf (gethash #\P *move-table*) (pawn-move #'+))
(setf (gethash #\p *move-table*) (pawn-move #'-))
然后问题可能是你的程序调用函数的方式。 – kmkaplan
我不知道为什么问题已关闭。通常我会发现问题没关系。请再次添加问题。但是在这里添加源代码(不要链接到外部托管的代码)以及测试用例如何工作以及它是如何出错的。 –
因为问题是“这个大型代码*有问题”,所以它已关闭。其他人不太可能从中受益。如果这个问题可以归结为一个有针对性的尖锐问题,那么这个问题就是主题。但就目前而言,这只是没有正确定义。 – corsiKa