
Hello. 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. https://signup.live.com/signup.aspx?id=60969

data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum) randomFigure :: IO FigType randomFigure = toEnum <$> randomRIO (1,11) Bob On 14 Mar 2010, at 10:57, Evgenij Merenkov wrote:
Hello.
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. Sign up now. _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thank you. Subject: Re: [Haskell-beginners] Randomly selecting a data constructor From: tom.davie@gmail.com Date: Sun, 14 Mar 2010 11:08:59 +0000 CC: beginners@haskell.org To: evgenij1@hotmail.com data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum) randomFigure :: IO FigTyperandomFigure = toEnum <$> randomRIO (1,11) Bob _________________________________________________________________ Your E-mail and More On-the-Go. Get Windows Live Hotmail Free. https://signup.live.com/signup.aspx?id=60969

Thomas Davie wrote:
data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum) randomFigure :: IO FigType randomFigure = toEnum <$> randomRIO (1,11)
You actually want randomRIO (0,10). If you don't want to have to remember that, or to remember to change "10" whenever the number of constructors changes, you can use:
data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum, Bounded) randomFigure :: IO FigType randomFigure = toEnum <$> randomRIO (fromEnum (minBound :: FigType), fromEnum maxBound)
Regards, Yitz

Thank you. Didn't know that you can supply type directly to a function, like "f :: Type".
From: gale@sefer.org Date: Sun, 14 Mar 2010 13:44:39 +0200 Subject: Re: [Haskell-beginners] Randomly selecting a data constructor To: tom.davie@gmail.com CC: beginners@haskell.org; evgenij1@hotmail.com
Sorry, hit "Send" too soon:
data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum, Bounded) randomFigure :: IO FigType randomFigure = toEnum <$> randomRIO (fromEnum (minBound :: FigType), fromEnum (maxBound :: FigType))
-Yitz _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_________________________________________________________________ Your E-mail and More On-the-Go. Get Windows Live Hotmail Free. https://signup.live.com/signup.aspx?id=60969

On Sun, Mar 14, 2010 at 12:53:41PM +0000, Evgenij Merenkov wrote:
Thank you. Didn't know that you can supply type directly to a function, like "f :: Type".
Yes, you can always put a type annotation on any value, it just tells the compiler what type it should be. In this case (minBound :: FigType) is to help out the compiler's type inference, since otherwise it would not know what type minBound should be (it could be any type which is an instance of Bounded). -Brent
From: gale@sefer.org Date: Sun, 14 Mar 2010 13:44:39 +0200 Subject: Re: [Haskell-beginners] Randomly selecting a data constructor To: tom.davie@gmail.com CC: beginners@haskell.org; evgenij1@hotmail.com
Sorry, hit "Send" too soon:
data FigType = TR1 | TR2 | TR3 | TR4 | SQ | L1 | L2 | Z1 | Z2 | Z3 | Z4 | NoFigure deriving (Enum, Bounded) randomFigure :: IO FigType randomFigure = toEnum <$> randomRIO (fromEnum (minBound :: FigType), fromEnum (maxBound :: FigType))
-Yitz _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_________________________________________________________________ Your E-mail and More On-the-Go. Get Windows Live Hotmail Free. https://signup.live.com/signup.aspx?id=60969 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Brent Yorgey
-
Evgenij Merenkov
-
Thomas Davie
-
Yitzchak Gale