What's some simple F# code that generates the .tail IL instruction?

可紊 提交于 2019-12-10 13:01:59

问题


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?


回答1:


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#



来源:https://stackoverflow.com/questions/2979472/whats-some-simple-f-code-that-generates-the-tail-il-instruction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!