George Ogata <g_ogata / optushome.com.au> writes: > Christian Neukirchen <chneukirchen / gmail.com> writes: > >> The real question is why the Ruby interpreter doesn't do tail-call >> optimization... > > Wouldn't this be tricky in the general case? Consider: > > def f arg > (class << arg; self; end).send(:define_method, :f){|a| 10} > f(1) > end > > `f' looks tail recursive but isn't since it may redefine itself: > > f(self) So what? It just calls the new definition with a TC and it's done... It can simply detect on each function *definition* whether it is going to use TCO. > We could just sweep the problem under the rug (optimize anyway and > hope no-one tries anything that crazy). Or we could resort to a new > keyword, say `recurse(...)', which would mean "call the function it > appears in". That would be optimizable. Please don't try to copy Forth, that is a bad idea in general. :-) > Just a thought. -- Christian Neukirchen <chneukirchen / gmail.com> http://chneukirchen.org