P08 (**) Eliminate consecutive duplicates of list elements.

Liste des GroupesRevenir à cl lisp 
Sujet : P08 (**) Eliminate consecutive duplicates of list elements.
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 19. Aug 2024, 10:14:08
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9uuse$2q1et$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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)

In newLisp, "apply" can be used for reduce or fold.

(define (compress lst)
  (reverse
    (apply
      (fn (accum x)
        (cond ((empty? accum) (list x))
              ((= x (first accum)) accum)
              (true (cons x accum))))
      (cons '() lst)
      2)  ;; How many things to process at a time.
))

(compress '(a a a a b c c a a d e e e e))
  ===>
(a b c a d e)

Date Sujet#  Auteur
19 Aug 24 * P08 (**) Eliminate consecutive duplicates of list elements.2B. Pym
19 Aug 24 `- Re: P08 (**) Eliminate consecutive duplicates of list elements.1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal