원주율 계산하기

import System.Environment import System.Random -- curried version of inCirc, no need to construct pairs inCirc :: Double -> Double -> Int inCirc x y | dx*dx + dy*dy < 0.25 = 1 | otherwise = 0 where dx = x - 0.5 dy = y - 0.5 -- transform a list of coordinates into a list of indicators -- whether the point is inside the circle (sorry for the -- stupid name) inCircles :: [Double] -> [Int] inCircles (x:y:zs) = inCirc x y : inCircles zs inCircles _ = [] -- given a count of experiments and an infinite list of coordinates, -- calculate an approximation to pi calcPi :: Int -> [Double] -> Double calcPi n ds = fromIntegral ct / fromIntegral n * 4 where ct = sum . take n $ inCircles ds -- now the IO part is only -- * getting the number of experiments and -- * getting the StdGen main :: IO () main = do args <- getArgs sg <- getStdGen let n = case args of (a:_) -> read a _ -> 10000 print $ calcPi n (randomRs (0,1) sg)