given following code
rolldie :: generatorstate int rolldie = generator <- let (value, newgenerator) = randomr (1,6) generator put newgenerator return value
i know can translate as:
rolldie2 :: generatorstate int rolldie2 = >>= \generator ->let (value, newgenerator) = randomr(1,6) generator in put newgenerator >> return value
i tested both functions , without put newgenerator >>
, , produce different results. question why? put
functions pure, , operator (>>
) means return value
should unaffected prior results.
when test both functions with same initial state, same answer:
λ> evalstate rolldie (mkstdgen 0) 6 λ> evalstate rolldie2 (mkstdgen 0) 6
i suspect you're not using same state both tests. how testing functions?
here's example state (i.e. random number generator) gets modified:
test :: generatorstate (int, int) test = <- rolldie -- modifies state! b <- rolldie2 -- gets different state return (a, b) runtest :: io () runtest = g <- getstdgen let (a, b) = evalstate test g print print b
as can see, when run 2 different answers.
λ> runtest 4 2
Comments
Post a Comment