Yeah, but I do not worry about it, much, at the beginning. Here
is another, it seems easier to think in the morning
(defun summarize (list)
(let ((summary nil))
(map nil (lambda (elt)
(let ((sum (find (first elt) summary :test #'eql :key #'first)))
(if sum
(incf (second sum) (second elt))
(push elt summary))))
list)
summary))
and its loop version
(defun summarize (list)
(loop with summary = nil
for elt in list
for sum = (find (first elt) summary :test #'eql :key #'first)
if sum do (incf (second sum) (second elt))
else do (push elt summary)
finally (return summary)))
CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
((B 111) (A 4) (C 7))
Wade Humeniuk wrote:
Yeah, but I do not worry about it, much, at the beginning. Here
is another, it seems easier to think in the morning
(defun summarize (list)
(let ((summary nil))
(map nil (lambda (elt)
(let ((sum (find (first elt) summary :test #'eql :key #'first)))
(if sum
(incf (second sum) (second elt))
(push elt summary))))
list)
summary))
and its loop version
(defun summarize (list)
(loop with summary = nil
for elt in list
for sum = (find (first elt) summary :test #'eql :key #'first)
if sum do (incf (second sum) (second elt))
else do (push elt summary)
finally (return summary)))
CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
((B 111) (A 4) (C 7))
Gauche Scheme
(use gauche.sequence) ;; group-sequence
(define (summarize alist)
(map
(lambda(xs) (list (caar xs) (fold + 0 (map cadr xs))))
(group-sequence alist :key car)))
(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c 7) (a 4) (b 111))
Another way:
(use srfi-1) ;; list-index
(define (index x xs) (list-index (cut equal? x <>) xs))
(define (summarize alist)
(let1 keys (delete-duplicates (map car alist) )
(map
cons
keys
(apply map
+
(map
(lambda(kv)
(let1 v (make-vector (length keys) 0)
(vector-set! v (index (car kv) keys) (cadr kv))
(vector->list v)))
alist)))))
(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c . 7) (a . 4) (b . 111))
Wade Humeniuk wrote:
Yeah, but I do not worry about it, much, at the beginning. Here
is another, it seems easier to think in the morning
(defun summarize (list)
(let ((summary nil))
(map nil (lambda (elt)
(let ((sum (find (first elt) summary :test #'eql :key #'first)))
(if sum
(incf (second sum) (second elt))
(push elt summary))))
list)
summary))
and its loop version
(defun summarize (list)
(loop with summary = nil
for elt in list
for sum = (find (first elt) summary :test #'eql :key #'first)
if sum do (incf (second sum) (second elt))
else do (push elt summary)
finally (return summary)))
CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
((B 111) (A 4) (C 7))
Gauche Scheme
(use gauche.sequence) ;; group-sequence
(define (summarize alist)
(map
(lambda(xs) (list (caar xs) (fold + 0 (map cadr xs))))
(group-sequence alist :key car)))
(summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
===>
((c 7) (a 4) (b 111))
(defun summarize (list)
(loop with summary = nil
for elt in list
for sum = (find (first elt) summary :test #'eql :key #'first)
if sum do (incf (second sum) (second elt))
else do (push elt summary)
finally (return summary)))
CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
((B 111) (A 4) (C 7))
Yeah, but I do not worry about it, much, at the beginning. Here
is another, it seems easier to think in the morning
(defun summarize (list)
(let ((summary nil))
(map nil (lambda (elt)
(let ((sum (find (first elt) summary :test #'eql :key #'first)))
(if sum
(incf (second sum) (second elt))
(push elt summary))))
list)
summary))
and its loop version
(defun summarize (list)
(loop with summary = nil
for elt in list
for sum = (find (first elt) summary :test #'eql :key #'first)
if sum do (incf (second sum) (second elt))
else do (push elt summary)
finally (return summary)))
CL-USER 2 > (summarize '((c 7) (a 1) (a 3) (b 1) (b 10) (b 100)))
((B 111) (A 4) (C 7))
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (0 / 16) |
| Uptime: | 168:16:58 |
| Calls: | 12,096 |
| Calls today: | 4 |
| Files: | 15,003 |
| Messages: | 6,517,822 |