[Haskell-cafe] working with Random.randoms
Stephen Howard
stephen at enterity.com
Fri Jun 13 12:38:25 EDT 2008
Hi List,
I am a newcomer doing my obligatory struggling with Haskell's type
system, and I've got a nut I've not been able to crack. Given:
import Random
random_test :: Int -> String
random_test n = do
g <- getStdGen
take n (randoms g)::String
I'm expecting that I ought to be able to pass this action an integer and
get back a random string that long (given, not all characters may be
printable).
But GHCI tells me:
RandomTest.hs:7:4:
Couldn't match `[]' against `IO'
Expected type: []
Inferred type: IO
In a 'do' expression: g <- getStdGen
In the definition of `random_test':
random_test n = do
g <- getStdGen
take n (randoms g) :: String
And yet, when I run these lines in GHCI by hand, things seem to work
(though the string is the same set of random characters each time,
another bit that I need to solve):
Prelude> :module Random
Prelude Random> g <- getStdGen
Prelude Random> take 5 (randoms g)::String
"\1025049\315531\882767\1032009\334825"
I'm guessing that randoms is returning an IO type but I'm not sure how
to go about extracting the String to return to the calling action.
Changing the type signature to Int -> IO String only gives me a
different error.
Where am I going wrong?
thanks,
Stephen
More information about the Haskell-Cafe
mailing list