'How do I deal with the error "No instance for (Control.Monad.IO.Class.MonadIO [])"?

Good evening everybody! This is a question concerning Haskell. I want to get x random elements from a list by using a function.

The problem I get is that when I try to use random numbers, with randomRIO. I get the error message:

No instance for (Control.Monad.IO.Class.MonadIO [])
        arising from a use of `randomRIO'

This error message suddenly goes away when i use print or return. But I dont want to use print, and return messes up the output to a nested list [[a]] instead of [a].

Does any of you have a tip on what I can do to extract x random elements from the list, in the form of a list?

The type would be something like this. xRanElems :: [a] -> Int -> [a] where the second Int is an accumulator but I got that covered.

xRanElems xs n = do
  r <- randomRIO (0, n)
  xRanElems2 xs n r

where n is just length xs - 1

xRanElems2 xs n r = (xs !! r) : (xRanElems (xsWithOutSelectedElem xs r) (n-1))

Thankful for any input!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source