3

Is there a way to convert a Clojure form to a string? E.g. convert:

(and (f 1) (g 3))

to:

"(and (f 1) (g 3))"

3 Answers 3

8
=> (defmacro string-it [x] (str x))
#'user/string-it
=> (string-it (+ 1 2))
"(+ 1 2)"
Sign up to request clarification or add additional context in comments.

Comments

3

Alternativly if you don't know the form before hand you can do,

(defmacro to-str [f]
  (str f))

(to-str (and (f 1) (g 3)))

and get,

"(and (f 1) (g 3))"

Comments

3

You can do this:

(str '(and (f 1) (g 3)))

EDIT

In case you're not familiar with it, the ' ("quote") character is a reader macro character (more) that escapes code -- i.e. keeps it from being evaluated.

You could also set a variable:

(def x '(and (f 1) (g 3)))
(str x)

and then if you wanted to run the code you could eval it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.