data HuffmanTree
= Empty
| Intermediate Integer HuffmanTree HuffmanTree
| Leaf Char Integer
deriving (Show,Eq)
decode :: HuffmanTree -> [Char] -> [Char]
decode (Intermediate n x y) (z:zs) =
if ..... (some checks for x,y,z).....
then [getChar x] ++ (decode (Intermediate n x y) zs)
在这个函数中,我在树上移动。在"然后"第一部分是最深的Leaf的x和之后的" ++"我应该再次给出整个输入树。问题是:在++部分之后,我的函数仍接受我当前的树(最深的叶子父母)。怎么解决? (我无法改变解码的类型,因为它是在作业中预定义的)
答案 0 :(得分:2)
您可以将原始树作为附加参数传递:
decode' :: HuffmanTree -> HuffmanTree -> [Char] -> [Char]
decode' (Intermediate originalN originalX originalY) (Intermediate n x y) (z:zs) =
if ..... (some checks for x,y,z).....
then [getChar x] ++ (decode' (Intermediate originalN originalX originalY) (Intermediate originalN originalX originalY) zs)
这样您就可以随时访问整个输入树。