[r6rs-discuss] [Formal] last-element behavior of for-each

AndrevanTonder andre at het.brown.edu
Fri Oct 6 18:56:27 EDT 2006


On Fri, 6 Oct 2006, Per Bothner wrote:

> I'm having problems seeing the purpose of the function, and also
> which lists would be affected by tail-call semantics.  (I'm not
> not very good with complex recursion, and this is effectively
> two nested cursive functions.)  Could you explain?

Sorry, what I meant to write (but didn't) was the following:

(define (traverse-tree tree)
   (for-each (lambda (node)
               (if (list? node)
                   (traverse-tree node)
                   (display node)))
             tree))

(traverse-tree '((1 2 3) (3 4 5) (((((6))))) (7)))

    ===> 1234567

(traverse-tree '(((((((((((((((((1))))))))))))))))))

    ===> 1

Here, for example, the sequence of examples

   (1), ((1)), (((1))), ...

will all require the same space to run under the proposed r6rs semantics.  For
e.g. a balanced binary tree, the maximum amount of memory used 
during traversal will not be affected, but the average amount over the time 
of execution will be about half with r6rs-semantics (only the leftmost branch 
would take the maximum memory, and after that the memory use just gets less and 
less).

Cheers
Andre



More information about the r6rs-discuss mailing list