2017-10-11 28 views
0

以下這個問題Clojure: Expand a var in let binding和溶液https://stackoverflow.com/a/20450289/1074389通過@amalloy 如果我能動態地將值傳遞給設形式我想知道提供乏Clojure的:展開設A變種結合動態值VAR

因此,與提出的這個代碼開始通過@ammlloy ...

(defmacro with-common [& body] 
`(let ~'[x 10, y 20] 
[email protected])) 

(with-common (+ x y)) 

我需要這個擴展的功能...

(defmacro with-common [x-val y-val & body] 
`(let ~'[x x-val, y y-val] 
[email protected])) 

(with-common 2 3 (+ x y)) ;; => 5 

預先感謝! 胡安

回答

1

我在想,如果我能動態值傳遞給讓利形式瓦爾

是,這種做法似乎做你想要什麼:

(defmacro with-common [x-val y-val & body] 
    (let [bindings ['x x-val 'y y-val]] 
    `(let ~bindings [email protected]))) 

(with-common 2 3 (+ x y)) 
;=> 5 
(macroexpand '(with-common 2 3 (+ x y))) 
;=> (let* [x 2 y 3] (+ x y))