What Morse Code Taught Me About Data Compression: A Developer's Tour of Variable-Length Encoding
Every developer meets data compression eventually — usually as a library call. You gzip a response, you minify a bundle, you pick a codec. The algorithm is a black box with a good README. But the core idea behind most of it is almost two hundred years old, and it was invented by people who had never seen a computer: the telegraph operators who built Morse code.
Morse code is a compression algorithm. Not metaphorically — literally. It is a variable-length prefix code, designed by measuring letter frequencies and assigning the shortest signals to the most common letters. E, the most frequent letter in English, is a single dot. T is a single dash. Q, one of the rarest, gets four elements. That is exactly what Huffman coding does, a century before David Huffman formalised it in 1952. If you have ever wondered why compression feels intuitive once you see it, this is why: the telegraph already taught us the trick.
The problem Morse code was solving
A telegraph wire is the worst network interface imaginable. One bit of state — current flowing or not flowing — and every bit costs real time, because a human operator has to key it by hand. Sending a message is expensive in the only currency that matters: seconds on the wire.
The naive solution is a fixed-length code. Give every letter the same number of elements and the system is simple to learn and simple to decode. But fixed-length codes waste the wire. If E and Q cost the same, you are spending as much time on a letter that appears once in a thousand characters as on one that appears in one in eight. The telegraph operators, who were paid by the word and judged by speed, could not afford that waste.
So they measured. Samuel Morse and Alfred Vail reportedly counted letter frequencies by looking at the type cases in a print shop — the compartments holding the most type were the most common letters. It is a lovely piece of engineering folklore, and whether or not the story is literally true, the result is visible in the code itself: the lengths track frequency almost perfectly.
| Letter | Morse | Elements | Approx. frequency in English |
|---|---|---|---|
| E | . | 1 | 12.7% |
| T | - | 1 | 9.1% |
| A | .- | 2 | 8.2% |
| O | --- | 3 | 7.5% |
| I | .. | 2 | 7.0% |
| N | -. | 2 | 6.7% |
| S | ... | 3 | 6.3% |
| H | .... | 4 | 6.1% |
| R | .-. | 3 | 6.0% |
| Q | --.- | 4 | 0.1% |
| Z | --.. | 4 | 0.1% |
| J | .--- | 4 | 0.2% |
The correlation is not an accident. It is the same optimisation your compressor performs when it builds a frequency table.
The same idea, in Python
The easiest way to feel this is to build both codes and compare them. Here is a small script that computes the average Morse codeword length for English text, then builds a Huffman code over the same frequencies and compares the two:
import heapq
from collections import Counter
MORSE = {
'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.',
'F': '..-.', 'G': '--.', 'H': '....', 'I': '..', 'J': '.---',
'K': '-.-', 'L': '.-..', 'M': '--', 'N': '-.', 'O': '---',
'P': '.--.', 'Q': '--.-', 'R': '.-.', 'S': '...', 'T': '-',
'U': '..-', 'V': '...-', 'W': '.--', 'X': '-..-', 'Y': '-.--',
'Z': '--..',
}
def average_code_length(text, code):
letters = [c for c in text.upper() if c in code]
total = sum(len(code[c]) for c in letters)
return total / len(letters)
def huffman_lengths(freq):
heap = [[w, [ch, ""]] for ch, w in freq.items()]
heapq.heapify(heap)
while len(heap) > 1:
lo = heapq.heappop(heap)
hi = heapq.heappop(heap)
for pair in lo[1:]:
pair[1] = "0" + pair[1]
for pair in hi[1:]:
pair[1] = "1" + pair[1]
heapq.heappush(heap, [lo[0] + hi[0]] + lo[1:] + hi[1:])
return {ch: len(code) for ch, code in heap[0][1:]}
sample = open("sample.txt").read() # any English text
freq = Counter(c for c in sample.upper() if c.isalpha())
huff = huffman_lengths(freq)
morse_avg = average_code_length(sample, MORSE)
huff_avg = sum(freq[c] * huff[c] for c in freq) / sum(freq.values())
fixed_avg = 5 # every letter costs 5 bits in a fixed 32-symbol code
print(f"Morse average: {morse_avg:.2f} elements per letter")
print(f"Huffman average: {huff_avg:.2f} bits per letter")
print(f"Fixed-length (5b): {fixed_avg:.2f} bits per letter")
Run it on any decent chunk of English and the numbers land close together: Morse averages around 3 elements per letter, Huffman around 4.2 bits, fixed-length 5 bits. Morse looks better on raw length, but the comparison is not quite fair — Morse is a ternary-ish system in practice (dot, dash, and the gaps between elements carry information too), while Huffman is strictly binary. The point is not which wins. The point is that a hand-built code from the 1840s sits in the same neighbourhood as the optimal algorithm, because both are doing the same thing: spending the cheapest signals on the most frequent symbols.
Why Morse is a prefix code (and why that matters)
There is a second property that makes Morse code work as a real protocol, and it is the one developers usually miss: it is uniquely decodable. You can tell where one letter ends and the next begins.
In Huffman coding, that property comes from the prefix rule — no codeword is a prefix of another. Morse code cheats slightly: E (.) is a prefix of I (..), A (.-), and several others. The code gets away with it because of the gaps. The pause between letters is part of the signal — a third symbol, in effect. The timing rules (one unit inside a character, three units between characters, seven between words) are the framing layer of the protocol.
That is a design lesson worth stealing: framing is part of the encoding, not an afterthought. Every protocol you have ever debugged has the same shape — a payload format plus a framing rule that tells the receiver where one message ends and the next begins. TCP has it. WebSocket frames have it. Length-prefixed binary protocols have it. Morse code has it, in the form of silence. When a decoder fails on real-world data, the bug is almost always in the framing, not the payload — and Morse code's history of mistimed gaps proves the point. Operators who rushed their spacing produced unreadable traffic even when every dot and dash was perfect.
What this teaches you about choosing a compressor
If you only remember one thing from this tour, make it this: compression is not about the algorithm, it is about the data. Morse code is optimal-ish for English prose because it was tuned for English prose. Point it at a different distribution — say, a log file full of timestamps, or source code full of brackets — and its frequency table is wrong, and its advantage shrinks.
Modern compressors handle this by being adaptive: DEFLATE builds its Huffman tables from the actual input, and algorithms like Brotli and Zstandard ship with dictionaries tuned for common data. The principle is identical to Vail counting type in a print shop — measure first, then encode. The difference is only that the machine does the measuring for you.
Three practical rules fall out of this:
- Know your distribution before you pick a codec. Text, JSON, images and audio have wildly different frequency profiles. A codec tuned for one is mediocre for another. This is why gzip on an already-compressed JPEG does nothing — the data has no exploitable frequency skew left.
- Short codes for frequent symbols is the whole game. Every optimisation in this space — dictionary matches, run-length encoding, entropy coding — is a variation on making the common case cheap. When you are designing a wire format or a compact storage schema, start by asking what repeats.
- Budget for framing. A format that cannot be unambiguously split into records is a format that will corrupt silently. Morse code's gaps, JSON's delimiters, protobuf's length prefixes — same job, different century.
Try it yourself
The fastest way to internalise this is to build the encoder. A Morse encoder is about forty lines of code in any language, and it makes the abstract concrete: you can hear the compression working as the common letters fly past in a single blip and the rare ones drag out over four elements. If you want a ready-made reference to check your timing against, the free Morse code translator at morsezone.com lets you type text and hear it keyed at a set speed — a useful ear-training check while you build.
Then take the next step: modify the Python script above to compute the average codeword length for your own data — your codebase, your log files, your chat history. Watch how the optimal code changes when the distribution changes. That shift is the entire history of compression in one experiment, and it is the reason no single codec wins everywhere.
The takeaway
Morse code is usually taught as a historical curiosity — a code for hobbyists and survivalists. It is that, but it is also something more useful to a working developer: the oldest surviving example of a variable-length code, built by measuring real data and optimising for the common case, with a framing layer made of silence. Huffman formalised the idea. Shannon proved it optimal. But the telegraph operators shipped it first, in 1844, on a wire that could only say yes or no.
The next time you reach for a compression library, remember that you are standing on the shoulders of people who counted letters in a print shop to save seconds on a wire. The tools got faster. The idea never changed.
Morse Zone builds free Morse code tools and learning guides at morsezone.com — including a translator, alphabet chart, and practice drills for beginners.
Top comments (0)