带有 Set 的 Haskell Ord 实例

发布于 2024-09-05 18:34:04 字数 576 浏览 2 评论 0 原文

我有一些代码想用来将边附加到 Node 数据结构:

import Data.Set (Set)
import qualified Data.Set as Set

data Node = Vertex String (Set Node)
    deriving Show

addEdge :: Node -> Node -> Node
addEdge (Vertex name neighbors) destination
    | Set.null neighbors    = Vertex name (Set.singleton destination)
    | otherwise             = Vertex name (Set.insert destination neighbors)

但是,当我尝试编译时,我收到此错误:

No instance for (Ord Node)
      arising from a use of `Set.insert'

据我所知,Set.insert 除了一个值和一个集合之外什么都不需要将其插入。这个命令是什么?

I have some code that I would like to use to append an edge to a Node data structure:

import Data.Set (Set)
import qualified Data.Set as Set

data Node = Vertex String (Set Node)
    deriving Show

addEdge :: Node -> Node -> Node
addEdge (Vertex name neighbors) destination
    | Set.null neighbors    = Vertex name (Set.singleton destination)
    | otherwise             = Vertex name (Set.insert destination neighbors)

However when I try to compile I get this error:

No instance for (Ord Node)
      arising from a use of `Set.insert'

As far as I can tell, Set.insert expects nothing but a value and a set to insert it into. What is this Ord?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

嘿咻 2024-09-12 18:34:04

在 GHCi 中:

> import Data.Set
> :t insert
insert :: (Ord a) => a -> Set a -> Set a

所以是的,它确实需要 Ord。至于Ord的含义,它是有序值类型类。在本例中这是必需的,因为 Data.Set 使用搜索树,因此需要能够比较值以查看哪个较大或它们是否相等。

几乎所有标准内置数据类型都是 Ord 的实例,列表、元组、Maybe 等都是 Ord< 的实例。 /code> 当它们的类型参数是时。当然,最值得注意的例外是函数,其中无法定义合理的排序(甚至相等)概念。

在许多情况下,您可以在声明后使用 deriving 子句自动为您自己的数据类型创建类型类的实例:

data Foo a = Foo a a Int deriving (Eq, Ord, Show, Read)

对于参数化类型,自动派生取决于类型参数也是一个实例,如下所示列表、元组等就是这种情况。

除了 Ord 之外,一些重要的类型类还有 Eq(相等比较,但不能小于/大于)、Enum(可以枚举以下值的类型) ,例如计数Integer)和Read/Show(使用字符串进行简单的序列化/反序列化)。要了解有关类型类的更多信息,请尝试Real World Haskell 中的本章,或者,更全面的概述,有维基百科文章

In GHCi:

> import Data.Set
> :t insert
insert :: (Ord a) => a -> Set a -> Set a

So yes, it does expect Ord. As for what Ord means, it's a type class for ordered values. It's required in this case because Data.Set uses a search tree, and so needs to be able to compare values to see which is larger or if they're equal.

Nearly all of the standard built-in data types are instances of Ord, as well as things like lists, tuples, Maybe, etc. being instances of Ord when their type parameter(s) are. The most notable exception, of course, are functions, where no sensible concept of ordering (or even equality) can be defined.

In many cases, you can automatically create instances of type classes for your own data types using a deriving clause after the declaration:

data Foo a = Foo a a Int deriving (Eq, Ord, Show, Read)

For parameterized types, the automatic derivation depends on the type parameter also being an instance, as is the case with lists, tuples, and such.

Besides Ord, some important type classes are Eq (equality comparisons, but not less/greater than), Enum (types you can enumerate values of, such as counting Integers), and Read/Show (simple serialization/deserialization with strings). To learn more about type classes, try this chapter in Real World Haskell or, for a more general overview, there's a Wikipedia article.

疏忽 2024-09-12 18:34:04

Haskell 集基于搜索树。为了将元素放入搜索树中,必须给出元素的排序。您可以像派生 Show 一样将其添加到数据声明中来派生 Ord,即:

data Node = Vertex String (Set Node)
   deriving (Show, Eq, Ord)

您可以通过 Data.Set.insert 的签名看到 Ord 的要求

(Ord a) => a -> Set a -> Set a

(Ord a) =>(Ord a) => 建立了一个约束,即 a 存在类型类 Ord 的实例。 有关类型类的部分位于 haskell 教程 给出了更全面的解释。

Haskell sets are based on a search tree. In order to put an element in a search tree an ordering over the elements must be given. You can derive Ord just like you are deriving Show by adding it to your data declaration, i.e.:

data Node = Vertex String (Set Node)
   deriving (Show, Eq, Ord)

You can see the requirement of Ord by the signature of Data.Set.insert

(Ord a) => a -> Set a -> Set a

The part (Ord a) => establishes a constraint that there is an instance of the typeclass Ord for a. The section on type classes in the haskell tutorial gives a more thorough explanation.

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