生成 .tail IL 指令的简单 F# 代码是什么?
我希望看到 .tail
IL 指令,但我一直在编写的使用尾部调用的简单递归函数显然已优化为循环。我实际上是在猜测这一点,因为我不完全确定反射器中的循环是什么样的。但我绝对没有看到任何 .tail
操作码。我在项目的属性中检查了“生成尾部调用”。我还尝试了 Reflector 中的调试和发布版本。
我使用的代码来自 Chris Smith 的《Programming F#》,第 190 页:
let factorial x =
// Keep track of both x and an accumulator value (acc)
let rec tailRecursiveFactorial x acc =
if x <= 1 then
acc
else
tailRecursiveFactorial (x - 1) (acc * x)
tailRecursiveFactorial x 1
可以有人建议一些确实会生成 .tail
的简单 F# 代码吗?
I'd like to see the .tail
IL instruction, but the simple recursive functions using tail calls that I've been writing are apparently optimized into loops. I'm actually guessing on this, as I'm not entirely sure what a loop looks like in Reflector. I definitely don't see any .tail
opcodes though. I have "Generate tail calls" checked in my project's properties. I've also tried both Debug and Release builds in Reflector.
The code I used is from Programming F# by Chris Smith, page 190:
let factorial x =
// Keep track of both x and an accumulator value (acc)
let rec tailRecursiveFactorial x acc =
if x <= 1 then
acc
else
tailRecursiveFactorial (x - 1) (acc * x)
tailRecursiveFactorial x 1
Can anyone suggest some simple F# code which will indeed generate .tail
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相互递归函数应该:(
刚才还没有尝试过)。
编辑
另请参阅
我怎么知道如果F# 中的函数是尾递归
Mutually recursive functions should:
(have not tried it just now).
EDIT
See also
How do I know if a function is tail recursive in F#