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.
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
>
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
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) (
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 <
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
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) (
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 <-
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
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,
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
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
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
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
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
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
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
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
>
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
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
20 matches
Mail list logo