我遇到了一个我不明白的错误问题
代码:
mergHufffman::(String,Int) -> (String,Int) -> (String,Int)
mergHufffman x y = (fst x ++ fst y, snd x + snd y)
data HTree a = Leaf a | Branch (HTree a) (HTree a) deriving Show
treeHuff::[(String,Int)] -> HTree
treeHuff (x:[]) = Leaf x
treeHuff (x:y:[])
| snd x < snd y = Branch Leaf x Leaf y
| snd x > snd y = Branch Leaf y Leaf x
treeHuff (x:y:z:list)
| snd x > snd merged = Branch (Leaf x) (treeHuff y:z:list)
| otherwise = Branch (treeHuff y:z:[]) (treeHuff x:list)
where merged = mergHufffman y z
Run Code Online (Sandbox Code Playgroud)
错误:
Expecting one more argument to ‘HTree’
Expected a type, but ‘HTree’ has kind ‘* -> *’
In the type signature for ‘treeHuff’:
treeHuff :: [(String, Int)] -> HTree
Run Code Online (Sandbox Code Playgroud)
如果可能的话,你可以解释错误,以及我做错的地方.
方案:
感谢Sepp2k和Carsten,我解决了这个问题.
mergHufffman::(String,Int) -> (String,Int) -> (String,Int)
mergHufffman x y = (fst x ++ fst y, snd x + snd y)
data HTree a = Leaf a | Branch (HTree a) (HTree a) deriving Show
treeHuff::[(String,Int)] -> HTree (String,Int)
treeHuff (x:[]) = Leaf x
treeHuff (x:y:[])
| snd x < snd y = Branch (Leaf x) (Leaf y)
| snd x > snd y = Branch (Leaf y) (Leaf x)
treeHuff (x:y:z:list)
| snd x > snd merged = Branch (Leaf x) (treeHuff $ sortFirst $ y:z:list)
| otherwise = Branch (treeHuff $ y:z:[]) (treeHuff $ sortFirst $ x:list)
where merged = mergHufffman y z
sortFirst::[(String,Int)]->[(String,Int)]
sortFirst freq = reverse $ sortBy (comparing snd) freq
readHuffTree :: HTree (String,Int)-> String -> [(String, String)]
readHuffTree (Branch x y) code = f1 ++ f2
where
f1 = readHuffTree x (code ++ "0")
f2 = readHuffTree y (code ++ "1")
readHuffTree (Leaf x) code = ((fst x, code):[])
Run Code Online (Sandbox Code Playgroud)
在HTree您再给它一个参数之前,错误消息告诉您这不是类型.那是HTree String或HTree Int将是类型,但HTree本身不是.
另一种说法:当你说"我想要归还一棵树"时,哈斯克尔问"一棵树是什么?".