A Harmonica as a Map of Parts
A disassembled harmonica looks orderly for about thirty seconds. Then the covers, reed plates and comb are on the bench, and a reed that needs attention has to be connected back to the plate it belongs to.
An adjacency list is a way to write down those connections. Each part has a list of its immediate neighbours. Here, an arrow means “contains or supports”: the comb leads to two plates, and each plate leads to two reeds. Real harmonicas have more parts and vary in construction; this is a deliberately small map for a repair notebook.
use std::collections::BTreeMap;
fn main() {
let parts = BTreeMap::from([
("comb", vec!["blow plate", "draw plate"]),
("blow plate", vec!["blow reed 1", "blow reed 2"]),
("draw plate", vec!["draw reed 1", "draw reed 2"]),
]);
for part in &parts["draw plate"] {
println!("Inspect {part}");
}
}
Both examples print the two draw reeds. After cleaning the draw plate, that list gives me a place to record which reeds still need an alignment or gap inspection. It does not tell me what the gaps should be: that depends on the instrument and how it is played.
Haskell can express the same map as a list of pairs. The lookup may fail, so the empty list supplies a sensible result for a part with no recorded children.
main :: IO ()
main = do
let parts = [("comb", ["blow plate", "draw plate"]),
("blow plate", ["blow reed 1", "blow reed 2"]),
("draw plate", ["draw reed 1", "draw reed 2"])]
let reeds = maybe [] id (lookup "draw plate" parts)
mapM_ (putStrLn . ("Inspect " ++)) reeds
Adjacency lists became a standard way to represent graphs as graph algorithms developed through the 1970s. They are compact when each item has only a few connections, and finding a part’s neighbours is straightforward. This map records arrows in one direction, though: to find a reed’s plate, I would need to search the lists or store reverse connections too. On a crowded workbench, that extra direction might earn its keep.