[Haskell-beginners] Random Generator

Brandon Allbery allbery.b at gmail.com
Wed Apr 1 15:19:47 UTC 2015


On Wed, Apr 1, 2015 at 11:08 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> myrandoms :: (RandomGen g, Random a) => g -> [a]
> myrandoms gen = let (value, newGen) = random gen in value:myrandoms
> (mkStdGen (value::Int))
>

You have declared a function that says that it can deal with any type `a`
that the *caller* chooses, then provided an implementation that only
supports Int.

Note that :: does not do conversion (as you said "Even though I am
converting my 'value' parameter to Int"); it declares that the type of
`value` *is* Int. Other types will be inferred to match, and this fails at
`value:` in a context which wants the type to be a caller-specified `a`,
not Int.

(I don't think you can coerce an unknown type `a` to Int given only the
context `Random a`. You must find a different way to implement this.)

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150401/daf8b62f/attachment.html>


More information about the Beginners mailing list