У меня возникли проблемы с ошибкой, я не понимаюОжидаемый тип, но TYPENAME имеет вид '* -> *'
Код:
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
Ошибка:
Expecting one more argument to ‘HTree’
Expected a type, but ‘HTree’ has kind ‘* -> *’
In the type signature for ‘treeHuff’:
treeHuff :: [(String, Int)] -> HTree
Если возможно, вы можете объяснить как ошибку, так и то, что я сделал.
Решение:
Благодаря 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):[])
это последняя часть 'treeHuff :: [(String, Int)] -> HTree' - последнее не является ** типом ** еще - вам нужно указать тип' a' int 'HTree a '- в этом случае' HTree (String, Int) '(или так кажется) – Carsten
Сообщение об ошибке ** говорит **, что HTree ожидает еще один аргумент и точно определяет, где он его ожидает. – Ben
Чтобы сделать вещи более запутанными, слово «тип» иногда относится к объектам уровня типа всех видов, а иногда и к типам «*» или «#». – dfeuer