5
votes

Is there any way to tell if a function in F# is tail-recursive?

I've tried to explain it, but some people seem to struggle and ask if there's a way to confirm what they've done is tail recursive. Is there anything easy you can do (other than trying to make it overflow the stack) to confirm tail responsiveness (or not) (preferably in the IDE)?

(I tried Reflector, but it just crashes on me when I open F# projects!)

1
You could try other decompilers there are pretty many nowadays.CodesInChaos
I tried the JetBrains one, and it also crashed (after installing a bunch of crap on my machine, like a Control Panel!)Danny Tuppeny
I guess my question would be "why"? If someone is seeing a stack overflow in their code then they would want to make sure they've got TCO but that should be fairly obvious. Otherwise, it sounds like a bit of premature optimization.Onorio Catenacci
I'm just asking as an aid to teaching it; people that are new to things often want to check they're doing it right; it's not about optimising a specific case, but understanding how to do it if the need arises :-)Danny Tuppeny

1 Answers

5
votes

From here (with several examples in the article):

How can I verify that tail calls are being used?

The easiest way is to ensure that tail calls are being used is to understand and apply the rules from the previous section. While the F# compiler itself doesn’t currently provide any way to verify that tail calls have been used at a particular call site, you can be sure by looking at the compiled version of the code using the MSIL Dissasembler (ildasm.exe). We’ll see several examples below.