mercredi 11 mai 2022

random number generator function returns a nested tuple in haskell

I am trying to understand why my haskell function returns a nested tuple, and I cannot seem to wrap my head around the problem.

I have this function that generates a random binary Int

test :: StdGen -> (Int, StdGen)
test g = do
  let (i, g') = randomR (0, 1) g
  return (i, g')

However, I cannot compile this unless I change the function to return a nested tuple, like so:

test :: StdGen -> (Int, (Int, StdGen))

If I do so, I have to get the snd element in the returned tuple, to get the desired result:

g = mkStdGen 5
(i, g') = snd test g

How do I make my test function return a single tuple with the random binary and a new generator (i, g)? What am I missing?




Aucun commentaire:

Enregistrer un commentaire