So for an assignment I have been given, I had three functions to complete, those being to extract an HCodeMap from each leaf node of a given tree, to encode a string into a list of Bits, and to decode that string of bits back into a string.
I have successfully completed the code extraction and encoding functions, but I am struggling to make progress with the last decoding function as we are not allowed to traverse a tree as we are not given one to use.
This is the format of the function, followed by some of the types we are supplied with:
decode :: [Bit] -> HCodeMap -> Maybe String
data Bit = Zero | One deriving (Show, Eq)
type HCodeMap = [(Char, [Bit])]
I initially tried creating my own lookup function, which would swap the values of the HCodeMap, and then to lookup the first n bits from the list of Bits we are given.
I will use an example to demonstrate if I have not made myself very clear:
[Bit] we are given : [One,Zero,One,One,Zero]
HCodeMap we are given: [('c',[Zero]),('a',[One,Zero]),('b',[One,One])]
I planned to take the first bit we are given from the list, being One, and then to search through HCodeMap testing to see if that was equal to any of the [Bit]s there.
This is where my reverse lookup function would come in, as I could lookup the list of bits within the HCodeMap, as I cannot lookup by letter. It was along the lines of:
lookup (bits we are given here) (each tuple of HCodeMap) $ map swap code
In this case, we see that One does not match any of the HCodeMap tuples, so I then test One,Zero. This matches with 'a', so I add 'a' to a string, and then carry on with the next [Bit] we are passed, being One again.
etc etc this goes on and we are left with the string "abc".
I am really struggling with how to actually put this into a function however.
I hope I have not made this too confusing, thanks for any help in advance!
HCodeMap
? – András Kovács