[Haskell-beginners] Randomly selecting a data constructor

Evgenij Merenkov evgenij1 at hotmail.com
Sun Mar 14 06:57:22 EDT 2010


I have the following data and it's constructors:

FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure

I need to randomly return one of them in a function. The obvious solution to me, is simply using a random number generator and case statement, where each value generated is associated with each data constructor. Like this:

import System.Random

randomFigType :: IO FigType
randomFigure = do
  x <- randomRIO (1::Int,11::Int)
  return $ case x of
             1 -> TR1
             2 -> TR2
             3 -> TR3
e t.c.

But is there a more direct way to do this?
Hotmail: Trusted email with Microsoft’s powerful SPAM protection.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100314/a3166577/attachment.html

More information about the Beginners mailing list