How to remove nested parentheses in LISP

前端 未结 12 1568
清酒与你
清酒与你 2020-11-29 10:20

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))                    


        
12条回答
  •  心在旅途
    2020-11-29 10:57

    You could define it like this for example:

    (defun unnest (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)))
    

提交回复
热议问题