Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure function to print symbol name and symbol value

Tags:

clojure

I have been struggling to find an answer or develop a solution. I am trying to figure out how to make code that makes code in Clojure. For my first feat, I want a function that will print to stdout the name of the symbol and its value, useful for debugging. Example:

(def mysymbol 5)
(debugging-function mysymbol)

mysymbol: 5

Does that make sense? Thanks for your help.

Post Discussion Update

Here is the answer from @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#))

So: (? myvariable)

like image 911
user1062571 Avatar asked Nov 23 '11 19:11

user1062571


1 Answers

You can see a simple version of this that I wrote on github. The main point is that you can't do this with a function, but with a macro it's simple enough - you just have to get your quoting and unquoting right.

like image 78
amalloy Avatar answered Oct 22 '22 04:10

amalloy