gpt4 book ai didi

common-lisp - 在 Common LISP 中从结构本身访问结构字段

转载 作者:行者123 更新时间:2023-12-01 10:37:03 24 4
gpt4 key购买 nike

对于我的项目,我特别需要一个具有(除其他外)2 个插槽的结构:

  • 一个保存数据(当前状态,一个结构)
  • 一个持有一个函数(is-state-a-solution)

该函数槽必须评估当前状态并基于它返回结果。但是,我找不到如何正确执行此操作。这是我的一段代码。

(defstruct state moves-left)

(defstruct problem
(current-state)
(solution (function (lambda () (null (state-moves-left :current-state)))))
)

编译时没有错误,但是当我解释这个时它们发生了:

> (setq p0 (make-problem :current-state (make-state)))
> (funcall (problem-solution p0))

SYSTEM::%STRUCTURE-REF: :CURRENT-STATE is not a structure of type STATE

有人知道如何解决这个问题吗?我通常只使用常用函数,但这些结构和插槽是硬性要求。

编辑:感谢您的回答。在得知这是不可能的之后,我更彻底地重新阅读了要求并发布了答案 here .

最佳答案

你可以有一个单独的 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))

但是:为什么不直接使用函数/方法呢?

(defmethod problem-solution ((p problem))
(null (state-moves-left (problem-current-state p))))

关于common-lisp - 在 Common LISP 中从结构本身访问结构字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443993/

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