2012-08-07 145 views

回答

4

我相信binding宏觀平行不連續的。

參見:http://clojuredocs.org/clojure_core/clojure.core/binding

+0

更新了我的情況下,答案並不清楚我的意思 – Kevin 2012-08-07 20:09:42

+0

我更新的格式更多使其更清晰。 – 2012-08-07 20:10:48

+0

感謝(DEFN測試[] (結合[0 B(+ 1)] b))的 – locojay 2012-08-07 20:16:06

2

letfn是功能的並行結合的形式存在,讓人們編寫相互遞歸的本地功能。它並不像一般的,你尋求儘管它可以在緊要關頭使用。

user> (letfn [(a [] 4) 
       (b [] c) 
       (c [] a)] 
     (a)) 
4 

結合可以使用,只要你,如果你嘗試使用並行綁定的動態作用域會給不是讓不同的結果*分配在瓦爾值的東西,要動態作用域

user> (def ^:dynamic x 4) 
#'user/x 
user> (defn foo [] x) 
#'user/foo 
user> (binding [x 8] (+ x (foo))) 
16 ; the value of x that gets called by foo is modified by the caller 
    ; yielding 16 instead of 12 

user> (binding [x 8 a 7] (+ x (foo))) 
; Evaluation aborted. 
Unable to resolve var: a in this context 

將在方案

user> (def ^:dynamic a 2) 
#'user/a 
user> (binding [x 8 a 7] (+ x a)) 
15 
user> (binding [x 8 a (+ x 2)] (+ x a)) 
14 ; this would be 18 if the binding of x to 8 had been used 
    ; instead the root value of 4 was used. 

一般來說它是最常見的順序地結合或如果需要的話使用嵌套let秒。

0

binding不會給您與平行let相同的功能,因爲它取決於綁定的存在。如前所述letfn只要你不介意在功能包裝你的價值觀會工作。另一種解決方案是編寫一個並行使用宏讓:

(defmacro letp 
    [bindings & exprs] 
    (let [bindings (partition 2 bindings) 
     vars  (->> bindings (map first) vec) 
     values (->> bindings (map second))] 
    `((fn ~vars [email protected]) 
     [email protected]))) 

所以下面的成立:

(def a 7) 

(letp [a 5 b a] b) 
;;=> 7