[Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Alex Young
Hi all, I'm new to Haskell, and don't quite understand how IO and lazy evaluation work together yet. In order to improve my understanding, I thought I'd try to knock together a translation of a fairly simple problem I can code up in C, that of calculating pi by throwing random numbers about.

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Tim Chevalier
On 11/5/07, Alex Young <[EMAIL PROTECTED]> wrote: > > randList :: Int -> [IO Int] > randList n = randListTail [] n > > randPairs :: Int -> [(IO Int, IO Int)] > randPairs n = zip (randList n) (randList n) [snip] > > doCountPair :: (IO Int, IO Int) -> IO Int > doCountPair (a, b) = do >x <- a >

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Jonathan Cast
On Mon, 2007-11-05 at 20:11 +, Alex Young wrote: > Hi all, > > I'm new to Haskell, and don't quite understand how IO and lazy > evaluation work together yet. In order to improve my understanding, I > thought I'd try to knock together a translation of a fairly simple > problem I can code up

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Don Stewart
alex: > Hi all, > > import Random > import System.Environment > import List > import Monad > > randMax = 32767 > unitRadius = randMax * randMax > > rand :: IO Int > rand = getStdRandom (randomR (0, randMax)) > > randListTail accum 0 = accum > randListTail accum n = randListTail (rand : accum) (

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Luke Palmer
On Nov 5, 2007 1:30 PM, Jonathan Cast <[EMAIL PROTECTED]> wrote: > > main = do > > Get two standard generators (one per dimension) > > > g0 <- newStdGen > > g1 <- newStdGen > > Get an infinite list of pairs > > > let pairs = [ (x, y) | x <- randoms (-1, 1) g0, > > y <

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread David Roundy
On Mon, Nov 05, 2007 at 01:42:50PM -0700, Luke Palmer wrote: > On Nov 5, 2007 1:30 PM, Jonathan Cast <[EMAIL PROTECTED]> wrote: > > Get an infinite list of pairs > > > > > let pairs = [ (x, y) | x <- randoms (-1, 1) g0, > > > y <- randoms (-1, 1) g1 ] > > This will retur

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Andrew Coppin
Alex Young wrote: rand :: IO Int rand = getStdRandom (randomR (0, randMax)) randListTail accum 0 = accum randListTail accum n = randListTail (rand : accum) (n - 1) randList :: Int -> [IO Int] randList n = randListTail [] n randPairs :: Int -> [(IO Int, IO Int)] randPairs n = zip (randList n) (

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Alex Young
Tim Chevalier wrote: On 11/5/07, Alex Young <[EMAIL PROTECTED]> wrote: randList :: Int -> [IO Int] randList n = randListTail [] n randPairs :: Int -> [(IO Int, IO Int)] randPairs n = zip (randList n) (randList n) [snip] doCountPair :: (IO Int, IO Int) -> IO Int doCountPair (a, b) = do x <-

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Alex Young
Don Stewart wrote: alex: You can replace most of your loops with Data.List functions, and simplify the code overall by threading around a lazy list of randoms, rather than calling into IO all the time: import System.Random import System.Environment import Data.List import Cont

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread David Benbennick
On 11/5/07, David Roundy <[EMAIL PROTECTED]> wrote: > On Mon, Nov 05, 2007 at 01:42:50PM -0700, Luke Palmer wrote: > > let pairs = [ (x,y) | x <- randoms (-1,1) g0 | y <- randoms (-1,1) g1 ] > > Or even better, just don't use list comprehensions, they're confusing: > > let pairs = zip (randoms (-1,

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread ajb
G'day all. Quoting Don Stewart <[EMAIL PROTECTED]>: calculatePi total g = fromIntegral (4*count) / fromIntegral total This is slightly more robust in cases where the multiplication would overflow an Int: calculatePi total g = fromRational ((4*fromIntegral count) % fromIntegra

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-05 Thread Brandon S. Allbery KF8NH
On Nov 5, 2007, at 16:21 , Alex Young wrote: C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>ghc BetterPi.hs C:\Users\Alex\Documents\HaskellLearning\MonteCarlo>main.exe 100 Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize' to increase it. But: C:\Users\Alex\Documents

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Henning Thielemann
On Mon, 5 Nov 2007, Andrew Coppin wrote: > randList :: Int -> IO [Int] > randList n = mapM (\x -> randomRIO (0, randMax)) [1..n] replicateM n (randomRIO (0, randMax)) but it is certainly better to use randomR and wrap it in a State monad ___ Haske

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Yitzchak Gale
Hi Alex, You wrote: > I'm new to Haskell, and don't quite understand how IO and lazy > evaluation work together yet. In order to improve my understanding, I > thought I'd try to knock together a translation of a fairly simple > problem I can code up in C... > Now, I have two questions. The easy

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Jonathan Cast
On 6 Nov 2007, at 6:00 AM, Yitzchak Gale wrote: There is still a problem here - my code (and also all of the previous posters, I think) clobbers the random generator, rendering it unusable for future calculations. In this case that is probably not a problem, but it is a bad habit to get into, a

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Yitzchak Gale
Hi Alex, > I think I would have avoided most of the problems > in the first place. I'm finding the documentation > to be rather undiscoverable at the moment, but I'm > sure it's just a question of learning my way around it. Make sure to keep http://haskell.org/ghc/docs/latest/html/libraries/ on

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Luke Palmer
On Nov 5, 2007 8:11 PM, Alex Young <[EMAIL PROTECTED]> wrote: > {--} > module Main where > > import Random > import System.Environment > import List > import Monad > > randMax = 32767 > unitRadius = randMax * randMax > > rand :: IO Int > rand = getStd

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Don Stewart
lrpalmer: > On Nov 5, 2007 8:11 PM, Alex Young <[EMAIL PROTECTED]> wrote: > > {--} > > module Main where > > > > import Random > > import System.Environment > > import List > > import Monad > > > > randMax = 32767 > > unitRadius = randMax * randMax >

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Yitzchak Gale
I wrote: >> There is still a problem here - my code (and also >> all of the previous posters, I think) clobbers the random >> generator, rendering it unusable for future calculations. >> In this case that is probably not a problem, but it >> is a bad habit to get into, and not very polite. Jonatha

Re: [Haskell-cafe] Monte Carlo Pi calculation (newbie learnings)

2007-11-06 Thread Jonathan Cast
On 6 Nov 2007, at 3:56 PM, Yitzchak Gale wrote: I wrote: There is still a problem here - my code (and also all of the previous posters, I think) clobbers the random generator, rendering it unusable for future calculations. In this case that is probably not a problem, but it is a bad habit to g