Search code examples
haskellwinghci

haskell: creating list of records


How to create a list of records in haskell

I have a Record

data TestList = Temp1 (String,[String])
          | Temp2 (String,[(String,String)])
    deriving (Show, Eq)

I am creating a list of records

testLists :: [TestList]
testLists =  [minBound..maxBound]

When I run, it throws me an error.

No instance for (Enum TestList)
      arising from the arithmetic sequence `minBound .. maxBound'
    Possible fix: add an instance declaration for (Enum TestList)
    In the expression: [minBound .. maxBound]
    In an equation for `testLists': testLists = [minBound .. maxBound]

It gives me a possible fix but I don't understand what it means. can anyone explain it and tell me how to fix it.


Solution

  • You can't use minBound and maxBound unless you declare beforehand what they mean for your type (which by the way is not a record type). You must, as the error also tells you, declare the type as an instance of Bounded. Without knowledge of what your type is to represent, it's impossible to say what such a declaration should look like exactly, but its general form is

    instance Bounded TestList where
      minBound = ...
      maxBound = ...
    

    (Fill in the ...)