gpt4 book ai didi

lambda - lambda 内部的非法函数调用

转载 作者:太空宇宙 更新时间:2023-11-03 18:55:29 30 4
gpt4 key购买 nike

我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。 white-pawn-move 函数有效。当我试图将其概括为任一玩家的棋子 (pawn-move) 时,我遇到了非法函数调用。我已经在 repl 中测试了函数调用,但我认为这不是问题所在。

我做错了什么?

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 #'-))

最佳答案

你能展示你得到的错误吗?

我刚刚使用 Emacs Lisp 尝试了您的代码(将字符表示更改为 Emacs-Lisp),并且我没有收到 sexps (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))

总而言之,也许可以更详细地说明您所看到的内容。

关于lambda - lambda 内部的非法函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14203604/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com