recur关键字如何找出递归点?
我编写了以下代码来练习如何在 clojure 中编写循环:
(ns tutorial.loops)
(defn testing_loops
"A function to test loops"
[]
(loop [x 0]
(when (< x 10)
(println x)
(recur (inc x))
)
)
)
(testing_loops)
我的问题是,recur 关键字如何知道递归点是什么?
它是如何决定循环是递归点而不是函数testing_loops的?
编辑:
为什么时间不能成为递归点?
I wrote the following code to practice how to write loops in clojure:
(ns tutorial.loops)
(defn testing_loops
"A function to test loops"
[]
(loop [x 0]
(when (< x 10)
(println x)
(recur (inc x))
)
)
)
(testing_loops)
My question is, how does the recur keyword know what the recursion point is?
How did it decide that the loop is the recursion point and not the function testing_loops?
EDIT:
Why can't the when be a recursion point?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个“父”循环或 fn 是重复目标。
The first "parent" loop or fn is the recur target.
事情是这样的,这整个
recur
东西是clojure编译器缺乏尾部调用消除的解决方法(在jvm中?)所以。
recur
将控制权传递到以下两点:函数的顶层,例如
(defn f [x] (recur (inc x)))
或最接近的父loop
主体(如果有)(包括一些罕见的宏,使用在底层循环
,就像核心中的 go-loop 一样。异步)。也就是说,我要强调:这纯粹是语言/编译器创建者的设计决定,因此这就是为什么不能重复
其他形式。只是要记住一条规则。就是这样the thing is, this whole
recur
stuff is the clojure compiler's workaround of the lack of tail call elimination (in jvm?)So. There are exactly 2 points where
recur
passes control to:The function's top level e.g.
(defn f [x] (recur (inc x)))
or closest parentloop
body if any (including some rare macros, usingloop
under the hood, like go-loop from core.async). That said, i'd emphasize: this is purely a design decision of language/compiler creators, so that's why no other forms can berecur
red to. Just a rule to remember. that's it