4

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

1 Answer 1

6

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 :-)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot.it's hard to buy a book about haskell in china. and english website is hard to understand. T_T
There are two excellent books that you can read online: book.realworldhaskell.org/read and learnyouahaskell.com. Unfortunately, I'm not aware of any materials in Chinese, but if you speak any other languages, check haskell.org/haskellwiki/Books and haskell.org/haskellwiki/Tutorials. Translations of tutorials in other languages do appear from time to time, so keep checking those sites. And perhaps you'll translate one of the tutorials into your language!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.