views:

426

answers:

3

How would I get something similar to the following?:

(evaluate-text "(+ 1 2)")  ; resolves to 3
A: 

How similar does it have to be? Clojure's eval works on lists, so:

(eval (list + 1 2)) #=> 3
David Seiler
I know about eval, but I don't think it works for me. I really want to evaluate a "String"
Nick Orton
+16  A: 
user> (eval (read-string "(+ 1 2)"))
3

You probably shouldn't ever need to do this. Macros and fns make this kind of thing unnecessary 99% of the time. This is quite brittle, and can be unsafe if these strings are coming from user input, and so on.

Brian Carper
Well I want to do something radically unsafe: a REPL chatbot.
Nick Orton
There is a clojurebot in #clojure.
JH
load-string does exactly what he is looking for
Abhijith
+4  A: 
(load-string "(+ 1 2)")
Abhijith