<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>today on one of the extremely rare occasions where I can use scheme at work, I had to get at the frequencies for a list of ids extracted from a logfile. E.g., I had a list like</div><div><br></div><div>'(1234 1234 5678 1234)</div><div><br></div><div>and wanted to, on the one hand, remove the duplicates and, on the other hand, store how many instances there were of each id.</div><div>For example, if represented as an alist, the result might have looked like this:</div><div><br></div><div>'((1234 3) (5678 1))</div><div><br></div><div>However, I could not imagine a "purely functional" way to do this, and so I used a box to update the frequencies, like so:</div><div><br></div><div><span class="Apple-style-span" style="font-size: 10px; "><div>(define count</div><div> (lambda (lst)</div><div> (let count-accum ((lst lst) (frequencies '()))</div><div> (if (null? lst)</div><div> frequencies</div><div> (let ((first (car lst)))</div><div> (cond ((assoc first frequencies)</div><div> (let ((count-box (cdr (assoc first frequencies))))</div><div> (begin (set-box! count-box (+ 1 (unbox count-box)))</div><div> (count-accum (cdr lst) frequencies))))</div><div> (else (count-accum (cdr lst) (cons (cons first (box 1)) frequencies)))))))))</div><div><br></div><div><br></div><div><span class="Apple-style-span" style="font-size: 12px; ">(I also considered a hashtable, which would have been equally "impure" and less practical, because the next processing step again required a list).</span></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;">I suppose there must be "more functional" (if I may say so) and more elegant ways to perform this task. I'd be glad to learn and thankful for any suggestions :-)</span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;">Ciao</span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;">Sigrid</span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px;"><br></span></font></div></span></div></body></html>