This works, assuming xs consists of positive integers:<div><br></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif">(define (benford xs)</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"> (for-each</font></div>
<div><font class="Apple-style-span" face="arial, helvetica, sans-serif"> (lambda (x) (printf "~a ~f~%" (car x) (/ (cdr x) (length xs))))</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"> (uniq-c = (sort < (map (compose car digits) xs)))))</font></div>
<div><br></div><div>Compose, digits and uniq-c are from my <a href="http://programmingpraxis.com/standard-prelude">Standard Prelude</a>.</div><div><br></div><div>Phil</div><div><br><div class="gmail_quote">On Wed, Oct 13, 2010 at 11:10 PM, Shriram Krishnamurthi <span dir="ltr"><<a href="mailto:sk@cs.brown.edu">sk@cs.brown.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Given: A list of numbers. Assume integers (though it isn't necessary<br>
for the underlying issue).<br>
<br>
Task: To determine how often the first digit is 1, the first digit is<br>
2, ..., the first digit is 9, and present as a table. You may exploit<br>
DrRacket's pretty-printer, e.g., by using a list of lists:<br>
<br>
'((#\1 22.51539138082674)<br>
(#\2 16.44678979771328)<br>
(#\3 15.567282321899736)<br>
(#\4 12.401055408970976)<br>
(#\5 9.058927000879507)<br>
(#\6 7.651715039577836)<br>
(#\7 6.420404573438875)<br>
(#\8 5.804749340369393)<br>
(#\9 4.133685136323659))<br>
<br>
I leave the precise format of the output unstated so you're free to<br>
choose a clever representation; your answer should be at least as<br>
visually clear as the above.<br>
<br>
You should not mutate the original list, since it may be necessary for<br>
other computations.<br>
<br>
Smallest/tightest/cleanest/best?<br>
<br>
Shriram<br>
<br>
PS: Of course, this is to explore Benford's Law:<br>
<br>
<a href="http://en.wikipedia.org/wiki/Benford's_law" target="_blank">http://en.wikipedia.org/wiki/Benford's_law</a><br>
<br>
which is the subject of my lecture tomorrow. The above<br>
distribution is from the size of the littoral zone (in acres) of<br>
the lakes of Minnesota.<br>
<br>
PPS: If you really want to you can assume the data are actually in a<br>
CSV file (as mine are -- thanks, Neil!), in case you can come up<br>
with something even cleverer.<br>
_________________________________________________<br>
For list-related administrative tasks:<br>
<a href="http://lists.racket-lang.org/listinfo/users" target="_blank">http://lists.racket-lang.org/listinfo/users</a><br>
</blockquote></div><br></div>