data Tree a = Branch a a | Leaf deriving (Show)
construct :: (Integral a) => [a] -> Tree a
construct [] = Leaf
construct (x:[]) = Leaf -- _____error________
construct (l:r:xs) = if l>r then Branch l (construct $ r:xs)
else Branch (construct $ l:xs) r
* Occurs check: cannot construct the infinite type: a ~ Tree a
* In the second argument of `Branch', namely `(construct $ r : xs)'
In the expression: Branch l (construct $ r : xs)
In the expression:
if l > r then
Branch l (construct $ r : xs)
else
Branch (construct $ l : xs) r
* Relevant bindings include
xs :: [a] (bound at C:\Stuff\code\New folder (2)\try.hs:69:16)
r :: a (bound at C:\Stuff\code\New folder (2)\try.hs:69:14)
l :: a (bound at C:\Stuff\code\New folder (2)\try.hs:69:12)
construct :: [a] -> Tree a
为什么是无限类型?这个“无限类型”是输入还是输出?
而且,如果我将 更改construct (x:[]) = Leaf为construct (x:[]) = x则错误发生在x. 为什么?