几周前我刚开始使用 Haskell,我缺乏想象力来解决这种情况下的功能。
所以我试图在用 Haskell 实现的图中找到一个顶点的前身。
我的图表:
-- | 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
}
功能successors
:
-- | Returns the successors of a vertex in a graph in ascending order
--
-- We say that `v` is a successor of `u` in a graph `G` if the arc `(u,v)`
-- belongs to `G`.
-- Note: Returns the empty list if the vertex does not belong to the graph.
successors :: Ord v => v -> Graph v -> [v]
successors v (Graph arcs _ _) = findWithDefault [] v arcs
我目前正在尝试解决的功能:
-- | Returns the predecessors of a vertex in a graph in ascending order
--
-- We say that `u` is a predecessor of `v` in a graph `G` if the arc `(u,v)`
-- belongs to `G`.
-- Note: Returns the empty list if the vertex does not belong to the graph.
predecessors :: Ord v => v -> Graph v -> [v]
predecessors v (Graph arcs _ _) =
map (fst) (filter (\(x,[y]) -> elem v [y]) (assocs arcs) )
我需要找到一种通过获取这些顶点的值(后继)来获取键(顶点)的方法。例如 :
-- >>> predecessors 3 $ addArcs emptyGraph [(1,2),(2,3),(1,3)]
-- [1,2]
但是当我运行那条线时,我 在 lambda 中得到了非详尽的模式。
那是什么,我该如何解决?谢谢!
- 编辑:没关系,我纠正了它,但我还是不太明白哈哈