33

How would I get something similar to the following?:

(evaluate-text "(+ 1 2)")  ; resolves to 3
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Nick Orton
  • 3,563
  • 2
  • 21
  • 28

3 Answers3

33
(load-string "(+ 1 2)")
Abhijith
  • 929
  • 8
  • 9
31
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
  • 71,150
  • 28
  • 166
  • 168
1

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

(eval (list + 1 2)) #=> 3
David Seiler
  • 9,557
  • 2
  • 31
  • 39