关注@amalloy提供的此问题Clojure: Expand a var in let binding和解决方案https://stackoverflow.com/a/20450289/1074389 我想知道我是否可以动态地将值传递给let form vars
首先从@ammlloy提出的代码开始......
(defmacro with-common [& body]
`(let ~'[x 10, y 20]
~@body))
(with-common (+ x y))
我需要这个功能的扩展...
(defmacro with-common [x-val y-val & body]
`(let ~'[x x-val, y y-val]
~@body))
(with-common 2 3 (+ x y)) ;; => 5
提前致谢! 涓
答案 0 :(得分:1)
我想知道我是否可以动态地将值传递给let form vars
是的,这种方法似乎可以做你想要的:
(defmacro with-common [x-val y-val & body]
(let [bindings ['x x-val 'y y-val]]
`(let ~bindings ~@body)))
(with-common 2 3 (+ x y))
;=> 5
(macroexpand '(with-common 2 3 (+ x y)))
;=> (let* [x 2 y 3] (+ x y))