Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Nope. Its worse. The solutions given in the book for the Fibonacci sequence and the factorial won't be optimized into tail recursion even in scheme unless some additional transformations were carried out on them first. In the factorial sequence, the last call is to the multiply method, not the factorial sequence and in the Fibonacci sequence, the last call is to the addition function.

Sad that most people don't catch that.



What is worse do you mean? Was I wrong about something?


No. You were right that with large inputs the program stack will blow up. However, the definition of tail recursive is if a function returns the value of a recursive call or does not make a recursive call and the factorial and Fibonacci functions weren't even tail recursion in that sense. Ie. it didn't matter where they were implemented, they would still blow up the stack anyway. The "worse" is because they will always blow the stack without some drastic optimization.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: