[plt-scheme] Tail recursion and the Visitor Pattern

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Dec 30 13:26:21 EST 2006

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.

HNY -- Matthias

Posted on the users mailing list.