Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eval a list into a let on clojure

My problem is the next, i try to evaluate a list with some vars using a let to asign values to this vars

if i do (def a (list * 'x 'y)) and (let [x 3 y 3] (eval a)) I have a CompilerException java.lang.RuntimeException: Unable to resolve symbol: x in this context, compiling:(NO_SOURCE_PATH:6)

but if I run (def x 4) (def y 4) and (eval a) i have a 16, anyway if I run again (let [x 3 y 3] (eval a)) again I have 16,

exist a method to binding the x and y correctly and eval the list?

ty!

like image 784
patz Avatar asked Dec 28 '22 08:12

patz


2 Answers

let defines lexically scoped bindings that are not accessible from the body of the eval function. This is no different than any other function. However, the bindings created with def are accessible because they are namespace global. All functions have access to namespace global variables, as long as they are public.

like image 188
fogus Avatar answered Jan 11 '23 19:01

fogus


(def ^:dynamic x 4) (def ^:dynamic y 4)
user=> (binding [x 3 y 3] (eval a))
9
user=> (eval a)
16
like image 40
BLUEPIXY Avatar answered Jan 11 '23 21:01

BLUEPIXY