August 19, 2024

P08 - Eliminate consecutive duplicates of list elements

If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.

Example:* (compress '(a a a a b c c a a d e e e e))(A B C A D E)

lisp

(defun compress (list)
  (cond
   ((null list) nil)
   ((null (cdr list)) list)
   ;; if the first item (list) and' equal to the consecutive
   ;; (first of the rest)
   ((eql (first list) (first (rest list)))
    ;; find it ignores the first of the list and continues recursively in the rest
    (compress (rest list)))
   (t (cons (first list) (compress (rest list))))
   )
  )
Be first to comment
Leave a reply