Clojure函数用于打印符号名称和符号值

use*_*571 5 clojure

我一直在努力寻找答案或制定解决方案.我试图找出如何在Clojure中创建代码的代码.对于我的第一个专长,我想要一个函数,它将打印到stdout符号的名称及其值,对调试很有用.例:

(def mysymbol 5)
(debugging-function mysymbol)

mysymbol: 5
Run Code Online (Sandbox Code Playgroud)

那有意义吗?谢谢你的帮助.

发布讨论更新

以下是@amalloy的答案:

(defmacro ?
"A useful debugging tool when you can't figure out what's going on:
wrap a form with ?, and the form will be printed alongside
its result. The result will still be passed along."
[val]
`(let [x# ~val]
    (prn '~val '~'is x#)
    x#))
Run Code Online (Sandbox Code Playgroud)

所以: (? myvariable)

ama*_*loy 6

你可以看到我在github上写的这个简单版本.重点是你不能用一个函数来做这个,但是用宏来做它很简单 - 你只需要引用和取消引用即可.