haskell:创建记录列表
如何在 haskell 中创建记录列表
我有一个记录
data TestList = Temp1 (String,[String])
| Temp2 (String,[(String,String)])
deriving (Show, Eq)
我正在创建一个记录列表
testLists :: [TestList]
testLists = [minBound..maxBound]
当我运行时,它会抛出一个错误。
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]
它给了我一个可能的解决方案,但我不明白这意味着什么。谁能解释一下并告诉我如何解决它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用
minBound
和maxBound
除非您事先声明它们对您的类型意味着什么(顺便说一句,这不是 记录类型)。正如错误还告诉您的那样,您必须将类型声明为Bounded
的实例
。如果不知道你的类型要代表什么,就不可能确切地说出这样的声明应该是什么样子,但它的一般形式是(填写
...
)You can't use
minBound
andmaxBound
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 aninstance
ofBounded
. 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(Fill in the
...
)您没有告诉它如何枚举
TestList
类型的值。即使它理解minBound
和maxBound
是什么,它也不知道如何发现之间的所有值是什么(以便创建包含这些值的列表) )。通过为
Enum TestList
添加实例声明,您基本上会指示它如何枚举值,因此它能够为您构造该序列。You didn't tell it how to enumerate values of type
TestList
. Even if it understands whatminBound
andmaxBound
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).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
实例(正如其他人所说)。需要Enum
实例,因为您使用了特殊的枚举语法[ a .. b]
。创建
Enum
实例后,您还需要为Bounded
编写一个实例,因为您已经使用了minBound
和maxBound
。通常,您可以告诉 Haskell 编译器派生这两个实例,但这在这里不起作用,因为列表和字符串都没有任一类型类的实例。无论如何,maxBound :: String 应该具有什么值?您始终可以创建更长的字符串,或者将另一个元素添加到列表中。由于您无法派生实例,因此您必须像 larsmans 答案中那样手动编写
Enum
实例,以及类似的Bounded
实例。There are two problems here. First, you'll need to create an
Enum
instance (as others have said). AnEnum
instance is required because you've used the special enumeration syntax[ a .. b]
.Once you've created the
Enum
instance, you'll also need to write an instance forBounded
because you've usedminBound
andmaxBound
.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. What value should
maxBound :: String
have, anyway? 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 theEnum
instance as in larsmans answer and similarly aBounded
instance.