[Haskell-cafe] approximating pi

Derek Elkins derek.a.elkins at gmail.com
Sun Apr 27 21:05:06 EDT 2008

On Mon, 2008-04-28 at 02:54 +0200, jerzy.karczmarczuk at info.unicaen.fr
> Don Stewart writes: 
> > Ry Dahl:
> >> By picking points randomly from a square one can calculate pi.
> ...
> >> What is the best way to express this algorithm in Haskell?
> > 
> > Using a random generator, such as System.Random or
> > System.Random.Mersenne, generate random numbers in your range,
> > testing if they're inside the circle, and loop until your limit
> > condition is reached. 
> > 
> > The full program is about 10 lines or so, so shouldn't be too hard to
> > work out, once you've worked out how to generate Doubles from
> > System.Random.random
> Ry Dahl seems to know Ruby. Ruby has also comprehensions. So, why not use
> 'randoms' to generate an infinite list of them, 'take' some N, and then
> 'sum' 1 on randoms filtered by the circle condition. I think that you
> won't need full 10 lines of code... 

Indeed.  I just stuck in one line into lambdabot to do this.  As an
actual executable and more nicely factored/laid out I'd say it would
probably be about 8 wc -l lines (that is everything, the module
declarations, imports, blank lines, etc.)

More information about the Haskell-Cafe mailing list