samedi 15 octobre 2016

Haskell: Random Coin Instance

I have defined a Coin data type:

data Coin = H | T
    deriving (Bounded, Eq, Enum, Ord, Show)

I now have to write a Random Coin instance, given the following framework:

instance Random Coin where
randomR (l, h) g = undefined
random           = undefined

Obviously, this random instance should return either H or T. I'm beginning to start understanding how Monads work, however, I'm confused about the initial random generator. I get that a random generator returns a (a, gen), but where do we get the initial generator to create a first random Coin? I currently have the following:

instance Random Coin where
randomR (l, h) g = case randomR(l, h) g of
                   (c, g') -> (c, g')
random           = getStdRandom(randomR(minBound :: Coin, maxBound :: Coin))

I'm especially confused about the random function, since particular expression seems to return type IO Coin. Any help to enlighten me is much appreciated!




Aucun commentaire:

Enregistrer un commentaire