Haskell 如何声明图的数据构造函数

Haskell 如何声明图的数据构造函数,haskell,graph,constructor,Haskell,Graph,Constructor,下面我给出列表和树的数据构造函数 data List a = NilL | Cons a (List a) deriving Show data Tree a = NilT | Branch a [Tree a] deriving Show 通过这些定义,我可以轻松创建无限结构,如下所示: list = Cons 1 list tree = Branch 1 lt where lt = tree : lt 我想用这种方式创建无限图(有向图和无向图)。如何为它声明一个数据构造函数,以及如

下面我给出列表和树的数据构造函数

data List a = NilL | Cons a (List a) deriving Show
data Tree a = NilT | Branch a [Tree a] deriving Show
通过这些定义,我可以轻松创建无限结构,如下所示:

list = Cons 1 list
tree = Branch 1 lt
 where
  lt = tree : lt

我想用这种方式创建无限图(有向图和无向图)。如何为它声明一个数据构造函数,以及如何在Haskell中使用该数据构造函数创建一个无限图?

一个简单的解决方案是使用某种形式的间接寻址,如索引

type Vertex = Integer
data Graph = Graph [Vertex] [(Vertex, Vertex)]

infGraph = Graph [1..] [(a, b) | a <- [1..], b <- [1..]]
我们在
[1..]
上映射
顶点
,它为我们提供了一个函数列表
[Vertex]->Vertex
,这些函数需要一个连接每个顶点的边列表。由于
infGraph
是所有顶点的列表,因此我们将其传递给每个
顶点
,然后打结


当然,对于严肃的工作,请使用。

在第二个例子中,您给出了
类型图=[Vertex]
,我不明白如何从这个东西中获得边?请解释一下。@t看看顶点的定义,边嵌入在顶点中。谢谢。我已经看到了它之前的定义。搞不懂Integer是如何处理这些事情的。对不起。@TemPora没问题,这回答了你的问题吗?这似乎有效,但我不明白
贴图(翻转顶点infGraph)[1..]
中的魔法是如何发生的。你能解释一下地图的使用方式和原因吗?你可能也会喜欢。
data Vertex = Vertex { tag   :: Integer
                     , edges :: [Vertex] }

type Graph = [Vertex] -- A graph is determined by the set of vertices

-- Construct a graph of infinitely many vertices in which
-- each vertex is connected.
infGraph = map (flip Vertex infGraph) [1..]
infGraph' = map (\v' -> v' infGraph') . map Vertex $ [1..]