Reputation: 165
i want to write a function for building a list of random numbers and here comes the code ive written
buildlist :: Int -> Int -> [Int]
buildlist n m = do
seed <- getStdGen
let l = randomRs (0, m) seed
let list = take n l
return list
and then the errors
Couldn't match expected type `[t0]' with actual type `IO StdGen'
In a stmt of a 'do' block: seed <- getStdGen
In the expression:
do { seed <- getStdGen;
let l = randomRs ... seed;
let list = take n l;
return list }
In an equation for `buildlist':
buildlist n m
= do { seed <- getStdGen;
let l = ...;
let list = ...;
.... }
ps.haskell is so different from c,java,ruby that i feel i've nerver learnt coding
Upvotes: 4
Views: 1008
Reputation: 8104
Because you are using IO (getStdGen
), the whole function must be in IO
monad. Change the return type to
buildList :: Int -> Int -> IO [Int]
and do read a good book :-)
Upvotes: 6