4 ms·
Why?
by eudox 11y ago
Why?
- ape4 11y agoI'm not an expert... but you don't want your C/C++ code stack overflowing or running out of memory in an uncontrolled way. Your C/C++ can do graph reduction by working on a structure in a loop. Then you'll be able to better monitor resources.
- eudox 11y agoEh, clarity of code is more important, in my opinion. If you run out of stack, get a better computer.
- johncolanduoni 11y agoStack doesn't generally scale up with RAM. GCC and Clang can both do some tail call optimization, and I find rewriting code to use an accumulator isn't very hard on the eyes.
- gohrt 11y agos/computer/compiler/
- bglusman 11y agoThis is why TCO (Tail call optimization) exists... but of course not all recusion is tail recursion, but I think it can always be written this way, and should always be for arbitrary depth recursion.
- gohrt 11y agoElimination, not Optimization. Either you can rely on it as part of the spec (Elimination) or you can't (Optmization)
- sgeisenh 11y agoYes, you can always convert direct style code to continuation passing style code. And continuation passing only uses tail calls. This is one of the big advantages of CPS. Of course if you don't have a tail call optimization, then you overflow the stack pretty quick.
- jonsen 11y agoThere is normally no deep recursion in compiling a program. The compiler would recurse over recursive program constructs, like nested if statements. Programmers normally do not nest constructs very deep.
- tbirdz 11y agoThis may be true for human programmers, but compilers also must compile machine generated code, which often looks very different. As an example, I can't remember the source on this, but I have read somewhere that the original C++ compiler (cfront) compiled C++ into C code, and this generated code exposed many bugs in the C compilers of the time, as it was very different than how a human would have written that code.
- jonsen 11y agoRunning out of stack space compiling a machine generated program would be a bug in the automatic source code generator then.
- jonsen 11y agoIn the same sense that running out of stack space compiling a human generated program is a bug in the programmer.
- kazinator 11y agoIf human-generated programs compete head-to-head with robot-generated programs for blowing up the implementation, that human has a worse bug than the robot.
- steveklabnik 11y agoI just watched a talk where Kernighan brought up this exact anecdote.
- chrisseaton 11y agoWhen I write a program I aspire to impose no arbitrary limits apart from those imposed by the architecture. So I'd like my compiler to be able to handle programs as complex as the user wants, with the only limit being the available virtual memory and disk space. If you use recursion for 'outer loop' stuff, you impose the extra limitation of the size of the stack, which is much smaller than available virtual memory. I work with partial evaluation, where intermediate representations during compilation can get quite large, and have seen stack overflows in the compiler for real. It would be nice if stacks were arbitrarily large anyway.
- iheartmemcache 11y agoThe `Letrec' construct (see: chapter 3) addresses your concerns. It extends the language from a standard S-K-I lambda calculus into a language that "copes" with recursion, especially with TCO.
- deleted 11y ago[deleted]