https://kotlinlang.org logo
k

karelpeeters

12/08/2018, 8:05 PM
Well but there you only call
callatz_tailrec
once, while in the challenge you need to call it
n
times.
j

joelpedraza

12/08/2018, 8:08 PM
I’m not sure what you mean. It calls it self more than once (assuming you dont start the recursion with 1)
Copy code
collatz_tailrec(23) =>

23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1
k

karelpeeters

12/08/2018, 8:11 PM
I mean that it only calls itself once per iteration.
j

joelpedraza

12/08/2018, 8:12 PM
Ah. Well yes then the first call is’nt in the tail position
My intuition behind how tail call elimination works is: If a recursive call is the tail position it can be replaced with a GOTO. If the function has more work to do it cant just execute a jump back to the top
k

karelpeeters

12/08/2018, 8:35 PM
But then
tailrec
isn't really applicable in the challenge, both because there's more work to be done after the tail call (constructing the Node) and because you need to call it multiple times.
j

joelpedraza

12/08/2018, 8:52 PM
Exactly correct.
11 Views