常见的 lisp subst 变体,可以遍历 defstructs

问题描述 投票:0回答:1

我正在使用 SBCL,并且我不介意特定于编译器的解决方案。我的表单涉及一些结构,我想遍历并修改一些子表单,但是

subst
看不到结构后面:

CL-USER> (defstruct my-struct slot-1 slot-2)
MY-STRUCT
CL-USER> (make-my-struct :slot-1 'a :slot-2 'b)
#S(MY-STRUCT :SLOT-1 A :SLOT-2 B)
CL-USER> (subst 'b1 'b (make-my-struct :slot-1 'a :slot-2 'b))
#S(MY-STRUCT :SLOT-1 A :SLOT-2 B)

有没有办法制作一个可以遍历所有 Lisp 原生结构的通用

subst

structure common-lisp traversal
1个回答
0
投票

对于 sbcl,类似的东西可能有效。

(use-package :sb-mop)

(defun search-and-replace (obj slot-old-value slot-new-value)
  (let ((slots (mapcar #'slot-definition-name (class-direct-slots (class-of obj)))))
    (dolist (s slots obj)
      (when (eql slot-old-value (slot-value obj s))
        (setf (slot-value obj s) slot-new-value)))))
© www.soinside.com 2019 - 2024. All rights reserved.