我在Clojure循环中的尾部位置在哪里? [英] Where is the tail position in my Clojure loop?
问题描述
Clojure说我不能从非尾部位置调用 recur
。
这不是尾部位置?
是什么?
(loop [i 20]
(for [x(range 1 21)]
(if(zero?(rem ix))
i
(recur(+ i 1)))))
对于
不会做你认为它做的;它不是一个命令循环。它是一个列表理解,或序列生成器。因此,在它的结尾没有返回或迭代调用,所以你不能把 recur
放在那里。
看起来你可能根本不需要这个表达式中的 loop
或 recur
是 c>所有你需要构建一个序列,但我不知道你想要建立什么序列。
Clojure is saying that I can't call recur
from a non-tail-position.
Is this not the tail position?
What is the tail position in my loop then?
(loop [i 20]
(for [x (range 1 21)]
(if (zero? (rem i x))
i
(recur (+ i 1)))))
for
does not do what you think it does; it is not an imperative loop. It is a list comprehension, or sequence-generator. Therefore, there is not a return or iterate call at its end, so you cannot place recur
there.
It would seem you probably do not need either loop
or recur
in this expression at all; the for
is all you need to build a sequence, but it's not clear to me exactly what sequence you wish to build.
这篇关于我在Clojure循环中的尾部位置在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!