Get the latest tech news

Building a data compression utility in Haskell using Huffman codes


In this post we will implement a data compression program in about 150 lines of Haskell. It will use Huffman coding and handle arbitrary binary files using constant memory for encoding and decoding.

Notice how go function, upon reaching a Leaf, returns a list where the head is known and the tail is a recursive call. Because we won’t use the[Bit] or ByteString in other parts of the program, the garbage collector will be able to free the memory we just allocated for that portion of input that we decoded. Canonical Huffman codes- Instead of navigating the tree for decoding in O(log n), we can use the code to index directly into a vector in O(1).

Get the Android app

Or read this on Hacker News

Read more on:

Photo of huffman

huffman

Photo of Haskell

Haskell

Photo of Huffman codes

Huffman codes

Related news:

News photo

I learned Haskell in just 15 years

News photo

How to Send an SMS in Haskell (2017)

News photo

Reddit’s Huffman Leaves ‘Young Troll’ Image Behind in Run-Up to IPO