在 Haskell 中提取图形的元素



我有一个关于哈斯克尔的小问题。 如果我有表示像这样的图形的数据类型:

import Data.Map (Map,empty,member,insert)
import Graphviz
-- | A directed graph
data Graph v = Graph
{ arcsMap :: Map v [v]     -- A map associating a vertex with its 
successors
, labelMap :: Map v String -- The Graphviz label of each node
, styleMap :: Map v String -- The Graphviz style of each node
}

我想通过访问给定图形的每个顶点来创建一个列表。

喜欢这个:

-- | Returns the list of vertices of a graph in ascending order
--
-- >>> vertices emptyGraph
-- []
-- >>> vertices $ addVertices emptyGraph [1,4,5,2,1]
-- [1,2,4,5]
vertices :: Graph v -> [v]

我的问题是我如何告诉Haskell查看arcsMap中的每个顶点并用它创建一个列表? 谢谢!!!

函数keys返回映射的所有键。因此,您可以实现如下vertices

vertices :: Graph v -> [v]
vertices = keys . arcsMap

最新更新