【发布时间】:2019-02-08 11:41:16
【问题描述】:
我正在尝试在 haskell 中实现霍夫曼编码并使用以下两种数据结构:
data Htree = Leaf Char | Branch Htree Htree deriving Show
data Wtree = L Integer Char | B Integer Wtree Wtree deriving Show
首先根据每个字符的频率/权重创建 Wtree。 构建 Wtree 后,我们知道树的结构,我不再需要每个叶子/分支的权重,所以我想将 Wtree 转换为 Htree,但我无法解决这个问题。
createHtree :: Wtree -> Htree
createHtree(L _ char) = Leaf char
createHtree(B _ w1 w2) = Branch createHtree(w1) createHtree(w2)
这是我尝试的解决方案,但它不会编译
预期的结果是我提到从 Wtree 到 Htree 的转换,它只需要删除 Wtree 的 Integer 部分。
【问题讨论】:
-
而且我认为最后一行的分组不正确,即使意图很明确:试试
Branch (createHtree w1) (createHtree w2) -
哇,我不敢相信这行得通,它现在编译了。我感到很茫然
-
Branch createHtree(w1) createHtree(w2)与Branch createHtree w1 createHtree w2相同,后者使用四个参数调用构造函数Branch(触发错误)。 Haskell 中的召回函数应用是(f x)而不是f(x)。 -
确实,这些是完全不同的。例如
f x y使用参数x和y调用函数f。它相当于(f x) y。相反,f (x y)将调用函数f,只使用一个参数,即调用函数x和参数y的结果。因此,应该小心使用括号。 -
是的,表达式在
=的右边,patterns在左边!在 RHS 上(L w char)构造 a 值(从两个值),在 LHS 上它解构这样的值。
标签: haskell type-conversion binary-tree huffman-code