2

Possible Duplicate:
How to remove nested parentheses in LISP

This is my second quick-and-silly question about LISP, but I am kind of stuck. I need to access all the nodes in a list with several levels. I need something like:

>> (get-symbols '(A (B (C D) E )))
(A B C D E)

I don't care about the order. How would you do that? I prefer code intuitivity rather than efficency.

Thanks

Community
  • 1
  • 1
Jk041
  • 934
  • 1
  • 15
  • 33
  • 2
    check out http://stackoverflow.com/questions/2680864/how-to-remove-nested-parentheses-in-lisp – Nick Dandoulakis Oct 31 '10 at 18:29
  • "nested parenthesis". I didn't think on that way to put it in, that's why i didn't find that one. If it works for me, and I hope so, should I remove this question? – Jk041 Nov 01 '10 at 02:30
  • 2
    "flattening nested lists" is the proper description. Your question is a duplicate. Delete it if you want to. – Nick Dandoulakis Nov 01 '10 at 05:50

3 Answers3

4

What you need is the flatten function for lists. Look it up.

Gintautas Miliauskas
  • 7,744
  • 4
  • 32
  • 34
  • I can't find that function in Common Lisp (I forgot to indicate which Lisp version i have to use) – Jk041 Nov 01 '10 at 02:31
4

From OnLisp:

(defun flatten (tree)
  (if (atom tree)
      (mklist tree)
    (nconc (flatten (car tree))
       (if (cdr tree) (flatten (cdr tree))))))
Marko
  • 30,263
  • 18
  • 74
  • 108
0
 (defun get-symbols (lst)
        (when lst
           (if (atom lst) (list lst)
               (append (get-symbols (car lst))
                       (get-symbols (cdr lst))))))
Galactus
  • 806
  • 7
  • 10