https://kotlinlang.org logo
Title
r

Rohan Maity

01/17/2019, 2:27 AM
Does anyone know how
tailrec
keyword works . I mean how it converts the tail call optimised recursion to iterative one
d

dr.dreigh

06/27/2019, 8:08 PM
it uses a loop, and mutation (you can verify this by converting your code to bytecode and decompiling to java code)
apologies for super slow response, I was also tempted to say it uses
tailrec
r

Rohan Maity

07/03/2019, 4:44 PM
Actually I was interested in procedure how it converts to loop and mutation. I have seen java bytecode
e

E.Kisaragi

11/19/2019, 10:42 PM
it should be converted to a loop which exits on return condition and has acc to memorize result;
kotlin
tailrec fun fact(n: Int): Int {
if (n == 1) return 1
return n*fact(n-1)

fun fact_(n: Int): Int {
var acc = n
var ncopy = n
while (ncopy != 1) { // negates break recursion call condition
    acc = acc * ncopy
}
return acc
}