[英]Haskell Type Matching Error in custom data type with List/Tuple Construction
我不明白为什么这行不通。 我的数据类型如下:
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
在最后一个LOC中。 我不明白为什么会这样以及如何避免这种情况。
您对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
,因此是错误的。
您还应该处理节点列表为空的情况。
first(x)
与first (x)
相同,后者与first x
相同。 因此, test first(x)
只是test first x
,而test
只需要一个参数。
如果first x
评估first x
:
test (Node (x:xs)) = test $ first x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.