lisp clos 访问器问题

标签 lisp common-lisp sbcl clos

当类在列表中时,我无法使用 clos 访问器函数。

假设我有 a 类:

(defclass a ()
  ((a :accessor a
      :initarg :a)))

我制作了 2 个实例:

(defparameter b (make-instance 'a :a 1))
(defparameter c (make-instance 'a :a 2))

然后如果我想创建一个函数来获取列表中每个实例的值

(defun get-a (lst)
  (mapcar #'a lst))

并调用它

(get-a '(b c))

但我这样做时出现错误:

There is no applicable method for the generic function
  #<STANDARD-GENERIC-FUNCTION A (1)>
when called with arguments
  (B).
    [Condition of type SIMPLE-ERROR]

如果我没有直接用 mapcar 调用访问器,而是调用一个包含访问器的函数,也会发生这种情况。我也尝试过使用循环和其他东西来代替 mapcar。

谢谢

最佳答案

如果您阅读错误,就会得到解释。

There is no applicable method for the generic function
  #<STANDARD-GENERIC-FUNCTION A (1)>
when called with arguments
  (B).

所以你接到了一个电话,类似于(a 'b)。但是 b 是一个符号,而不是 CLOS 实例。

(b c) 是两个符号的列表。您可能想创建一个包含两个 CLOS 实例的列表。使用 LIST 创建一个包含评估参数的列表。

关于lisp clos 访问器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22207932/

相关文章:

lisp - 回到上一个状态

loops - LISP 遍历列表

lisp - Unreachable Ill-formed if-expression 在 Scheme 中是语法错误,但在 Common Lisp 中不是

macros - &allow-other-keys ,它有什么作用? - 普通口齿不清

lisp - 常见的 lisp,未定义的交集函数行为?

recursion - 用于检查列表中的值的 Lisp 递归函数不起作用

common-lisp - Common Lisp CFFI 和 C 退出代码

common-lisp - nodgui 和 SBCL(?) : How to avoid the type error "The value NIL is not of type STREAM when binding STREAM" right from the beginning?

scheme - 截断(受限) Racket 中的列表

stream - 读取字节忽略 SBCL 灰色流中的 eof-error-p