How to remove nested parentheses in LISP

前端 未结 12 1419
清酒与你
清酒与你 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:36

    This is a accumulator based approach. The local function %flatten keeps an accumulator of the tail (the right part of the list that's already been flattened). When the part remaining to be flattened (the left part of the list) is empty, it returns the tail. When the part to be flattened is a non-list, it returns that part prefixed onto the tail. When the part to be flattened is a list, it flattens the rest of the list (with the current tail), then uses that result as the tail for flattening the first part of the list.

    (defun flatten (list)
      (labels ((%flatten (list tail)
                 (cond
                   ((null list) tail)
                   ((atom list) (list* list tail))
                   (t (%flatten (first list)
                                (%flatten (rest list)
                                          tail))))))
        (%flatten list '())))
    

    CL-USER> (flatten '((1 2) (3 4) ((5) 6) 7))
    (1 2 3 4 5 6 7)
    
    0 讨论(0)
  • 2020-11-29 10:37

    Lisp has the function remove to remove things. Here I use a version REMOVE-IF that removes every item for which a predicate is true. I test if the thing is a parenthesis and remove it if true.

    If you want to remove parentheses, see this function:

    (defun unnest (thing)
      (read-from-string
       (concatenate
        'string
        "("
        (remove-if (lambda (c)
                     (member c '(#\( #\))))
                   (princ-to-string thing))
        ")")))
    

    Note, though, as Svante mentions, one does not usually 'remove' parentheses.

    0 讨论(0)
  • 2020-11-29 10:37

    Most of the answers have already mentioned a recursive solution to the Flatten problem. Using Common Lisp Object System's multiple dispatching you could solve the problem recursively by defining 3 methods for 3 possible scenarios:

    (defmethod flatten ((tree null))
      "Tree is empty list."
      ())
    (defmethod flatten ((tree list))
      "Tree is a list."
      (append (flatten (car tree))
              (flatten (cdr tree))))
    (defmethod flatten (tree)
      "Tree is something else (atom?)."
      (list tree))
    
    (flatten '(2 ((8) 2 (9 (d (s (((((a))))))))))) ; => (2 8 2 9 D S A)
    
    0 讨论(0)
  • 2020-11-29 10:39

    This popular question only has recursive solutions (not counting Rainer's answer).

    Let's have a loop version:

    (defun flatten (tree &aux todo flat)
      (check-type tree list)
      (loop
         (shiftf todo tree nil)
         (unless todo (return flat))
         (dolist (elt todo)
           (if (listp elt)
               (dolist (e elt)
                 (push e tree))
               (push elt flat))))))
    
    0 讨论(0)
  • 2020-11-29 10:40
    (defun unnest (somewhat)
      (cond
       ((null somewhat) nil)
       ((atom somewhat) (list somewhat))
       (t
        (append (unnest (car somewhat)) (unnest (cdr somewhat))))))
    
    0 讨论(0)
  • 2020-11-29 10:44

    Here's what I'd do:

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

    That works mainly because I have Quicklisp installed already.

    0 讨论(0)
提交回复
热议问题