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

Aubrey Jaffer agj at alum.mit.edu
Fri Oct 6 22:38:45 EDT 2006


 | Date: Fri, 6 Oct 2006 18:56:27 -0400 (EDT)
 | From: AndrevanTonder <andre at het.brown.edu>
 | ...
 | 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).

To minimize memory usage, wouldn't balanced binary trees put child
nodes in the CAR and CDR?  In that case, FOR-EACH can't be used.



More information about the r6rs-discuss mailing list