1

I have this data type :

data Node a = Node
    { label :: a,
        adjacent :: [(a,Int)] } deriving (Show, Eq)
data Network a = Graph [Node a] deriving (Show, Eq)

I have a function which turns a Graph to a list of nodes :

deGraph :: ([Node a] -> Network a) -> [Node a] -> [Node a]  
deGraph _ x = x
 for example : 
Main> deGraph Graph [ ( Node 'a' [ ( 'b' , 3 ) , ( 'c' ,2 ) ] ) , ( Node 'b' [ ('c' , 3 ) ] ) , ( Node 'c' [] ) ]
[Node {label = 'a', adjacent = [('b',3),('c',2)]},Node {label = 'b', adjacent = [('c',3)]},Node {label = 'c', adjacent = []}]

But when I use the function inside a function like this :

func1 (Graph x) = deGraph (Graph x)

I get this error :

ERROR "./Network.hs":14 - Type error in application * Expression : deGraph (Graph x) Term : Graph x Type : Network b * Does not match : [Node a] -> Network a

Can you tell me how can I solve this problem?

4

2 回答 2

4

您的deGraph函数有两个参数,只返回两个参数中的第二个。

你可能想要这个:

deGraph :: Network a -> [Node a]
deGraph (Graph x) = x

GHCi 中的调用deGraph有效,因为您忘记将括号括起来Graph和下面的列表,所以它也是一个带有两个参数的调用。在func1中,您(正确地)使用括号,但随后出现类型错误,因为您不一致。

于 2013-04-19T09:20:05.810 回答
0

也简单地做Graph一个记录:

data Network a = Graph { nodes :: [Node a] } deriving (Show, Eq)

然后nodes有类型Network a -> [Node a],并且可以被称为

Main> nodes $ Graph listOfNodes
于 2013-04-19T09:21:34.627 回答