[英]Nested Let in Anglican Clojure
I am new to Anglican, the Probabilistic programming language in Clojure. 我是Clojure中的概率编程语言Anglican的新手。 I am trying to use a nested let construct in Clojure. 我试图在Clojure中使用嵌套的let结构。
The following defquery
runs without any issues. 以下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))
However, instead of returning the value of p2
at the end, if I try to return the value of p3
, it returns errors. 但是,如果我尝试返回p3
的值,则不会返回最后的p2
值,而是返回错误。
(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))
The idea is to assign p3 based on the outcome of p2. 想法是根据p2的结果分配p3。 However, I am unable to do so. 但是,我无法这样做。 What am I doing wrong? 我究竟做错了什么?
Thanks in advance, 提前致谢,
As the comment said, you need to return p3
from within the scope of the let
in which it is defined: 如评论所述,您需要在定义它的let
范围内返回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)}))]
p3)
(let [p3 (sample
(categorical
{:twins (/ 2 10),
:notwins (/ 8 10)}))]
p3 ))))
Update 更新资料
As amalloy points out, the 2nd part could be: 就像合金一样,第二部分可能是:
; 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)} )))
or even 甚至
; return the result of `(sample (categorical ...))` called
; with one of the 2 maps
(sample
(categorical
(if (= p2 :speA)
{:twins (/ 1 10),
:notwins (/ 9 10)}
{:twins (/ 2 10),
:notwins (/ 8 10)} )))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.