| Deutsch English Français Italiano |
|
<v7md14$pr7r$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!feeds.phibee-telecom.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Accumulating in hash-table
Date: Mon, 22 Jul 2024 19:47:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <v7md14$pr7r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 22 Jul 2024 21:47:50 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e396b2dbc7de6c10992283c407241561";
logging-data="847099"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mD0KLhPkqsq0VBnpMtqIA"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:PILd6cyRqttNCVAJl89dWQZE13o=
Bytes: 2172
> (defun distribution1 (items values test)
> (let ((table (make-hash-table :test test)))
> (loop for item in items
> for value in values
> do (incf (gethash item table 0) value))
> (let ((items-list nil))
> (maphash (lambda (item sum-value)
> (push (cons item sum-value) items-list))
> table)
> (sort items-list #'> :key #'cdr))))
>
> An example call:
>
> CL-USER 58 > (distribution1 '("a" "b" "c" "b" "a" "f" "e" "g"
> "h" "k" "z" "k" "r" "u" "f")
> '(1 5 8 7 14 8 3 7 9 4 3 21 5 7 9)
> #'equal)
> (("k" . 25) ("f" . 17) ("a" . 15) ("b" . 12) ("h" . 9) ("c" . 8)
> ("g" . 7) ("u" . 7) ("r" . 5) ("e" . 3) ("z" . 3))
Gauche Scheme
(define (distribution1 items values test)
(let1 table (make-hash-table test)
(for-each
(^(item value)
(hash-table-update! table item (cut + value <>) 0))
items
values)
(sort (hash-table->alist table) > cdr)))
(distribution1 '("a" "b" "c" "b" "a" "f" "e" "g"
"h" "k" "z" "k" "r" "u" "f")
'(1 5 8 7 14 8 3 7 9 4 3 21 5 7 9)
'equal?)
===>
(("k" . 25) ("f" . 17) ("a" . 15) ("b" . 12) ("h" . 9) ("c" . 8) ("g" . 7)
("u" . 7) ("r" . 5) ("z" . 3) ("e" . 3))