lundi 14 octobre 2019

Trim an infinite list of random numbers

I'm trying to trim a list of random numbers so that sum of the numbers in the resulting smaller list accumultates to under a value of 1.

This is interesting in a sense that average of these list prefix lengths is e, somehow.

Getting the length of prefix got a problem - I managed to get it work it on a determined infinite list, or a slice of random list, but the program hangs on infinite random list. What am I doing wrong?

import System.Random

-- Count list items that accumulate to 1.
count :: (Num a, Ord a) => [a] -> Int
count xs = 1 + length xs' 
         where xs'= takeWhile (< 1) $ scanl1 (+) xs

-- Works of infinite list 
a = (return (repeat 0.015)) :: IO [Double]
fa = fmap count a 
--67 

-- Works on trimmed list of randoms
rio = randomRIO (0::Double, 1)         
b = sequence $ take 10 (repeat rio)
fb = fmap count b 
-- fb is usually 2 to 5

-- Hangs on infinite list of randoms
c = sequence (repeat rio)
fc = fmap count c
-- fc hangs... ;(



Aucun commentaire:

Enregistrer un commentaire