Raffael Cavallaro <raffaelcavallaro@pas-d'espam-s'il-vous-plait-mac.com> wrote:
+---------------
| vippstar@gmail.com said:
| > Unfortunately, I don't know how to do step 1.
|
| There is a very common lisp utility called flatten.
| Paul Graham gives a version in On Lisp thus:
| (defun flatten (x)
| (labels ((rec (x acc)
| (cond ((null x) acc)
| ((atom x) (cons x acc))
| (t (rec (car x) (rec (cdr x) acc))))))
| (rec x nil)))
+---------------
Note that this function should be more properly called FLATTEN-TREE,
as contrasted with FLATTEN-LIST, e.g.:
(defun flatten-list (list)
(loop for item in list
when (consp item)
nconc (flatten-list item)
else
collect item))
The difference appears when the data really is a "list of lists"
and some of the lists contain NIL as an member:
> (flatten-tree '(1 2 nil (3 4 nil (5 6 nil 7) nil) nil))
(1 2 3 4 5 6 7)
> (flatten-lists '(1 2 nil (3 4 nil (5 6 nil 7) nil) nil))
(1 2 NIL 3 4 NIL 5 6 NIL 7 NIL NIL)
>
Sometimes you want one, and sometimes you want the other.
It all depends on your particular application.
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607