2014-05-06 41 views
0

我不明白为什么这不起作用。 我的数据类型如下:在过去的LOCHaskell类型与列表/元组构造的数据类型匹配错误

data IndexedTree a = Leaf [a] | Node [(IndexedTree a, a)] 

first (x, _) = x 
test :: IndexedTree a -> Bool 
test (Leaf x) = True 
test (Node (x:xs)) = test first(x) 

收益率

Could not match expected type IndexedTree a0 
with actual type (t0,t1)-> t0 

。 我不明白为什么会发生这种情况,以及如何避免这种情况。

+1

请注意,'Prelude'函数'fst'与您的'first'完全相同。 – Benesh

回答

4

你的test的定义应该是:

test :: IndexedTree a -> Bool 
test (Leaf x) = True 
test (Node (x:xs)) = test (first x) 

first (x)相同first x,而且由于功能应用同伙左

test first (x) 

被解析为

(test first) x 

test期望一个IndexedTree a说法,但first具有类型(a, b) -> a,因此错误。

您还应该处理节点列表为空的情况。

1

first(x)first (x)相同,与first x相同。所以test first(x)只是test first x,test只有一个参数。

如果你要评估first x第一:

test (Node (x:xs)) = test $ first x