[Haskell-beginners] Re: Random animals

Amy de Buitléir amy at nualeargais.ie
Wed Nov 17 16:48:41 EST 2010


aditya siram <aditya.siram <at> gmail.com> writes:

> 
> Does this help?
> http://www.haskell.org/haskellwiki/99_questions/Solutions/24
> -deech

Indeed it does, thank you deech. For the benefit of anyone who googles this, 
here's a solution that works.

import "mtl" Control.Monad.State
import System.Random

-- | Choose an element at random from a list and return the element and its index
randomListSelection :: [a] -> IO (Int, a)
randomListSelection xs = do
  i <- randomRIO (0,(length xs)-1)
  return (i, xs !! i)

chooseCreatur :: IO (Int, [Char])
chooseCreatur = randomListSelection ["cat", "dog", "lion", "mouse"]



More information about the Beginners mailing list