Skip to content

Instantly share code, notes, and snippets.

@djanatyn
Created September 29, 2022 19:30
Show Gist options
  • Save djanatyn/2881dd7ed804b9c08b5db672fc656c64 to your computer and use it in GitHub Desktop.
Save djanatyn/2881dd7ed804b9c08b5db672fc656c64 to your computer and use it in GitHub Desktop.
exploring huffman coding
-- build huffman coding tree from text input
ghci> buildTree "recurse center"
Branch
(Branch
(Branch
(Leaf (Frequency {char = 'u', count = 1}))
(Branch
(Leaf (Frequency {char = 's', count = 1}))
(Leaf (Frequency {char = 't', count = 1}))))
(Leaf (Frequency {char = 'r', count = 3})))
(Branch
(Branch
(Branch
(Leaf (Frequency {char = ' ', count = 1}))
(Leaf (Frequency {char = 'n', count = 1})))
(Leaf (Frequency {char = 'c', count = 2})))
(Leaf (Frequency {char = 'e', count = 4})))
-- interpret huffman tree -> dictionary
ghci> coding $ buildTree "recurse center"
[('u',"000"),('s',"0010"),('t',"0011"),('r',"01"),(' ',"1000"),('n',"1001"),('c',"101"),('e',"11")]
-- translate input to binary
ghci> let input = "recurse center"; tree = buildTree input in translate (coding tree) input
"011110100001001011100010111100100111101"
-- grab binary string
ghci> let binary = (let input = "recurse center"; tree = buildTree input in translate (coding tree) input)
-- split bits into bytes
ghci> unfoldr grabByte binary
["01111010","00010010","11100010","11110010","0111101"]
-- each byte in decimal
ghci> let bytes = unfoldr grabByte binary in fst <$> concatMap (Numeric.readBin @Int) bytes
[122,18,226,242,61]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment