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?

Hrothgor
  • 53
  • 5
  • 2
    You can not unwrap a value out of an `IO`. – Willem Van Onsem Apr 07 '21 at 20:33
  • 1
    see https://stackoverflow.com/q/36729022/67579 – Willem Van Onsem Apr 07 '21 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 ? – Hrothgor Apr 08 '21 at 09: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. – Daniel Wagner Apr 08 '21 at 20:05

0 Answers0