我在Clojure循环中的尾部位置在哪里? [英] Where is the tail position in my Clojure loop?

查看:128
本文介绍了我在Clojure循环中的尾部位置在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆