Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是 Lisp 的新手,正在尝试不同的方法来提高我的技能。我想编写一个包装现有函数的宏,以便我可以为这些函数设置表单之前和之后,有点像 CLOS 的辅助方法或 Elisp 的建议包。跟踪函数动态包装代码的能力引起了我的兴趣,我自己能够做到这一点似乎很有用。
我怎样才能做到这一点?
请注意,我正在使用 SBCL,并且出于这个问题的目的,我对这样做的“正确”方式不太感兴趣,因为我正在添加到我的 Lisp 技巧包中。
我不知道在 CLOS 之外对此有任何内置支持。但是您可以重新定义原始函数,如下所示:
(defmacro add-post (fun-name &body body) (let ((orig (gensym))) `(let ((,orig (fdefinition ,fun-name))) (setf (fdefinition ,fun-name) (lambda (&rest args) (apply ,orig args) ,@body)))))