[Haskell-cafe] Generate 50 random coordinates

Jason Dagit dagit at codersbase.com
Sat Dec 2 02:41:05 EST 2006


On 12/1/06, Huazhi (Hank) Gong <hankgong at nm.gist.ac.kr> wrote:
> Hello,all
> My intention is to generate 50 random coordinates like (x,y).
> myrand :: Int
> myrand = randomRIO(1::Int, 100)

When we look at the type of randomRIO we see:
randomRIO :: forall a. (Random a) => (a, a) -> IO a

You're giving it a tuple of Int, so we can substitute Int for 'a' in
that type signature:
myrand :: IO Int

> rf=[(myrand, myrand) | a <- [1..50]]

Here you are creating a list of tuples.  We see from above that the
type of the tuples would be (IO Int, IO Int), so rf :: [(IO Int, IO
Int)].  This is because we have not run the IO action to generate the
Int yet.

> My short program is like this. However, GHCI say that the return type of
> randomRIO is IO a while the function defined by me is Int. Since I only need
> a integral type as my cooridinate, could you tell me how to fix this?

Your type signature tries to make a claim that myrand has type Int,
but the compiler will disagree because of that pesky IO type.

Yet Another Haskell Tutorial[1] seems to have a section on the pesky IO type.

[1] http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf

I hope that helps,

More information about the Haskell-Cafe mailing list