gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:59:20 25 4
gpt4 key购买 nike

我一直在努力寻找答案或制定解决方案。我想弄清楚如何编写在 Clojure 中编写代码的代码。对于我的第一个壮举,我想要一个函数,它将符号的名称及其值打印到标准输出,对调试很有用。例子:

(def mysymbol 5)
(debugging-function mysymbol)

mysymbol: 5

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

发布讨论更新

这是@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#))

所以: (? myvariable)

最佳答案

你可以看到我写的一个简单版本 on github .主要的一点是你不能用一个函数来做到这一点,但是用一个宏就足够简单了——你只需要正确地引用和取消引用。

关于Clojure 函数打印符号名称和符号值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com