[plt-scheme] Re: Tail recursion and the Visitor Pattern
Matthias Felleisen wrote:
> On Dec 30, 2006, at 12:50 PM, wooks . wrote:
>
> >
> > Thought I'd try and make this tail recursive and got into a tangle.
> >
> > class ButLastV extends EmptyException implements IList{
> > public AList forCons(Object first, AList rest) {
> > return rest.isEmpty() ?
> > Empty.ONLY :
> > new Cons(first, ((AList)rest.accept(this)));
> > }
> > }
> >
> > Then I realised there is no recursive call .... well there is but
> > its of indirect. Does this make tail recursion in the Visitor
> > Pattern a no no.
>
> Try a Visitor that uses an accumulator. Then test it on a large data
> set and weep. Java isn't TCO [I declare this a new English adjective
> as of now], meaning it doesn't allow programmers to design according
> to OO principles. Instead you MUST put yourself into the mind-set of
> a prehistoric IMPERATIVE programmer and work from there.
>
So even if I could figure out how to use an accumulator/replace the
embedded (indirect recursion) with tail recursive (not sure how or
which) , lack of TCO means I would still get a stack overflow, right?