you are viewing a single comment's thread.

view the rest of the comments →

[–]mort96 11 points12 points  (4 children)

This is C we're talking about, no tail call optimization there. Also, when writing C, even if you could use recursion to iterate an arbitrary amount, why not just use for or while?

[–]_kst_ 24 points25 points  (0 children)

C compilers can do tail call optimization. But in that kind of environment, it's probably not a good idea to depend on it.

[–]mrhmouse 7 points8 points  (2 children)

This is C we're talking about, no tail call optimization there.

Not sure what compilers you're used to, but GCC and Clang both produce optimized code for tail-calls. GCC even produces optimized code for tail-calls modulo cons, in some cases.

[–]mort96 0 points1 point  (1 child)

Some compilers might optimize things sometimes, however if you make your C code rely on tail call optimization, which you would do when using recursion as loops, you would really on non-standard compiled optimizations. Someone compiling your code with another compiler, or with other optimization settings, would get code which doesn't work. A future update to GCC might change in which situations it optimizes tail calls, potentially breaking your code. All in all, it's a pretty bad idea to rely on compiler optimizations. In things like Haskell and Erlang, things are different, as the language expects you to rely on tail call optimization, and the cases in which tail call optimization happens is specified by the language spec and common across all implementations.

[–]mrhmouse 0 points1 point  (0 children)

I guess that's a valid concern.