对于我的项目,我特别需要一个具有(除其他外)2 个插槽的结构:
该函数槽必须评估当前状态并基于它返回结果。但是,我找不到如何正确执行此操作。这是我的一段代码。
(defstruct state moves-left)
(defstruct problem
(current-state)
(solution (function (lambda () (null (state-moves-left :current-state)))))
)
Run Code Online (Sandbox Code Playgroud)
编译时没有错误,但是当我解释这个时它们会发生:
> (setq p0 (make-problem :current-state (make-state)))
> (funcall (problem-solution p0))
SYSTEM::%STRUCTURE-REF: :CURRENT-STATE is not a structure of type STATE
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?我通常只是使用常用功能,但这些结构和插槽是硬性要求。
编辑:感谢您的回答。在了解到这是不可能的后,我更彻底地重新阅读了要求并在此处发布了答案。
你可以有一个单独的create函数:
(defun create-problem (state)
(let ((problem (make-problem :current-state state)))
(setf (problem-solution problem)
(lambda ()
(null (state-moves-left (problem-current-state problem)))))
problem))
Run Code Online (Sandbox Code Playgroud)
但是:为什么不直接使用函数/方法呢?
(defmethod problem-solution ((p problem))
(null (state-moves-left (problem-current-state p))))
Run Code Online (Sandbox Code Playgroud)