Re: [racket] a small programming exercise

2010-10-15 Thread namekuseijin
On Fri, Oct 15, 2010 at 10:05 AM, Phil Bewig wrote: > Not quite. > Random numbers are uniformly distributed, so the first digits of a set of > random numbers should all appear equally. > Benford's Law most often applies to sets of naturally-occurring numbers that > are scale-invariant.  Consider t

Re: [racket] a small programming exercise

2010-10-15 Thread Jos Koot
_ From: Phil Bewig [mailto:pbe...@gmail.com] Sent: 15 October 2010 15:06 To: Jos Koot Cc: Chris Stephenson; users@racket-lang.org Subject: Re: [racket] a small programming exercise Not quite. Random numbers are uniformly distributed, so the first digits of a set of random numbers

Re: [racket] a small programming exercise

2010-10-15 Thread Phil Bewig
Message- > > From: users-boun...@racket-lang.org > > [mailto:users-boun...@racket-lang.org] On Behalf Of Chris Stephenson > > Sent: 15 October 2010 11:13 > > To: users@racket-lang.org > > Subject: Re: [racket] a small programming exercise > > > snip > > >

Re: [racket] a small programming exercise

2010-10-15 Thread Jos Koot
> -Original Message- > From: users-boun...@racket-lang.org > [mailto:users-boun...@racket-lang.org] On Behalf Of Chris Stephenson > Sent: 15 October 2010 11:13 > To: users@racket-lang.org > Subject: Re: [racket] a small programming exercise > snip > > Thin

Re: [racket] a small programming exercise

2010-10-15 Thread Jos Koot
Stupid me, for I did already read about Benford's law. Thanks. Jos > -Original Message- > From: users-boun...@racket-lang.org > [mailto:users-boun...@racket-lang.org] On Behalf Of Chris Stephenson > Sent: 15 October 2010 11:13 > To: users@racket-lang.org > Subjec

Re: [racket] a small programming exercise

2010-10-15 Thread Chris Stephenson
On 15/10/10 11:33, Jos Koot wrote: > When taking a long list of pseudo random positive integers most of which are > far greater than the base, I expect about the same frequency for each first > digit from 1 to base-1. This seems to hold if the base is a power of 10, but > for other bases, e.g. base

Re: [racket] a small programming exercise

2010-10-15 Thread Jos Koot
When taking a long list of pseudo random positive integers most of which are far greater than the base, I expect about the same frequency for each first digit from 1 to base-1. This seems to hold if the base is a power of 10, but for other bases, e.g. base 24, I get rather unexpected results. See p

Re: [racket] a small programming exercise

2010-10-14 Thread Eli Barzilay
9 hours ago, Robby Findler wrote: > On Thu, Oct 14, 2010 at 11:09 AM, Matthias Felleisen > wrote: > > 2. But could DrRacket execute RUN by > > > >  -- spawning a process that performs the compilation of the def window > >  -- printing the repl prompt and allowing users to type > >  -- and insertin

Re: [racket] a small programming exercise

2010-10-14 Thread Robby Findler
On Thu, Oct 14, 2010 at 11:09 AM, Matthias Felleisen wrote: > 2. But could DrRacket execute RUN by > >  -- spawning a process that performs the compilation of the def window >  -- printing the repl prompt and allowing users to type >  -- and inserting results (if any) as they become available from

Re: [racket] a small programming exercise

2010-10-14 Thread Robby Findler
Remainder is probably the one you want. Robby On Thursday, October 14, 2010, Joe Marshall wrote: > Here's an interesting data point. > > (define (first-digit-fast-1 n) >  (if (> 10 n) >     n >     (let loop ((i 10) >                (next 100)) >       (if (> next n) >           (first-digi

Re: [racket] a small programming exercise

2010-10-14 Thread Joe Marshall
Here's an interesting data point. (define (first-digit-fast-1 n) (if (> 10 n) n (let loop ((i 10) (next 100)) (if (> next n) (first-digit-fast (floor (/ n i))) (loop next (* i i)) This takes about 13 seconds on my work machine. If I tak

Re: [racket] a small programming exercise

2010-10-14 Thread Joe Marshall
On Thu, Oct 14, 2010 at 3:19 PM, Justin Zamora wrote: > On Thu, Oct 14, 2010 at 2:26 PM, Joe Marshall wrote: >> Here's a fast way to get the leftmost digit of a number: >> ... >> This version is slightly slower for small numbers, but much much better >> for large ones: >> (defun leftmost-digit (n

Re: [racket] a small programming exercise

2010-10-14 Thread Justin Zamora
On Thu, Oct 14, 2010 at 2:26 PM, Joe Marshall wrote: > Here's a fast way to get the leftmost digit of a number: > ... > This version is slightly slower for small numbers, but much much better > for large ones: > (defun leftmost-digit (n base) > (if (> base n) > n > (do* ((i base next) >

Re: [racket] a small programming exercise

2010-10-14 Thread Sam Tobin-Hochstadt
On Thu, Oct 14, 2010 at 2:35 PM, Matthias Felleisen wrote: > Question is whether we can learn from your rewrite how to write a Guide on > Type Conversion for expert Racket programmers. -- Matthias There's only 1 entry in the Guide from that rewrite: - Whenever you get an error involving a polym

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
Question is whether we can learn from your rewrite how to write a Guide on Type Conversion for expert Racket programmers. -- Matthias _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users

Re: [racket] a small programming exercise

2010-10-14 Thread Sam Tobin-Hochstadt
On Thu, Oct 14, 2010 at 10:22 AM, Jay McCarthy wrote: > I wrote mine in Racket then converted to Typed Racket. Attached is a different version of Jay's original code, ported to Typed Racket. There are 4 changes to the runtime code: 1. use `vector-set!' and `vector-ref' instead of `dict-update!'

Re: [racket] a small programming exercise

2010-10-14 Thread Joe Marshall
Here's a fast way to get the leftmost digit of a number: (defun leftmost-digit (n base) (do* ((i 1 next) (next base (* i base))) ((> next n) (floor n i (This is in Common Lisp. DO* is to DO as LET* is to LET. Floor takes two integer arguments and returns the floor of the le

Re: [racket] a small programming exercise

2010-10-14 Thread namekuseijin
On Thu, Oct 14, 2010 at 2:10 PM, Vincent St-Amour wrote: > At Thu, 14 Oct 2010 13:56:16 -0300, > namekuseijin wrote: >> (all-d (filter ns (lambda (n) (char=? n d >> (all-but-d (filter ns (lambda (n) (not (char=? n d)) > > Sounds like a job for partition. > > http://doc.racket-lang

Re: [racket] a small programming exercise

2010-10-14 Thread Vincent St-Amour
At Thu, 14 Oct 2010 13:56:16 -0300, namekuseijin wrote: > (all-d (filter ns (lambda (n) (char=? n d > (all-but-d (filter ns (lambda (n) (not (char=? n d)) Sounds like a job for partition. http://doc.racket-lang.org/reference/pairs.html?q=partition#%28def._%28%28lib._racket/list..

Re: [racket] a small programming exercise

2010-10-14 Thread Nadeem Abdul Hamid
Oh yeah, duh. On Thu, Oct 14, 2010 at 12:48 PM, Matthias Felleisen wrote: > > Because (require racket) provides everything you'd ever want. > > > On Oct 14, 2010, at 12:39 PM, Nadeem Abdul Hamid wrote: > >>> Thanks for setting my head straight! >> >> >> Oh, I wasn't meaning to do that! :) >> >>

Re: [racket] a small programming exercise

2010-10-14 Thread namekuseijin
On Thu, Oct 14, 2010 at 1:10 AM, Shriram Krishnamurthi wrote: > Given: A list of numbers.  Assume integers (though it isn't necessary > for the underlying issue). > > Task: To determine how often the first digit is 1, the first digit is > 2, ..., the first digit is 9, and present as a table.  You

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
Because (require racket) provides everything you'd ever want. On Oct 14, 2010, at 12:39 PM, Nadeem Abdul Hamid wrote: >> Thanks for setting my head straight! > > > Oh, I wasn't meaning to do that! :) > > Incidentally, why does your solution run even when the language level > is set to "Inte

Re: [racket] a small programming exercise

2010-10-14 Thread Robby Findler
You might try disabling errortrace and see if that helps (it can help a lot with redex programs). Robby On Thu, Oct 14, 2010 at 11:30 AM, Shriram Krishnamurthi wrote: > Why doesn't that impact regular Run?  Because all the things that look > normal (define, cond, etc.) are going through many, m

Re: [racket] a small programming exercise

2010-10-14 Thread Sam Tobin-Hochstadt
On Thu, Oct 14, 2010 at 12:18 PM, Shriram Krishnamurthi wrote: > It would seem TR should not take much longer than regular Run. There are a lot of things that make TR much slower than regular compilation. 1. `local-expand' makes everything slower. 2. TR necessarily involves multiple passes over

Re: [racket] a small programming exercise

2010-10-14 Thread Nadeem Abdul Hamid
> Thanks for setting my head straight! Oh, I wasn't meaning to do that! :) Incidentally, why does your solution run even when the language level is set to "Intermediate Student"? In fact, even mine runs in ISL while I use lambda. I'm running DrRacket, version 5.0.1.7--2010-10-06. --- nadeem O

Re: [racket] a small programming exercise

2010-10-14 Thread Robby Findler
On Thu, Oct 14, 2010 at 11:20 AM, Matthias Felleisen wrote: > > Yes, macro expansion is slow. -- Matthias So is the coloring that check syntax does, tho. Robby _ For list-related administrative tasks: http://lists.racket-lang.org/listinfo/users

Re: [racket] a small programming exercise

2010-10-14 Thread Shriram Krishnamurthi
Why doesn't that impact regular Run? Because all the things that look normal (define, cond, etc.) are going through many, many more layers of macros provided by TR? Is there a non-linear expansion? Shriram On Thu, Oct 14, 2010 at 12:20 PM, Matthias Felleisen wrote: > > On Oct 14, 2010, at 12:1

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
Thanks for setting my head straight! I ran this stress test for your 5 line solution and mine: > (define *the-list* (build-list 1000 (lambda (i) (random (+ i 1) > (time (__ *the-list*)) For small benchmarks, your solution may take twice as long as my 8 line ASL solution, but fo

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
On Oct 14, 2010, at 12:18 PM, Shriram Krishnamurthi wrote: > It would seem TR should not take much longer than regular Run. (I'm > not aware of it performing any sophisticated non-linear > interprocedural computations, which would serve as an *intrinsic* > cause for significant delay.) I presu

Re: [racket] a small programming exercise

2010-10-14 Thread Shriram Krishnamurthi
It would seem TR should not take much longer than regular Run. (I'm not aware of it performing any sophisticated non-linear interprocedural computations, which would serve as an *intrinsic* cause for significant delay.) I presume this gap is caused by what you're calling the absence of an "optimi

Re: [racket] a small programming exercise

2010-10-14 Thread Nadeem Abdul Hamid
Here's a solution in ISL: ;; benson : [listof positive-number] -> [listof (list number number)] (define (benson a-lon) (let ([digs (map (compose string->number first explode number->string exact->inexact) a-lon)]) (build-list 10 (λ(i) (list i

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
On Oct 14, 2010, at 12:03 PM, Shriram Krishnamurthi wrote: > (Google found that a half-second delay led to a 20% loss inb user traffic). 1. At some point DrRacket will provide 'optimistic make' and the problem will go away, for some value of 'optimistic' and 'away'. 2. But could DrRacket exe

Re: [racket] a small programming exercise

2010-10-14 Thread Shriram Krishnamurthi
Speaking as a user of TR, I would really far prefer that the compilation time shrank before there were more optimizations. The *perceived* time between hitting F5 and getting a prompt is huge. In my class, when I run a typed program, there are nervous titters and jokes about how long it's going to

Re: [racket] a small programming exercise

2010-10-14 Thread Jay McCarthy
I timed the process not the function to account for loading the typed runtime. Jay Sent from my iPhone On Oct 14, 2010, at 9:51 AM, Sam Tobin-Hochstadt wrote: > On Thu, Oct 14, 2010 at 10:22 AM, Jay McCarthy wrote: >> I wrote mine in Racket then converted to Typed Racket. > Attached is a dif

Re: [racket] a small programming exercise

2010-10-14 Thread Matthias Felleisen
On Oct 14, 2010, at 10:57 AM, Justin Zamora wrote: > Since Shriram seemed to be encouraging cleverness in representation, I > submit the following solution, which assumes the inputs and outputs > are in binary. > > (define (benford l) > '(1 1.0)) > > Justin > _

Re: [racket] a small programming exercise

2010-10-14 Thread Justin Zamora
Since Shriram seemed to be encouraging cleverness in representation, I submit the following solution, which assumes the inputs and outputs are in binary. (define (benford l) '(1 1.0)) Justin _ For list-related administrative tasks: http:/

Re: [racket] a small programming exercise

2010-10-14 Thread Justin Zamora
Mine's basically the same as everyone else's, except I find the first digit using math instead of string conversion. #lang racket ;; Compute the base-10 logarithm of a number (define (log10 x) (/ (log x) (log 10))) ; Compute the first (base-10) digit of a number (define (first-digit n) (inex

Re: [racket] a small programming exercise

2010-10-14 Thread Jay McCarthy
I wrote mine in Racket then converted to Typed Racket. Here is the final version: http://github.com/jeapostrophe/exp/blob/master/benford.rkt The changes to make it typed are pretty severe: http://github.com/jeapostrophe/exp/commit/9487a13344360c93bf63a7859a2336a074f43cae I had to drop using di

Re: [racket] a small programming exercise

2010-10-14 Thread Phil Bewig
This works, assuming xs consists of positive integers: (define (benford xs) (for-each (lambda (x) (printf "~a ~f~%" (car x) (/ (cdr x) (length xs (uniq-c = (sort < (map (compose car digits) xs) Compose, digits and uniq-c are from my Standard Prelude

Re: [racket] a small programming exercise

2010-10-14 Thread Noel Welsh
The issues are: - Converting characters to integers. My solution may not work in all encodings - Storing the counts. I used mutation. N. (define (count-first-digit lst) (define counts (make-vector 10 0)) (for ([elt (in-list lst)]) ;; Relies on the char representation of the numb

Re: [racket] a small programming exercise

2010-10-14 Thread Richard Mittel
Code is below. There's something stupidly verbose about the way I extract the first digit, so suggestions are welcome. (define (count-firsts d) (let ((c (make-vector 9 0))) (let loop ((d d)) (if (null? d) (let* ((cl (vector->list c)) (sum (exact->inexact (apply + cl

[racket] a small programming exercise

2010-10-13 Thread Shriram Krishnamurthi
Given: A list of numbers. Assume integers (though it isn't necessary for the underlying issue). Task: To determine how often the first digit is 1, the first digit is 2, ..., the first digit is 9, and present as a table. You may exploit DrRacket's pretty-printer, e.g., by using a list of lists: