[Haskell-cafe] Generating random enums

michael rice nowgate at yahoo.com
Fri Oct 16 15:36:16 EDT 2009


What is the minimum I need to do to get this function to generate a three direction tuple?

Michael


=====================

import System.Random
import Data.Ord

data Dir
    = North
    | South
    | East
    | West
    deriving (Show, Read, Eq, Enum, Ord, Bounded)

threeDirs :: StdGen -> (Dir,Dir,Dir)
threeDirs gen =
  let (firstDir, newGen) = random gen
      (secondDir, newGen') = random newGen
      (thirdDir, newGen'') = random newGen'
  in (firstDir, secondDir, thirdDir) 



=====================

GHCi, version 6.10.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l dir.hs
[1 of 1] Compiling Main             ( dir.hs, interpreted )

dir.hs:15:29:
    No instance for (Random Dir)
      arising from a use of `random' at dir.hs:15:29-42
    Possible fix: add an instance declaration for (Random Dir)
    In the expression: random newGen'
    In a pattern binding: (thirdDir, newGen'') = random newGen'
    In the expression:
        let
          (firstDir, newGen) = random gen
          (secondDir, newGen') = random newGen
          (thirdDir, newGen'') = random newGen'
        in (firstDir, secondDir, thirdDir)
Failed, modules loaded: none.
Prelude> 
 




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091016/a0ef3b3b/attachment.html


More information about the Haskell-Cafe mailing list