在返回向量的循环函数中对变量进行clojure赋值
我的问题是。。。更新currentRow nextRow bitPosition的值的好方法是什么 每次执行recur[currentRow nextRow bitPosition]时。现在,我正在与这样一个事实作斗争,即我不能在clojure做这样简单的事情。相反,我陷入了痛苦的世界,我甚至不知道如何在循环中将变量设置为新值 //我希望我能这么做在返回向量的循环函数中对变量进行clojure赋值,clojure,Clojure,我的问题是。。。更新currentRow nextRow bitPosition的值的好方法是什么 每次执行recur[currentRow nextRow bitPosition]时。现在,我正在与这样一个事实作斗争,即我不能在clojure做这样简单的事情。相反,我陷入了痛苦的世界,我甚至不知道如何在循环中将变量设置为新值 //我希望我能这么做 currentRow =(get myVector 0) //这是我的密码 (loop [myVector []] (let
currentRow =(get myVector 0)
//这是我的密码
(loop [myVector []]
(let [
rule ruleParam
currentRow currentRowParam
nextRow 2r0
bitPosition 2r0
]
(when (bit-test rule (bit-and currentRow 2r111))
(
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)
))
(when (= false (bit-test rule (bit-and currentRow 2r111)) )
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)
)
(recur [currentRow nextRow bitPosition]))
))
我问题的答案。谢谢你的指导
简而言之,你不能 局部变量仅在数学意义上是可变的:你可以考虑 当它们采用不同的值,但您不能为它们指定值时,所显示的内容。 函数不会改变局部变量。 例如,你有
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)
这些表达毫无作用。让我们看第一个
(bit-shift-right currentRow 1)
这将返回按1右移的currentRow位的值。它不会更改当前行。因为您没有对返回值做任何处理,所以它被遗忘了
要使用函数返回的值,可以重复使用循环,这只是一个可以重复使用的函数
如果我们相应地重铸您的代码,我们会得到如下结果
(loop [myVector []]
(loop [rule ruleParam
currentRow currentRowParam
nextRow 2r0
bitPosition 2r0]
(if (bit-test rule (bit-and currentRow 2r111))
(recur
rule
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition))
(recur
rule
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)))
(recur rule currentRow nextRow bitPosition)))
。。。如果省略了补充if条件
这仍然是胡说八道
循环中的所有最终表达式都是递归的,因此您永远不会
逃避它。
最后的复发是永远不会达到的,因为它上面的两个之一
法律总是颁布的。
你可能会发现更容易的问题有助于你掌握这门语言 简而言之,你不能 局部变量仅在数学意义上是可变的:你可以考虑 当它们采用不同的值,但您不能为它们指定值时,所显示的内容。 函数不会改变局部变量。 例如,你有
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)
这些表达毫无作用。让我们看第一个
(bit-shift-right currentRow 1)
这将返回按1右移的currentRow位的值。它不会更改当前行。因为您没有对返回值做任何处理,所以它被遗忘了
要使用函数返回的值,可以重复使用循环,这只是一个可以重复使用的函数
如果我们相应地重铸您的代码,我们会得到如下结果
(loop [myVector []]
(loop [rule ruleParam
currentRow currentRowParam
nextRow 2r0
bitPosition 2r0]
(if (bit-test rule (bit-and currentRow 2r111))
(recur
rule
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition))
(recur
rule
(bit-shift-right currentRow 1)
(bit-set nextRow 1)
(inc bitPosition)))
(recur rule currentRow nextRow bitPosition)))
。。。如果省略了补充if条件
这仍然是胡说八道
循环中的所有最终表达式都是递归的,因此您永远不会
逃避它。
最后的复发是永远不会达到的,因为它上面的两个之一
法律总是颁布的。
你可能会发现更容易的问题有助于你掌握这门语言 ruleParam和currentRowParam来自哪里?在循环中的let中绑定到它们是愚蠢的,它们在循环运行时无法更改。没关系,我从您最后的递归调用中发现了ruleParam和currentRowParam来自何处?在循环中的let中绑定它们是愚蠢的,它们在循环运行时无法更改。没关系,我从您最后的recur调用中发现了这一点