LINUX.ORG.RU

История изменений

Исправление Aswed, (текущая версия) :

Проблема была в replicateM


{-# LANGUAGE BangPatterns #-}
import System.Random
import Data.List
import Control.Monad
import Control.DeepSeq

randomNorm :: IO Double
randomNorm = do
	u <- randomIO
	v <- randomIO
	let res = (sqrt ((-2) * log u) * cos (2*pi*v))
	res `deepseq` return res

sum' = foldl1' (+)

replicateM' :: Int -> IO Double -> [Double] -> IO [Double]
replicateM' !n !elem !a
	| n == 0 = return a
	| otherwise = elem >>= \e -> replicateM' (n-1) elem (e:a)

main = do
	print =<< (liftM sum' $ replicateM' 1000000 randomNorm [])

Исходная версия Aswed, :

Проблема была в replicateM [haskell] {-# LANGUAGE BangPatterns #-} import System.Random import Data.List import Control.Monad import Control.DeepSeq

randomNorm :: IO Double randomNorm = do u <- randomIO v <- randomIO let res = (sqrt ((-2) * log u) * cos (2*pi*v)) res `deepseq` return res

sum' = foldl1' (+)

replicateM' :: Int -> IO Double -> [Double] -> IO [Double] replicateM' !n !elem !a | n == 0 = return a | otherwise = elem >>= \e -> replicateM' (n-1) elem (e:a)

main = do print =<< (liftM sum' $ replicateM' 1000000 randomNorm []) [/haskell]