繁体   English   中英

具有列表/元组构造的自定义数据类型中的Haskell类型匹配错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM