3

I am developing in Haskell and I had to use System.Random.

Now I have a IO [Int] and I would like to parse it to get the Int out of it and then use it to perform an action on a list.

I did:

function :: IO [Int] -> [a] -> a
function (x:xs) list = (list !! x)

But I get an error on (x:xs):

 Couldn't match expected type 'IO [Int]' with actual type '[a0]'.

How do I parse the IO [Int] to use the Int inside?

4
  • 2
    You can not unwrap a value out of an IO. Commented Apr 7, 2021 at 20:33
  • 1
    see stackoverflow.com/q/36729022/67579 Commented Apr 7, 2021 at 20:34
  • 1
    @WillemVanOnsem so there is no way i can use these value ? But then I don't understand how can you use random in Haskell outside of the main to print. I can't do calcul with random since he only return a IO Int ? Commented Apr 8, 2021 at 9:00
  • 1
    @Hrothgor You cannot unwrap an IO thing to pass on to a pure consumer. But you can wrap a pure consumer to accept IO things (and return IO things), with fmap or (>>=) depending on whether the function returns an IO thing or not. Commented Apr 8, 2021 at 20:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.