我将通过图形表示任意的忠诚度关系。这样,每个节点的上下文将显示其所在的所有关系。我从一元关系的属性开始。
我发现自己遇到了NodeMapM monad的奇怪行为,对于我认为等效的功能序列,它给出了不同的结果( LEdges )。我在哪里错了?
import Data.Graph.Inductive.Graph (empty)
import Data.Graph.Inductive.PatriciaTree
import Data.Graph.Inductive.NodeMap
p1 = ("P1", ['A','B','C','D'])
p2 = ("P2", ['B','C','E','F'])
p3 = ("P3", ['A','C','F','G'])
toLedges :: (a, [b]) -> [(b,b,a)]
toLedges (le,xs) = zipWith (\x1 x2 -> (x1,x2,le)) (init xs) (tail xs)
ex1 :: NodeMapM Char String Gr ()
ex1 = do insMapNodesM (snd p1)
insMapNodesM (snd p2)
insMapNodesM (snd p3)
insMapEdgesM (toLedges p1)
insMapEdgesM (toLedges p2)
insMapEdgesM (toLedges p3)
-- run empty ex1 :: ((),(NodeMap Char, Gr Char String))
{- (()
, (NodeMap {map = fromList [('A',1),('B',2),('C',3),('D',4),('E',5),('F',6),('G',7)], key = 8}
,mkGraph [(1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E'),(6,'F'),(7,'G')]
[(1,2,"P1"),(1,3,"P3"),(2,3,"P1"),(2,3,"P2"),(3,4,"P1"),(3,5,"P2"),(3,6,"P3"),(5,6,"P2"),(6,7,"P3")]
)
)
-}
ex2 :: NodeMapM Char String Gr ()
ex2 = do insMapNodesM (snd p1)
insMapEdgesM (toLedges p1)
insMapNodesM (snd p2)
insMapEdgesM (toLedges p2)
insMapNodesM (snd p3)
insMapEdgesM (toLedges p3)
-- run empty ex2 :: ((),(NodeMap Char, Gr Char String))
{- (()
, (NodeMap {map = fromList [('A',1),('B',2),('C',3),('D',4),('E',5),('F',6),('G',7)], key = 8}
,mkGraph [(1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E'),(6,'F'),(7,'G')]
[(1,3,"P3"),(2,3,"P2"),(3,6,"P3"),(5,6,"P2"),(6,7,"P3")]
)
)
-}
答案 0 :(得分:2)
重新添加节点将删除该节点中的所有边。请参见insMapNodesM
最终称为the source of insNode
:
insNode (v,l) = (([],v,l,[])&)
两个空白列表分别用于传入和传出边缘。
这对insMapNodesM
来说似乎是我的不幸行为。我非常怀疑您是否会立即让他们进行更改;向后兼容的问题使我对于改变已经存在了很长时间的图书馆中的行为感到不安。