Learn You a Haskell presents the randoms function.

I implemented it as follows:

-- randoms that takes a generator and returns an infinite sequence 
-- of values based on that generator
import System.Random

randoms' :: StdGen -> [Int]
randoms' g = num : randoms' gen
   where (num, gen) = random g 

Please critique it.


Straight forward. Easy to follow. But this is clearly some kind of fold.

randoms' = unfoldr (Just . random)

works nicely.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.