我在Common Lisp中遇到了一个小问题.我需要编写一个函数,它将一个符号作为参数(即一个函数的名称或一个变量的名称),然后执行其他操作,然后打印该符号而不进行评估.引用在这种情况下不起作用,所以我该怎么做?例如,假设我定义var为整数3用defparameter,那么函数应该有这样的行为:
(my-function var)
REPL: var is a symbol and its value is 3.
Run Code Online (Sandbox Code Playgroud)
我怎么打印var?我怎样才能准确地看到varREPL,或者我给输入变量的任何其他名称my-function?有人能帮我吗?
您正在寻找的功能不可能与函数一起使用,因为它们可能已被编译为参数只是在堆栈上传递而代码查看内存索引而不是变量.
您正在寻找某种元编程,这就是宏的用途:
(defmacro info (expr)
(let ((compile-type (type-of expr)) (tmp (gensym "tmp")))
`(let ((,tmp ,expr))
(format t "REPL: ~s is a ~a and it's value is ~s~%" ',expr ',compile-type ,tmp)
,tmp)))
(info "test")
; REPL: "test" is a (simple-base-string 4) and it's value is "test"
; ==> "test"
(info *print-circle*)
; REPL: *print-circle* is a symbol and it's value is nil
; ==> nil
(info #'+)
; REPL: #'+ is a cons and it's value is #<system-function +>
; ==> #<system-function +>
(info (+ 3 4))
; REPL: (+ 3 4) is a cons and it's value is 7
; ==> 7
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
264 次 |
| 最近记录: |