Can an F# function be considered tail recursive it uses the TailCall .net opcode

前端 未结 2 534
小鲜肉
小鲜肉 2021-01-19 15:08

Since .net has the TailCall opcode, can this be used to deterime if an F# function is truly tail recursive?

If it is true, has anyone made a VS add-in that identifi

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 15:39

    Yes, if the compiler emits the tail call instruction, that call will be tail recursive (as of CLR 4, but there are still some exceptions, where it won't be actually tail recursive). But that doesn't necessarily mean that the whole function is tail recursive. For example, I can imagine QuickSort function compiled so that the first recursive call is not tail recursive and the second one is.

    Also, just because some function does not contain the tail instruction, it doesn't necessarily mean that it is not tail recursive. The JIT compiler can recognize a tail call even without the tail instruction and optimize it as such.

    What's more, the F# compiler sometimes compiles recursive functions in a non-recursive way. This is somewhat different than normal tail call optimization and the tail instruction is not used, but the overall effect is similar.

提交回复
热议问题