vendredi 24 novembre 2017

getRandom hangs the second time around. Why?

The following code hangs when it get to the second runRand. Why?

import Control.Monad.Random (Rand, getRandom, runRand)
import System.Random        (RandomGen, mkStdGen)

rgen :: (RandomGen g) => Rand g [Int]
rgen = do
    r <- sequence (repeat getRandom)
    return $ take 5 r

main = do
  let g0 = mkStdGen 0
      (i,g1) = runRand rgen g0 
  print i

  print "one done"

  let (j,_) = runRand rgen g1 
  print j

Based on the answer to this question : Infinite random sequence loops with randomIO but not with getRandom I would have expected the lazy nature of getRandom to allow this program to terminate.




Aucun commentaire:

Enregistrer un commentaire