Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove nested parentheses in LISP

How can I remove nested parentheses recursively in Common LISP Such as

  (unnest '(a b c (d e) ((f) g))) => (a b c d e f g)
  (unnest '(a b))                 => (a b)
  (unnest '(() ((((a)))) ()))     => (a)

Thanks

like image 502
bubdada Avatar asked Apr 21 '10 06:04

bubdada


2 Answers

Here's what I'd do:

(ql:quickload "alexandria")
(alexandria:flatten list)

That works mainly because I have Quicklisp installed already.

like image 155
Xach Avatar answered Oct 02 '22 11:10

Xach


(defun flatten (l)
  (cond ((null l) nil)
        ((atom l) (list l))
        (t (loop for a in l appending (flatten a)))))
like image 27
Donnie Cameron Avatar answered Oct 02 '22 13:10

Donnie Cameron