vendredi 29 mars 2019

let vs do in monadic values

What's the difference in using let or do when trying to work with monadic values? (not sure if this is the right way to phrase it)

For example:

*Main> dupRand = let i = randomRIO (1, 10) in sequence [i, i]
*Main> :t dupRand
dupRand :: (Random a, Num a) => IO [a]
*Main> dupRand
[8,3]
*Main> dupRand
[2,9]
*Main> dupRand
[9,5]

*Main> dupRand2 = do i <- randomRIO (1, 10); sequence [i, i]
*Main> :t dupRand2
dupRand2 :: (Random a, Num a) => IO [a]
*Main> dupRand2
[6,6]
*Main> dupRand2
[9,9]
*Main> dupRand2
[5,5]

why is it that in dupRand2, the function successfully duplicates a random value, whereas in dupRand, the function just seems like it generates two random values?




Aucun commentaire:

Enregistrer un commentaire