[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
Richard O'Keefe
ok at cs.otago.ac.nz
Thu Mar 3 22:58:07 CET 2011
By the way, tuples *can* be members of Enum if you make them so.
Try
instance (Enum a, Enum b, Bounded b) => Enum (a,b)
where
toEnum n = (a, b)
where a = toEnum (n `div` s)
b = toEnum (n `mod` s)
p = fromEnum (minBound `asTypeOf` b)
q = fromEnum (maxBound `asTypeOf` b)
s = q - p + 1
fromEnum (a, b) = fromEnum a * s + fromEnum b
where p = fromEnum (minBound `asTypeOf` b)
q = fromEnum (maxBound `asTypeOf` b)
s = q - p + 1
data T1 = A | B | C deriving (Enum, Eq, Bounded, Show)
data T2 = D | E | F deriving (Enum, Eq, Bounded, Show)
t1 = [(A,D) .. (B,F)]
I can't think of an approach that doesn't require all but one of
the tuple elements to have Bounded types. There are of course
all sorts of ways to enumerate tuples; this one is compatible
with the Ord instance.
More information about the Haskell-Cafe
mailing list