【发布时间】:2019-06-26 10:41:11
【问题描述】:
在学习 CL 并在不同的小项目中作为业余爱好练习了一段时间后,我的个人 CL 图上仍有一些空白区域。最近,我有很多函数都使用相同的let 构造,我想写一个宏来让代码更简洁:
(defmacro with-context (&body body)
`(let ((context (make-array 4 :element-type 'fixnum
:initial-contents '(0 1 2 3))))
,@body))
以便我以后可以定义如下函数(仅作为一个最小示例):
(defun test-a ()
(with-context
(setf (aref context 3)
(+ (aref context 0) (aref context 1)))
context))
现在我想知道是否可以使用像 (context n) 这样的宏/函数来缩短 (aref context n) 表达式。
(defun context (n)
(aref context n))
当然,变量context 在编译时是未知的。我只是不知道我在这里是否有一个基本的误解,或者我如何告诉 lisp 我真正想要什么。所以,我的问题基本上是这是否可能,是否是个好主意。
【问题讨论】:
标签: scope macros common-lisp