2013-01-07 133 views
1

我正在研究一个函数来计算棋类棋子的有效移动。功能white-pawn-move的作品。当我试图将其推广到任一玩家的兵(pawn-move)时,我遇到了一个非法的函数调用。我已经测试了repl中的funcalls,我不认为这是问题。lambda中的非法函数调用

我在做什么错?

http://pastebin.com/fEiQTwi5

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

然后问题可能是你的程序调用函数的方式。 – kmkaplan

+2

我不知道为什么问题已关闭。通常我会发现问题没关系。请再次添加问题。但是在这里添加源代码(不要链接到外部托管的代码)以及测试用例如何工作以及它是如何出错的。 –

+0

因为问题是“这个大型代码*有问题”,所以它已关闭。其他人不太可能从中受益。如果这个问题可以归结为一个有针对性的尖锐问题,那么这个问题就是主题。但就目前而言,这只是没有正确定义。 – corsiKa

回答

0

你能告诉你的错误?

我刚刚使用Emacs Lisp(将字符表示更改为Emacs-Lisp)尝试了您的代码,并且我没有为sexps (pawn-move #'+)(pawn-move #'-)发生错误。他们是否为你提出了一个错误?我得到这个为(pawn-move #'+),例如:

 
    (lambda (file rank) 
     (let ((movelist 'nil)) 
     (if (and (within-boardp 
        file 
        (funcall direction rank 1)) 
        (eql 115 
         (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)) 


总之,或许能给你看到更多的细节。

0

你的white-pawn-move返回一个惊涛骇浪,而你的pawn-move返回一个函数,可以返回一个惊涛骇浪。我猜你试图调用(pawn-move #'+),你以前调用(white-pawn-move),但你必须调用(funcall (pawn-move #'+))