You could do something like this, for example:
(defun fletten-level (tree)
(loop for e in tree
nconc
(if (consp e)
(copy-list e)
(list e))))
(fletten-level '(a (b (d (g f))) e))
;; (A B (D (G F)) E)
This loops over the original tree top-level branches and creates a new list containing if the branch was a leaf, that leaf, and if the branch had two other branches, then both the first leaf and the rest of the branches.
EDIT: sorry, it wasn't good the first time actually, now it should be fixed.
EDIT2: just because I almost got confused myself. (cons (car e) (cdr e))
looks a little weird because it is basically the same as saying just e
. However, I realized that nconc
will destructively modify the conses, so it has to be this way (i.e. create a new cons cell to be concatenated rather than reuse the old one).
EDIT3: Wow... actually, it has to be copy-list
, because it will modify the original list this way.