套入让圣公会Clojure

时间:2018-09-23 18:55:08

标签: clojure

我是Clojure概率编程语言Anglican的新手。我试图在Clojure中使用嵌套的let结构。

以下defquery运行没有任何问题。

(defquery panda3 [p1]
  (let [p2 (sample 
              (categorical
                {:speA (/ 1 2),
                 :speB (/ 1 2)}))]
    (if (= p2 :speA)
      ( let [p3 (sample 
              (categorical
                {:twins (/ 1 10),
                 :notwins (/ 9 10)}))] 

      )
      ( let [p3 (sample 
              (categorical
                {:twins (/ 2 10),
                 :notwins (/ 8 10)}))]

      ))

    p2))

但是,如果我尝试返回p2的值,而不是最后返回p3的值,它将返回错误。

(defquery panda3 [p1]
  (let [p2 (sample 
              (categorical
                {:speA (/ 1 2),
                 :speB (/ 1 2)}))]
    (if (= p2 :speA)
      ( let [p3 (sample 
              (categorical
                {:twins (/ 1 10),
                 :notwins (/ 9 10)}))] 

      )
      ( let [p3 (sample 
              (categorical
                {:twins (/ 2 10),
                 :notwins (/ 8 10)}))]

      ))

    p3))

这个想法是根据p2的结果分配p3。但是,我无法这样做。我究竟做错了什么?

预先感谢

1 个答案:

答案 0 :(得分:2)

正如评论所说,您需要在定义的threading.Lock范围内返回p3

let

更新

就像合金一样,第二部分可能是:

(defquery panda3 [p1]
  (let [p2 (sample 
              (categorical
                {:speA (/ 1 2),
                 :speB (/ 1 2)}))]
    (if (= p2 :speA)
      (let [p3 (sample 
                 (categorical
                   {:twins (/ 1 10),
                    :notwins (/ 9 10)}))] 
        p3)
      (let [p3 (sample 
                 (categorical
                   {:twins (/ 2 10),
                    :notwins (/ 8 10)}))]
        p3 ))))

甚至

  ; return the result of `(sample (categorical ...))` called
  ; with one of the 2 maps
  (if (= p2 :speA)
    (sample 
      (categorical
        {:twins (/ 1 10),
         :notwins (/ 9 10)} ))
    (sample 
      (categorical
        {:twins (/ 2 10),
         :notwins (/ 8 10)} )))