haskell:创建记录列表

发布于 2024-12-11 13:58:02 字数 662 浏览 2 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╭⌒浅淡时光〆 2024-12-18 13:58:02

您不能使用 minBoundmaxBound 除非您事先声明它们对您的类型意味着什么(顺便说一句,这不是 记录类型)。正如错误还告诉您的那样,您必须将类型声明为 Bounded实例。如果不知道你的类型要代表什么,就不可能确切地说出这样的声明应该是什么样子,但它的一般形式是

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

(填写 ...

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 ...)

云柯 2024-12-18 13:58:02

您没有告诉它如何枚举 TestList 类型的值。即使它理解 minBoundmaxBound 是什么,它也不知道如何发现之间的所有值是什么(以便创建包含这些值的列表) )。

通过为 Enum TestList 添加实例声明,您基本上会指示它如何枚举值,因此它能够为您构造该序列。

You didn't tell it how to enumerate values of type 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).

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.

白衬杉格子梦 2024-12-18 13:58:02

这里有两个问题。首先,您需要创建一个 Enum 实例(正如其他人所说)。需要 Enum 实例,因为您使用了特殊的枚举语法 [ a .. b]

创建 Enum 实例后,您还需要为 Bounded 编写一个实例,因为您已经使用了 minBoundmaxBound

通常,您可以告诉 Haskell 编译器派生这两个实例,但这在这里不起作用,因为列表和字符串都没有任一类型类的实例。无论如何,maxBound :: String 应该具有什么值?您始终可以创建更长的字符串,或者将另一个元素添加到列表中。由于您无法派生实例,因此您必须像 larsmans 答案中那样手动编写 Enum 实例,以及类似的 Bounded 实例。

There are two problems here. First, you'll need to create an Enum instance (as others have said). An Enum 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 for Bounded because you've used minBound and maxBound.

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 the Enum instance as in larsmans answer and similarly a Bounded instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文