[英]Difficulty implementing a huffman tree in haskell (Edited with my current try at the solution)
我目前在 haskell 的勇敢冒险中遇到困难。 我被困在一个需要递归的问题上,想知道是否有人愿意提供一个解决方案的例子。 问题是:
Given a Huffman tree and a stream of bits, return a pair containing (1) the string of symbols encoded by the bits (according to the Huffman tree), and (2) a Bool indicating whether the output stream contains every bit from the input (也就是说,如果有任何剩余位,则返回 False)。
例如。
input: decode xyz-code [True,False]
output: ("y", True)
input: decode xyz_code [True,False,True,True,True,False,False,True,False]
output: {"yzyxy",True)
我得到了结构
data BTree a = Leaf a | Fork (BTree a) (BTree a) deriving (Show, Eq)
和
xyz_code :: BTree Char
xyz_code =
Fork
(Leaf 'x')
(Fork
(Leaf 'y')
(Leaf 'z'))
(编辑)这是我到目前为止提出的,为什么这不起作用并得到一个非详尽的模式错误:
decode :: BTree a -> [Bool] -> ([a], Bool)
decode _ [] = ([],True)
decode (Leaf v) [bs] = ([v], bs)
decode (Fork left right) (b:bs)
| b = decode right bs
| otherwise = decode left bs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.