以下代码在到达第二个runRand时挂起。为什么呢?
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
根据这个问题的答案:Infinite random sequence loops with randomIO but not with getRandom我原本期望getRandom的惰性允许这个程序终止。