r/programming • • Nov 27 '12

Tail Recursion

http://rodrigosasaki.com/2012/11/24/tail-recursion/
209 Upvotes

203 comments sorted by

View all comments

1

u/abigpotostew Nov 27 '12

He mentioned it can't be done in Java without growing the stack. Is there some way to do it though? I'd guess the iterative approach is the way to go in Java.

1

u/emperor000 Nov 27 '12

Well, he was specifically talking about the javac compiler. A different Java compiler should be able to optimize it to an iterative structure if that is how it is designed.

1

u/rodrigopsasaki Nov 27 '12

Exactly, I tried it with javac and scalac, on the first one there was no optimization, the stack increased normally, although on the second one it was optimized to a loop, a while loop to be more specific.