Code is below. There's something stupidly verbose about the way I extract the first digit, so suggestions are welcome.<br><br>(define (count-firsts d)<br> (let ((c (make-vector 9 0)))<br> (let loop ((d d))<br> (if (null? d)<br>
(let* ((cl (vector->list c))<br> (sum (exact->inexact (apply + cl))))<br> (map list<br> (build-list 9 add1)<br> (map (lambda (x) (/ x sum)) cl)))<br> (let* ((first (string->number<br>
(list->string<br> (list<br> (car (string->list<br> (number->string (car d))))))))<br> (ind (sub1 first)))<br> (vector-set! c ind (add1 (vector-ref c ind)))<br>
(loop (cdr d)))))))<br><br><br><div class="gmail_quote">On Thu, Oct 14, 2010 at 12:10 AM, 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: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); 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%27s_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>