我正在编写一个函数来检查一棵树是否为BST。我尝试的方法是将树按中序遍历打印到列表中,然后检查列表是否递增。然而我遇到了这个错误:
Couldn't match expected type `a' against inferred type `[t]'
`a' is a rigid type variable bound by
the type signature for `checkList' at BST.hs:24:18
In the pattern: x : y : xs
In the pattern: [x : y : xs]
In the definition of `checkList':
checkList [x : y : xs] = x <= y && checkList (y : xs)
下面是我目前的代码(只有一个checkList函数)。
checkList :: (Ord a) => [a] -> Bool
checkList [] = True
checkList [x] = True
checkList [x:y:xs] = x <= y && checkList (y:xs)