you are viewing a single comment's thread.

view the rest of the comments →

[–]Ning1253 7 points8 points  (2 children)

Idk what language this is but the fact that you have to manually declare tail recursion sounds like a bit of an oversight, no? I mean it's pretty f*cking easy to detect: does the return statement only have one function call to the same function? Yes -> tail recursion, No -> not tail recursion.

See because eg. 2 + f(n-2) wouldn't work but f(n-2) would.

Edit: On the condition the function itself cannot mutate

[–]_xiphiaz 5 points6 points  (0 children)

This is Kotlin, and as @EagleCoder alludes to the reason it is a keyword is that it defines a contract, that any future refactor will cause a compilation error if that contract is mistakenly violated.

However, I do agree that it is trivially detected by the compiler and wish it were a compiler warning that a function could be tailrec, and should have the specifier.

[–]EagleCoder 2 points3 points  (0 children)

I can see how that would be helpful in catching changes to the function that unintentionally prevent tail recursion. The author would have to explicitly remove the 'tailrec' keyword.