Haskell 如何计算嵌套元组的数量以及如何合并此元组列表
1.如何计算嵌套元组的数量 2.合并时出错,即使嵌套数相同,如何合并此元组列表 错误:Haskell 如何计算嵌套元组的数量以及如何合并此元组列表,haskell,Haskell,1.如何计算嵌套元组的数量 2.合并时出错,即使嵌套数相同,如何合并此元组列表 错误: <interactive>:13:1: No instance for (Eq Expr) arising from a use of `union' Possible fix: add an instance declaration for (Eq Expr) In the expression: union b1 b2 In an equation for `i
<interactive>:13:1:
No instance for (Eq Expr) arising from a use of `union'
Possible fix: add an instance declaration for (Eq Expr)
In the expression: union b1 b2
In an equation for `it': it = union b1 b2
此处的函数定义与声明不一致。但这不仅仅是一个错误:您调用的“tuple”实际上是一个树,因此您可能需要这样的代码:
data NestType = ConsLeaf Int | ConsPair NestType NestType
countnest :: NestType -> Int
countnest (ConsLeaf _) = 1
countnest (ConsPair x y) = max (countnest x) (countnest y) +1
如果定义自定义类型,它就不再是元组了。可以直接计算元组吗?此外,在上述情况下,b1和b2如何合并?如何使用此自定义类型?countnest(ConsPair(ConsLeaf 1)(ConsLeaf 2))(ConsLeaf 3))我现在知道如何使用您的意思是“union”是连接吗?(1,2,3)->[1,2,3]?在更改此数据表达式时遇到了一些困难,即导出(显示)数据NestType=ConsLeaf Expr | ConsPair NestType让cartProd xs ys=[(ConsPair(ConsLeaf x),(ConsLeaf y))| x并集是串联的[(1,2),3]((4,5),6)]到[(1,2),3),(4,5)]
*Main> :l oo.hs
[1 of 1] Compiling Main ( oo.hs, interpreted )
oo.hs:140:12:
Couldn't match expected type `Int'
with actual type `((Int, Int), (Int, Int))'
In the pattern: (a1, a2)
In the pattern: ((a1, a2), (b1, b2))
In an equation for `countnest':
countnest ((a1, a2), (b1, b2))
= max
(max
((countnest (fst (a1, a2))) + 1) ((countnest (snd (a1, a2))) + 1))
(max
((countnest (fst (b1, b2))) + 1) ((countnest (snd (b1, b2))) + 1))
oo.hs:140:21:
Couldn't match expected type `Int'
with actual type `((Int, Int), (Int, Int))'
In the pattern: (b1, b2)
In the pattern: ((a1, a2), (b1, b2))
In an equation for `countnest':
countnest ((a1, a2), (b1, b2))
= max
(max
((countnest (fst (a1, a2))) + 1) ((countnest (snd (a1, a2))) + 1))
(max
((countnest (fst (b1, b2))) + 1) ((countnest (snd (b1, b2))) + 1))
countnest :: (Int,Int) -> Int
countnest ((a1,a2), (b1,b2)) = ...
data NestType = ConsLeaf Int | ConsPair NestType NestType
countnest :: NestType -> Int
countnest (ConsLeaf _) = 1
countnest (ConsPair x y) = max (countnest x) (countnest y) +1