Eval list in let on clojure

My problem is the following, I'm trying to evaluate a list with some vars using let to asign characters for these vars

If I do (def a (list * 'x 'y)) and (let [x 3 y 3] (eval a)) , I have a CompilerException java.lang.RuntimeException: cannot resolve character: x in this context, compilation: (NO_SOURCE_PATH: 6)

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

is there a method to bind x and y correctly and an eval list?

ty!

+4
source share
3 answers

Well, you can also eval let expression, see if you need it:

 (eval '(let [x 3 y 3] (* xy))) 

EDIT:

According to the comments, this will work for your case:

 (def a (list (list * 'x 'y))) (eval (concat '(let [x 3 y 3]) a)) 

Better yet, use quasicotation:

 (def a (list * 'x 'y)) (eval `(let ~'[x 3 y 3] ~a)) 
+2
source

let defines lexically-bound bindings that are not available from the body of the eval function. This is no different from any other function. However, bindings created with def are available because they are global namespaces. All functions have access to global namespace variables if they are publicly available.

+5
source
 (def ^:dynamic x 4) (def ^:dynamic y 4) user=> (binding [x 3 y 3] (eval a)) 9 user=> (eval a) 16 
+3
source

Source: https://habr.com/ru/post/1269794/


All Articles