Re: Random Color

2002-11-21 Thread Dean Herington
On Thu, 21 Nov 2002, Mike T. Machenry wrote:

> This program yeilds:
> Fail: toEnum{Color}: tag (4) is outside of enumeration's range (0,3)
> in ghc... any ideas why?
> 
> -mike

Hmm.  The program works fine for me, using GHC 5.04.1 or Hugs of Oct 2002.

Dean


> On Thu, Nov 21, 2002 at 10:07:12AM -0500, Dean Herington wrote:
> > "Mike T. Machenry" wrote:
> > 
> > > Andrew and list,
> > >
> > >   I am a beginer. I really don't know what I would do if I derived
> > > Color from Enum. You say I could create elements that way. Is there
> > > some simple example someone could post to the list? Thank you for
> > > your help.
> > 
> > Here's one way to generate random values from an enumerable set:
> > 
> > import Random
> > 
> > randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g)
> > randomR_Enum (lo, hi) gen = (toEnum val, gen')
> >  where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen
> > 
> > random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g)
> > random_EnumBounded = randomR_Enum (minBound, maxBound)
> > 
> > data Color = Blue | Red | Green deriving (Show, Enum, Bounded)
> > instance Random Color where
> >   randomR = randomR_Enum
> >   random  = random_EnumBounded
> > 
> > main = do stdGen <- getStdGen
> >   print (take 10 (randoms stdGen :: [Color]))
> > 
> > 
> > Dean

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Random Color

2002-11-21 Thread Mike T. Machenry
This program yeilds:
Fail: toEnum{Color}: tag (4) is outside of enumeration's range (0,3)
in ghc... any ideas why?

-mike

On Thu, Nov 21, 2002 at 10:07:12AM -0500, Dean Herington wrote:
> "Mike T. Machenry" wrote:
> 
> > Andrew and list,
> >
> >   I am a beginer. I really don't know what I would do if I derived
> > Color from Enum. You say I could create elements that way. Is there
> > some simple example someone could post to the list? Thank you for
> > your help.
> 
> Here's one way to generate random values from an enumerable set:
> 
> import Random
> 
> randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g)
> randomR_Enum (lo, hi) gen = (toEnum val, gen')
>  where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen
> 
> random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g)
> random_EnumBounded = randomR_Enum (minBound, maxBound)
> 
> data Color = Blue | Red | Green deriving (Show, Enum, Bounded)
> instance Random Color where
>   randomR = randomR_Enum
>   random  = random_EnumBounded
> 
> main = do stdGen <- getStdGen
>   print (take 10 (randoms stdGen :: [Color]))
> 
> 
> Dean
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Random Color

2002-11-21 Thread Dean Herington
"Mike T. Machenry" wrote:

> Andrew and list,
>
>   I am a beginer. I really don't know what I would do if I derived
> Color from Enum. You say I could create elements that way. Is there
> some simple example someone could post to the list? Thank you for
> your help.

Here's one way to generate random values from an enumerable set:

import Random

randomR_Enum :: (Enum a, RandomGen g) => (a, a) -> g -> (a, g)
randomR_Enum (lo, hi) gen = (toEnum val, gen')
 where (val, gen') = randomR (fromEnum lo, fromEnum hi) gen

random_EnumBounded :: (Enum a, Bounded a, RandomGen g) => g -> (a, g)
random_EnumBounded = randomR_Enum (minBound, maxBound)

data Color = Blue | Red | Green deriving (Show, Enum, Bounded)
instance Random Color where
  randomR = randomR_Enum
  random  = random_EnumBounded

main = do stdGen <- getStdGen
  print (take 10 (randoms stdGen :: [Color]))


Dean

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Random Color

2002-11-20 Thread Mike T. Machenry
Andrew and list,

  I am a beginer. I really don't know what I would do if I derived
Color from Enum. You say I could create elements that way. Is there
some simple example someone could post to the list? Thank you for
your help.

-mike

On Thu, Nov 21, 2002 at 01:55:55PM +1100, Andrew J Bromage wrote:
> G'day all.
> 
> On Wed, Nov 20, 2002 at 08:44:36PM -0500, Mike T. Machenry wrote:
> 
> > I am trying to construct an infinate list of pairs of random colors.
> > I am hung up on getting a random color. I have:
> > 
> > data Color = Blue | Red | Green deriving (Eq, Ord, Show)
> > 
> > am I supposed to instantiate a Random class instance from color?
> 
> You could derive instances of Enum (and possibly also Bounded) and
> create random elements that way.
> 
> Cheers,
> Andrew Bromage
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe



Re: Random Color

2002-11-20 Thread Andrew J Bromage
G'day all.

On Wed, Nov 20, 2002 at 08:44:36PM -0500, Mike T. Machenry wrote:

> I am trying to construct an infinate list of pairs of random colors.
> I am hung up on getting a random color. I have:
> 
> data Color = Blue | Red | Green deriving (Eq, Ord, Show)
> 
> am I supposed to instantiate a Random class instance from color?

You could derive instances of Enum (and possibly also Bounded) and
create random elements that way.

Cheers,
Andrew Bromage
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe