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

Tail call optimization is not guaranteed for optimized C/C++ programs, and is not done at all in debug mode, so this kind of implementation can cause trouble with large structures.

I used to have fun, too, implementing tail-recursive functions to do stuff like flashing a LED on microcontrollers. I have since recovered :)

It is also my experience that helper functions like "sum" tend to be used very few times, and need a lot of variations, so it's actually better to just write the loop when needed. The theoritically advantageous functional programming is hard to convey into C++. Syntax and other technical reasons are partly to blame, but the biggest has to be simply the culture. Very few C++ coders think functionally, and coding, especially in C++, tends to be a team job...



The tail recursion was not my point. I also could have given another implementation of sum.

I wanted to demonstrate that the implementation of Dice::total() can be much clearer and still in a similar functional way.




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

Search: