我如何调整;“尾巴”;避免Haskell中出现不正确的缩进

我如何调整;“尾巴”;避免Haskell中出现不正确的缩进,haskell,Haskell,有没有办法解决压痕不正确的问题?我想检查给定树上的每个节点是否都比它的祖父大(如果有)。 我已经编写了一个函数,它给出了每个级别上的所有节点,主函数的思想是比较节点是否比其子节点小 data BTree = Empty | Node Int BTree Btree grandchildrenIncreased :: BTree -> Bool grandchildrenIncreased Empty = Empty grandchildrenIncreased BTree = func

有没有办法解决压痕不正确的问题?我想检查给定树上的每个节点是否都比它的祖父大(如果有)。 我已经编写了一个函数,它给出了每个级别上的所有节点,主函数的思想是比较节点是否比其子节点小

data BTree = Empty | Node Int BTree Btree

grandchildrenIncreased :: BTree -> Bool
grandchildrenIncreased Empty = Empty
grandchildrenIncreased BTree = func BTree level
 where f BT n
  | head (level BT n) < head (level BT n+2) = tail (level BT 1)
     (level BT n+2) grandchildrenIncreased f BT (n+1)
  | length (level BT n) == 0 = True
  | otherwise = False

level :: BTree -> Int -> [a]
level Empty _ = []
level (Node n lt rt) 1 = [n]
level (Node _ lt rt) k
 = (level lt (k-1)) ++ (level rt (k-1))
data BTree=Empty |节点Int BTree BTree
孙辈增加::BTree->Bool
孙子女:空=空
孙子增加的BTree=功能BTree级别
其中f BT n
|头部(BT n级)<头部(BT n+2级)=尾部(BT 1级)
(等级BT n+2)孙辈增加f BT(n+1)
|长度(级别BT n)==0=True
|否则=假
级别::BTree->Int->[a]
级别为空=[]
级别(节点n lt rt)1=[n]
标高(节点lt rt)k
=(lt(k-1)级)++(rt(k-1)级)

它会在“tail”所在的整行上产生错误。

通常最好使用模式匹配。通过使用模式匹配,如果您没有覆盖所有可能的模式,编译器可以警告您,因此您的程序出错的可能性较小

也就是说,您当前的尝试有很多问题,不仅仅是缩进

如果我们看一下类型,您的
孙辈增加的
似乎没有多大意义。例如,您编写了
grandrenempty=Empty
,但结果类型是
Bool
,因此它应该是
True
False

您还可以将类型构造函数(如
BTree
)与数据构造函数混合使用。例如,您的第二行
BTree=func BTree level
也没有多大意义,因为
BTree
是一种类型,它不是树值的特定模式

您似乎还在您的
孙子中使用
level
来引用
level::BTree->Int->[a]
函数以及一些不存在的
Int

最后,通过首先将树转换为级别列表,您将丢失哪个节点连接到哪个祖辈节点的结构,从而很难进行检查

grandchildrenIncreased :: BTree -> Bool
grandchildrenIncreased Empty = True
grandchildrenIncreased n@(Node v l r) =
    all (v <) (level n 3) && grandchildrenIncreased l && grandchildrenIncreased r
然而,在这里定义
子函数可能更有用:

children :: BTree -> [BTree]
children Empty = []
children (Node _ lt rt) = [lt, rt]
以及提取
节点的值的方法:

treeVal :: BTree -> Maybe Int
treeVal Empty = Nothing
treeVal (Node v _ _) = Just v
然后,我们可以使用以下公式得出值:

import Data.Maybe(catMaybes)

grandchildrenIncreased :: BTree -> Bool
grandchildrenIncreased Empty = True
grandchildrenIncreased n@(Node v l r) =
    all (v <) (catMaybes (treeVal <$> (children n >>= children))) &&
    grandchildrenIncreased l && grandchildrenIncreased r
导入数据。可能(catMaybes)
孙辈增加::BTree->Bool
孙子增加为空=真
孙辈增加n@(节点v l r)=
全部(v>=子项)&&

孙子增加了l&&孙子增加了r
您可以使用模式匹配。变量名必须以小写开头。除非
BT
是一个构造函数(而且看起来不是),否则您应该将其命名为类似
BT
。另外,请在将来发布完整的错误消息。
import Data.Maybe(catMaybes)

grandchildrenIncreased :: BTree -> Bool
grandchildrenIncreased Empty = True
grandchildrenIncreased n@(Node v l r) =
    all (v <) (catMaybes (treeVal <$> (children n >>= children))) &&
    grandchildrenIncreased l && grandchildrenIncreased r