我是Haskell的新手,我正在为创建图形及其中的节点创建一个类型类来玩。既然我想要有向图和无向图,我有Haskell不能匹配类型,声明刚性变量
data Node = Node { label :: Char
, index :: Int
} deriving (Ord, Eq)
type Graph edgeType = ([Node], [edgeType])
data Edge = DirectedEdge {h :: Node, t :: Node}
| UndirectedEdge {a :: Node, b :: Node}
instance Show Node where
show n = ['(', label n, ')']
instance Show Edge where
show (DirectedEdge h t) = show h ++ "->" ++ show t
show (UndirectedEdge a b) = show a ++ "-" ++ show b
所以我区分了有向和无向边。图必须只有两种类型的边。我也有以下几点:
nodes :: [Node]
nodes = zipWith Node ['a'..] [0..]
emptyGraph :: [Node] -> Graph edgeType
emptyGraph ns = (ns, [])
到目前为止好,但我写一个函数connect
,与节点连接到现有的图形。理想情况下,我只希望它适用于无向图,但这似乎不是一种选择。相反,我有这样的事情:
connect :: Graph edgeType -> Node -> Graph edgeType
connect (ns, es) n = (n:ns, e:es)
where e = UndirectedEdge n (head ns)
但是,这提供了以下错误:
Couldn't match type `edgeType' with `Edge'
`edgeType' is a rigid type variable bound by
the type signature for
connect :: Graph edgeType -> Node -> Graph edgeType
什么是完成我想实现的最佳途径?
谢谢。有没有什么办法可以将'combine' _只适用于无向图,因为我已经定义了它们? 'connect :: Graph UndirectedEdge - > Node - > Graph UndirectedEdge'不起作用。 – 2011-06-07 02:52:22
@Jordan这不起作用,因为'UndirectedEdge'是一个“构造函数”而不是“类型”。您需要根据Lambdageek的建议寻找解决方案,将Edge分成两种截然不同的类型。 – 2011-06-07 04:09:26