Nested Tree Recursion: fringe

Collect a list of the atoms on the fringe of a tree:


(define (fringe tree)
  (nreverse (fringeb tree '())))

(define (fringeb tree lst) (if (pair? tree) (fringeb (cdr tree) (fringeb (car tree) lst)) (if (null? tree) lst (cons tree lst) ) ) )

> (fringe '(and (now) (for something (completely different)))) (and now for something completely different)

Note that since fringeb uses cons to build its answer, the answer comes out backwards; therefore, fringe uses nreverse to reverse it.

Contents    Page-10    Prev    Next    Page+10    Index