5

I can convert a string to a sequence, and then convert that sequence to a string representing the sequence.

user=> (str (first (list (seq "(xy)z"))))
"(\\( \\x \\y \\) \\z)"

I can also insert apply into the above form to get the original string back

user=> (apply str (first (list (seq "(xy)z"))))
"(xy)z"

but is there a way to convert a string representing a sequence, to the sequence that the string represents? such as:

"(\\( \\x \\y \\) \\z)"
user=> (some-fn2 "(\\( \\x \\y \\) \\z)")
(\( \x \y \) \z \))

1 Answer 1

8

The read-string function reads a string into a Clojure expression.

(read-string "(\\( \\x \\y \\) \\z)")
(\( \x \y \) \z)  

The read family of functions are a big part of what makes Clojure a lisp and the whole "everything is data" mindset. You can read any form with them:

(read-string "{:a 1 :b 3 :c (1 2 3)}")
{:a 1, :b 3, :c (1 2 3)}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the fast answer. I feel like a guy who was given a fish, but knows he needs to learn to fish. I am still struggling with how to find the clojure functions I know must exist by searching various clojure resources. read-string was nowhere on my radar.

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.