Re: duplicates

Liste des GroupesRevenir à cl lisp 
Sujet : Re: duplicates
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 26. Jun 2025, 19:31:53
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <103k3mn$3jsdj$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Pascal Costanza wrote:
 
There doesn't seem to be a way to return a list of duplicates of a
sequence in ANSI CL -- though there is a remove-duplicates. Is there a
reason for this? It would be handy if you could tell remove-duplicates
not to include any duplicated elements so you could do a set-
difference at the end to get a list of duplicates. Feel free to post
code to prove me wrong. Thanks!
 
(loop
   with counts
   for element in list
   do (incf (getf counts element 0))
   finally (return
             (loop for (element count) on counts by #'cddr
               if (> count 1)
               collect element into duplicates
               else collect element into uniques
               finally (return (values uniques duplicates)))))
 
I am using a property list for counting elements, which means that eq is
implicitly used for detecting equivalent elements. If you want to use
other comparison functions, it is better to use an association list
(which makes the code a little bit wordier).
 
In general, LOOP is a pretty good poor man's list comprehension
facility. Just ignore that it performs iteration and use it to emulate a
more declarative style.

Gauche Scheme

(apply values
  (map (cut  map car <>)
    (values->list
      (partition (^(xs) (= 1 (cdr xs)))
        (rlet1 res ()
          (dolist (x '(a b c d e f g b d f))
            (if-let1 found (assoc x res)
              (inc! (cdr found)) (push! res `(,x . 1)))))))))

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


Date Sujet#  Auteur
26 Aug 24 * Re: duplicates4B. Pym
26 Aug 24 +* Re: duplicates2B. Pym
26 Aug 24 i`- Re: duplicates1B. Pym
26 Jun 25 `- Re: duplicates1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal