Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Adding features does not necessarily increase functionality -- it just makes the manuals thicker.


devel / comp.lang.lisp / Counting symbols

SubjectAuthor
o Counting symbolsRobert L.

1
Counting symbols

<suru3i$17gh$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16902&group=comp.lang.lisp#16902

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!eYeqBzNjiJ3ACJwkx5pzXw.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: Counting symbols
Date: Sat, 19 Feb 2022 23:22:28 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <suru3i$17gh$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="40465"; posting-host="eYeqBzNjiJ3ACJwkx5pzXw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Sat, 19 Feb 2022 23:22 UTC

> Erik Naggum wrote:
>
> > (defun count-member (symbols tree)
> > (let* ((counts (loop for symbol in symbols collect (cons symbol 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!

Gauche Scheme and Racket:

(require srfi/1) ;; iota for Racket

(define (countsyms syms tree)
(let ((index (map cons syms (iota (length syms))))
(counts (make-vector (length syms) 0)))
(define (count sym)
(let ((i (cond ((assoc sym index) => cdr) (else #f))))
(when i (vector-set! counts i (+ 1 (vector-ref counts i))))))
(let loop ((tree tree))
(unless (null? tree)
(if (symbol? (car tree))
(count (car tree))
(loop (car tree)))
(loop (cdr tree))))
(map cons (map car index) (vector->list counts))))

(countsyms '(w x y z) '(a x (b y y (z) z)))
===>
((w . 0) (x . 1) (y . 2) (z . 2))


devel / comp.lang.lisp / Counting symbols

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor