<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div><br></div><div>Frequencies come in ranges and my guess is that you have the full range of them. </div><div><br></div><div>If so, I would have no hesitations giving up pure FP and use a vector in the proper range. I doubt very much any FP technique comes close in efficiency. </div><div><br></div><div>-- Matthias</div><div><br></div><div><br></div><br><div><div>On Apr 18, 2009, at 4:18 PM, <a href="mailto:keydana@gmx.de">keydana@gmx.de</a> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">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><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_________________________________________________</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span class="Apple-converted-space"> </span>For list-related administrative tasks:</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span class="Apple-converted-space"> </span><a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a></div> </blockquote></div><br></body></html>