Thanks, I get it now!   Some languages will generate code that won't
overflow the stack when doing tail recursion.  Ruby doesn't have that
optimization, yet.
On 12/16/05, Collins, Justin <collinsj / seattleu.edu> wrote:
> I wouldn't really call it optimization (although I guess it is), it's more like implementing them in such a way that you get around the limitations of computer memory. (As opposed to just making them run faster or something.) It doesn't really have anything to do with efficiency in that sense.
>
> Code like:
>
> def fun(x)
>     fun(x)
> end
>
> fun(10)
>
> should run forever, like it would in a language like Scheme or Lua. A function calls itself, which calls itself, for infinitly. Unfortunately, Ruby does not currently work that way.
>
> It's not that Ruby doesn't work "right" in this case, it just runs into the limits of hardware (the size of the stack). The "proper" way of implementing tail calls would never use the stack, so it wouldn't be an issue.
>
> By the way, is there a particular reason why tail calls aren't implemented that way?