Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tuples vs. Lists: A Brief Philosophical Digression

You might wonder, with the innocent optimism of someone who hasn’t yet been burned by premature optimization, why we need tuples when we have lists. This is an excellent question that deserves a nuanced answer.

Tuples are for when you know exactly how many things you have. They’re fixed-size, stored contiguously in memory, and accessing any element takes constant time. Think of them as arrays that have given up on the whole “mutable” thing and embraced a life of peaceful immutability.

Lists are for when you don’t know how many things you have, or when you might want to add more. They’re variable-size, stored as linked chains of cons cells (more on that later, dear reader), and accessing the Nth element requires traversing N-1 elements first.

Use tuples for records, small fixed collections, and return values from functions. Use lists for sequences, collections of arbitrary size, and whenever you feel like recursing through your data like a functional programming warrior of old.