【问题标题】:What's some simple F# code that generates the .tail IL instruction?生成 .tail IL 指令的一些简单 F# 代码是什么?
【发布时间】:2010-06-05 06:54:56
【问题描述】:

我希望看到.tail IL 指令,但是我一直在编写的使用尾调用的简单递归函数显然已优化为循环。我实际上是在猜测这一点,因为我不完全确定反射器中的循环是什么样的。不过,我绝对看不到任何 .tail 操作码。我在项目的属性中检查了“生成尾调用”。我还尝试了 Reflector 中的 Debug 和 Release 版本。

我使用的代码来自Programming F# by Chris Smith,第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#代码?

【问题讨论】:

    标签: .net f# tail-recursion tail-call-optimization


    【解决方案1】:

    相互递归的函数应该:

    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)
    

    (刚刚没试过)。

    编辑

    另见

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

    【讨论】:

    • 我刚刚检查过了。是的!它生成.tail。
    猜你喜欢
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2015-09-16
    • 2012-02-19
    • 2016-07-29
    • 2015-11-17
    相关资源
    最近更新 更多