Since you're using IO inside choseRandom
you need to change the type signature:
choseRandom :: [a] -> IO a
secondly, you don't need to use >>=
to get the length of the list. >>=
has type
Monad m => m a -> (a -> m b) -> m b
The type of length
is [a] -> Int
, so the type of length list
is Int
, which is not a monad.
You can calculate it directly when calling randomInt
:
choseRandom :: [a] -> IO a
choseRandom list =
randomInt(0, length list) >>= (\num -> return (list !! num))
which is the same as
choseRandom :: [a] -> IO a
choseRandom list = fmap (\num -> (list !! num)) (randomInt(0, length list))