简体   繁体   English

haskell:创建记录列表

[英]haskell: creating list of records

How to create a list of records in haskell 如何在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. 任何人都可以解释它并告诉我如何解决它。

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 ). 您不能使用minBoundmaxBound除非您事先声明它们对您的类型的意义(顺便说一下,它不是记录类型 )。 You must, as the error also tells you, declare the type as an instance of Bounded . 您必须,正如错误也告诉您的那样,将类型声明为Boundedinstance 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 ... ) (填写...

You didn't tell it how to enumerate values of type TestList . 您没有告诉它如何枚举TestList类型的值。 Even if it understands what minBound and maxBound are, it doesn't know how to discover what all of the values in between are (in order to create a list with those values). 即使它理解minBoundmaxBound是什么,它也不知道如何发现它们之间的所有值(为了创建具有这些值的列表)。

By adding an instance declaration for Enum TestList , you would basically be instructing it on how to enumerate values, so it would be able to construct that sequence for you. 通过为Enum TestList添加实例声明,您基本上可以指示它如何枚举值,因此它将能够为您构造该序列。

There are two problems here. 这里有两个问题。 First, you'll need to create an Enum instance (as others have said). 首先,您需要创建一个Enum实例(正如其他人所说)。 An Enum instance is required because you've used the special enumeration syntax [ a .. b] . Enum实例是必需的,因为您使用了特殊的枚举语法[ a .. b]

Once you've created the Enum instance, you'll also need to write an instance for Bounded because you've used minBound and maxBound . 一旦你创建了Enum实例,你还需要为Bounded编写一个实例,因为你已经使用了minBoundmaxBound

Usually you can tell Haskell compilers to derive both of these instances, however that won't work here because neither Lists nor Strings have instances for either type class. 通常你可以告诉Haskell编译器派生这两个实例,但是这在这里不起作用,因为Lists和Strings都没有任何类型类的实例。 What value should maxBound :: String have, anyway? 无论如何, maxBound :: String应该有什么价值? You could always make a longer string, or add another element to a list. 您总是可以创建一个更长的字符串,或者将另一个元素添加到列表中。 Since you can't derive the instances, you'll have to manually write the Enum instance as in larsmans answer and similarly a Bounded instance. 由于您无法派生实例,因此您必须手动编写Enum实例,如larsmans answer和类似的Bounded实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM