Guest User

Landsburg code

a guest
Jan 1st, 2011
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. --compile as ghc -threaded --make $file.hs -o boygirl
  4. --run as ./boygirl [# of sims] [# of families] +RTS -N[# of CPUS]
  5.  
  6. import System.Environment(getArgs)
  7. import System.Random(newStdGen,randoms)
  8.  
  9. main :: IO ()
  10. main = do
  11.   g <- newStdGen
  12.   (c:n:_) <- getArgs
  13.   printResults (read c) (read n) (randoms g)
  14.  
  15. runSim :: Int -> Int -> [Bool] -> Double -> Double -> (Double, Double)
  16. runSim c n bs gx bx = let (g,b,restbs) = howmany n bs 0 0 in
  17.   if c == 0 then (gx+(avgUp g b),bx+(avgUp b g)) else runSim (c-1) n restbs (gx+(avgUp g b)) (bx+(avgUp b g))
  18.  
  19. avgUp :: Int -> Int -> Double
  20. avgUp x y = x' / (x'+y') where
  21.  x' = fromIntegral x
  22.   y' = fromIntegral y
  23.  
  24. printResults c n bs = let (gx,bx) = runSim c n bs 0 0
  25.                          c' = fromIntegral c in
  26.                       do
  27.                       putStrLn $ "Sims: " ++ (show c)
  28.                       putStrLn $ "Avg Girls Fraction: " ++ (show (gx/ c'))
  29.                      putStrLn $ "Avg Boys Fraction: " ++ (show (bx / c'))
  30.                      
  31. howmany :: Int -> [Bool] -> Int -> Int -> (Int,Int,[Bool])
  32. howmany  n rs grrls bois  | n == 0 = (grrls,bois,rs)
  33.                           | (head rs) = howmany n (tail rs) (grrls+1) bois
  34.                           | not (head rs) = howmany (n-1) (tail rs) grrls (bois+1)
Advertisement
Add Comment
Please, Sign In to add comment