Hacker News new | past | comments | ask | show | jobs | submit login

To quote Structure and Interpretation of Computer Programs (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html...):

[...] most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose ``looping constructs'' such as do, repeat, until, for, and while. The implementation of Scheme we shall consider in chapter 5 does not share this defect. It will execute an iterative process in constant space, even if the iterative process is described by a recursive procedure. An implementation with this property is called tail-recursive. With a tail-recursive implementation, iteration can be expressed using the ordinary procedure call mechanism, so that special iteration constructs are useful only as syntactic sugar.




Right. Recursion has a problem with stack overflows, but that can sometimes be automatically fixed by turning it into iteration and throwing out the superfluous frames. Which one is the special case now?


Why? Iteration.

Stacks and stuff are just a way for computers to do some forms of recursion. Recursion itself is just a general mathematical idea.

Look at the Natural numbers (http://en.wikipedia.org/wiki/Recursion#Example:_the_natural_...) or dynamic programming (http://en.wikipedia.org/wiki/Recursion#Recursive_optimizatio...). Dynamic programming is an interesting case, because in most languages the idiomatic way to express it uses iterative constructs.




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

Search: