Re: count symbols in a list

Liste des GroupesRevenir à cl lisp 
Sujet : Re: count symbols in a list
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 06. Jul 2025, 22:22:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <104epf6$2ethv$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:

Erik Naggum wrote:
 
 I want to write a function that takes a list of symbols k and and lisp
 expression l and counts the number of times each symbol in k occurs in
 the lisp expression. It should return an alist binding each symbol to its
 count.  I want to do this without flattening the list before I go through
 it looking for symbols.
 
  Look for two things in this code: How it is formatted, and how it does
  its work.  (The way you have formatted your code annoys people.)  Explain
  to me why this works and gives the right answer when you have ascertained
  that it does.  Explain why it is efficient in both time and space.
 
(defun count-member (symbols tree)
  (let* ((counts (loop for symbol in symbols collect (cons symbol 0)))
 
Why didn't he use the simpler "mapcar" instead of "loop"?
Example:
 
(mapcar (lambda(s) (cons s 0)) '(a b c))
  ===>
((A . 0) (B . 0) (C . 0))
 
 
         (lists (list tree))
         (tail lists))
    (dolist (list lists)
      (dolist (element list)
        (cond ((consp element)
               (setf tail (setf (cdr tail) (list element))))
              ((member element symbols :test #'eq)
               (incf (cdr (assoc element counts :test #'eq)))))))
    counts))
 
 
Testing:
 
* (count-member '(w x y z) '(a x (b y y (z) z)))
 
((W . 0) (X . 1) (Y . 0) (Z . 0))
 
It only counts the top-level symbols!


The testing was done under SBCL.  Perhaps the function
will work correctly under another version of CL.
In any case, this is questionable code.


Date Sujet#  Auteur
6 Jul18:14 * Re: count symbols in a list5B. Pym
6 Jul20:30 +* Re: count symbols in a list2B. Pym
6 Jul20:55 i`- Re: count symbols in a list1B. Pym
6 Jul22:22 `* Re: count symbols in a list2B. Pym
7 Jul02:19  `- Re: count symbols in a list1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal