2010-06-05 82 views
7

我希望看到.tail IL指令,但使用我一直在写的tail调用的简单递归函数显然是优化成循环的。我实际上正在猜测,因为我不完全确定Reflector中的循环是什么样的。尽管我确实没有看到任何.tail操作码。我在项目的属性中选中了“生成尾部呼叫”。我也尝试了反射器中的Debug和Release版本。什么是生成.tail IL指令的一些简单的F#代码?

我使用的代码是从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 

任何人都可以提出一些简单的F#代码这的确会产生.tail

回答

6

相互递归函数应该:

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#

+1

我刚才检查。是!它会生成.tail。 – 2010-06-05 07:37:15