【问题标题】:Huffman code in Haskell (Make Tree)Haskell 中的霍夫曼代码(制作树)
【发布时间】:2018-08-15 18:04:03
【问题描述】:

所以这是一个关于 Huffman Tress 的长问题。我正在尝试制作树和代码表。

这是我的类型

module Types where

type Occurence  = (Value, Number)
type Occurences = [Occurence]
type Number     = Int
type Value      = Char
type Code       = [Directions]
type CodeTable  = [(Value, Code)]


data Directions = L | R deriving (Eq, Ord, Show)


data HTree      = Leaf {frequency :: Number, character:: Value} |
                  Node {frequency:: Number, 
                        leftChild:: HTree,
                        rightChild:: HTree} deriving Show


makeLeaf :: Occurence -> HTree
makeLeaf (c,n) = Leaf n c

这是我对制作树功能的尝试

module MakeTree(makeTree, treeFromTable) where
import Types

makeTree :: Occurences -> HTree
makeTree = makeCodes . toTreeList

toTreeList :: Occurences -> [HTree]
toTreeList = map (uncurry Leaf)

h :: [HTree] -> [HTree]
h (t1:t2:ts) = insertTree (join t1 t2) ts

makeCodes :: [HTree] -> HTree
makeCodes [t] = t
makeCodes ts = makeCodes (h ts)

join :: HTree -> HTree -> HTree
join t1 t2 = Node (freq1+freq2) t1 t2
    where
      freq1 = v t1
      freq2 = v t2

v :: HTree -> Int
v (Leaf _ n ) = n
v (Node n _ _) = n

insertTree :: HTree -> [HTree] -> [HTree]
insertTree t [] = [t]
insertTree t (t1:ts) 
     | v t < v t1 = t:t1:ts
     | otherwise = t1 : insertTree t ts

这是我制作 CodeTable 的尝试

constructTable :: HTree -> CodeTable
constructTable = convert []
      where
      convert :: Code -> HTree -> CodeTable
      convert hc (Leaf c n) = [(c, hc)]
      convert hc (Node n tl tr) = (convert (hc++[L]) tl) ++ (convert (hc++[R]) tr)

代码表错误

CodeTable.hs:14:33: error:
    • Couldn't match type ‘Int’ with ‘Char’
      Expected type: Value
        Actual type: Number
    • In the expression: c
      In the expression: (c, hc)
      In the expression: [(c, hc)]
   |
14 |       convert hc (Leaf c n) = [(c, hc)]

你有没有发现任何错误,或者你能告诉我为什么这些代码都不适合我吗?

【问题讨论】:

    标签: haskell huffman-code


    【解决方案1】:
    Expected type: Value
    Actual type: Number
    

    从错误信息中已经很清楚了,下面这个表达式有什么问题:

    convert hc (Leaf c n) = [(c, hc)]
    

    由于HTree定义为:

    data HTree      = Leaf {frequency :: Number, character:: Value} |
                      Node {frequency:: Number, 
                            leftChild:: HTree,
                            rightChild:: HTree} deriving Show
    

    Leaf 接受frequency(数字)作为第一个参数,character(值)作为第二个参数。因此,我们只需要交换表达式中的参数:

    convert hc (Leaf n c) = [(c, hc)]
    

    同样在v函数的定义中:

    v (Leaf n _ ) = n
    

    type Occurence的定义中:

    type Occurence  = (Number, Value)
    

    makeLeaf 中也是如此:

    makeLeaf (n, c) = Leaf n c
    

    完成这些步骤后,您的代码应该可以成功编译

    【讨论】:

    • 非常感谢!我仍然收到 make Tree MakeTree.hs:16:14 的错误:错误: • 无法将类型“Char”与“Int”匹配预期类型:出现次数 -> [HTree] 实际类型:[(数字,值)] -> [HTree] • 在表达式中:map (uncurry Leaf) 在“toTreeList”的等式中:toTreeList = map (uncurry Leaf) | 16 | toTreeList = map (uncurry Leaf)
    • 你把type Occurence的定义改成type Occurence = (Number, Value)了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多