如何在Common Lisp中返回变量的值和名称

时间:2015-02-17 02:56:30

标签: lisp common-lisp

我正在尝试编写一个宏来返回变量的名称和值在常见的lisp中。如何在LISP宏中返回变量的名称和值?

类似

(RETURNVAL(x))

将返回

x的值为5

1 个答案:

答案 0 :(得分:2)

(defmacro returnval (x)
  `(format t "~a has value ~a." ',x ,x))

CL-USER> (defparameter *forty-two* 42)
*FORTY-TWO*
CL-USER> (returnval *forty-two*)
*FORTY-TWO* has value 42.
NIL
CL-USER> (let ((x 5))
           (returnval x))
X has value 5.
NIL

如果您真的想在表单周围添加一组额外的parens,那么您也可以这样做:

(defmacro returnval ((x))
  `(format t "~a has value ~a." ',x ,x))

CL-USER> (returnval (*forty-two*))
*FORTY-TWO* has value 42.
NIL
CL-USER> (let ((x 5))
           (returnval (x)))
X has value 5.
NIL