Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Your supervisor is thinking about you.


comp / comp.lang.lisp / Re: Symbol Frequency

SubjectAuthor
o Re: Symbol FrequencyB. Pym

1
Subject: Re: Symbol Frequency
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sun, 15 Sep 2024 05:23 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Symbol Frequency
Date: Sun, 15 Sep 2024 05:23:30 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <vc5r0d$20jhc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 15 Sep 2024 07:23:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0f1f4abe2f0188b39c05744f5a8843bd";
logging-data="2117164"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AUi2xBnQFpkBsk8g/7bwD"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:oC+dLfaUCIxMcuyEiHCchYdESqI=
View all headers

John Gilson wrote:

> > Is there a standard Lisp function that will take a list and return an
> > associative list representing the symbol frequency in the original list?
> > For example, (frequency '(a (a (b) a))) => ((a . 3) (b . 1)). Any help
> > would be greatly appreciated.
> >
> > Thank you.
>
> Coincidentally, someone else asked the same question earlier today.
>
> (defun occurrences (list &key (test #'eql))
> (if (consp list)
> (let (alist)
> (labels ((occurrences-aux (object)
> (cond ((null object))
> ((consp object)
> (occurrences-aux (first object))
> (occurrences-aux (rest object)))
> (t
> (let ((count (assoc object alist :test test)))
> (if (null count)
> (setf alist (acons object 1 alist))
> (incf (rest count))))))))
> (occurrences-aux (first list))
> (occurrences-aux (rest list)))
> alist)))
>
> > (occurrences '(a (a (a b c) b c) b . c))
> ((C . 3) (B . 3) (A . 3))

Gauche Scheme:

(define (occurrences tree)
(rlet1 alist '()
(let go ((x tree))
(cond
((null? x))
((pair? x) (go (car x)) (go (cdr x)))
(#t (ainc! alist x))))))

(occurrences '(a (a (a b c) b c) b . c))
===>
((c . 3) (b . 3) (a . 3))

Given:

(define-syntax ainc!
(syntax-rules ()
[(_ alist key val func default)
(let ((pair (assoc key alist)))
(if pair
(set-cdr! pair (func val (cdr pair)))
(set! alist (cons (cons key (func val default)) alist))))]
[(_ alist key val func)
(ainc! alist key val func 0)]
[(_ alist key val)
(ainc! alist key val +)]
[(_ alist key)
(ainc! alist key 1)]))

1

rocksolid light 0.9.8
clearnet tor