2013-04-06 21 views
1

如何建立在Haskell有圖:如何在Haskell中構建圖形?

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

和功能:

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

'Graph'不是一個類型,它是一個類型類。您需要先定義一個合適的類型。 – 2013-04-06 17:35:26

回答

9

目前,你只是有一個typeclass,這是八九不離十像一個面向對象的接口。就像一個接口一樣,你實際上不能「構建」一個類。您需要選擇一個具體的實現(使用關鍵字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 

這可能會或可能不會取決於你想要做什麼是可以接受的實例。有關您的實際目標的更多信息將有助於我提出更好的代表性。