如何在Haskell中构建图形?

时间:2013-04-06 17:32:41

标签: haskell graph

如何在Haskell中构建图表:

type Node = Int
type Arc = (Node, Node) 

和功能:

class Graph g where
         build :: [Node] -> [Arc] -> g

1 个答案:

答案 0 :(得分:9)

目前你只有一个typeclass,它就像一个OOP界面。就像一个界面,你实际上不能“构建”一个类。您需要选择一个具体的实现(使用关键字data),然后在其上实现函数build。这就是您传递给需要Graph

的函数的内容

举个简单的例子:

--The concrete data type
data NaiveGraph = NG [Node] [Arc]

--Now we make it an instance of Graph
instance Graph NaiveGraph where
   build = NG

根据您的要求,这可能是也可能不是可接受的实例。有关您实际目标的更多信息将有助于我建议更好的代表性。