生成 .tail IL 指令的简单 F# 代码是什么?

发布于 2024-09-04 12:14:54 字数 644 浏览 2 评论 0原文

我希望看到 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心安伴我暖 2024-09-11 12:14:54

相互递归函数应该:(

let rec even n = 
    if n = 0 then 
        true 
    else
        odd (n-1)
and odd n =
    if n = 1 then 
        true 
    else
        even (n-1)

刚才还没有尝试过)。

编辑

另请参阅

我怎么知道如果F# 中的函数是尾递归

Mutually recursive functions should:

let rec even n = 
    if n = 0 then 
        true 
    else
        odd (n-1)
and odd n =
    if n = 1 then 
        true 
    else
        even (n-1)

(have not tried it just now).

EDIT

See also

How do I know if a function is tail recursive in F#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文