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

Posted by kld2010 on Stack Overflow See other posts from Stack Overflow or by kld2010
Published on 2010-06-05T06:54:56Z Indexed on 2010/06/05 7:02 UTC
Read the original article Hit count: 127

Filed under:

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?

© Stack Overflow or respective owner

Related posts about F#