t3x.org / nss / flatten.html

(Nils' Scheme Snippets)

 
Paren matching: ON  |  Category: lists  |  Overview  |  Scheme Books  |  License
 

(flatten pair) => list

 
Purpose
Convert tree to flat list.
 
Arguments
tree tree to flatten
 
Example
(flatten '(a (b ((c) d . e)))) => (a b c d e)
(define (flatten x)
  (letrec
    ((f (lambda (x r)
          (cond ((null? x) r)
                ((pair? x)
                  (f (car x)
                     (f (cdr x) r)))
                (else (cons x r))))))
    (f x '())))

Copyright (C) 2007 Nils M Holm <nmh @ t3x . org>