Arrays
In the grand tapestry of Erlang data structures, arrays occupy a peculiar position—rather like finding a perfectly functional bicycle in a showroom full of hovercrafts. They work, certainly. They do their job with admirable efficiency. But you can’t help wondering if perhaps you’ve wandered into the wrong universe, one where zero-based indexing is considered normal and mutable-looking structures somehow maintain their functional purity through sheer force of will.
Arrays in Erlang (and by extension, LFE) are functional, extendible data structures that provide O(log n) access to elements via numeric indices. They were designed for situations where you absolutely, positively need to access element 42 without first traversing through elements 0 through 41, and where the list’s charming linked-list aesthetic just won’t cut it. Think of them as lists that went to business school and came back wearing sensible shoes.
A Brief Philosophical Interlude
Before we dive into the mechanics, it’s worth pondering why arrays exist in a language so thoroughly committed to lists that it named itself after one. The answer, as with most things in life, comes down to pragmatism wrestling with ideology in a dimly lit car park behind the theoretical computer science building.
Lists are magnificent for sequential processing, for building data structures one cons cell at a time like a particularly methodical coral reef. But if you need random access—if you need to leap directly to element 1,000 without the tedious business of hopping through 999 predecessors—lists begin to look rather less magnificent and rather more like a very long queue at the Department of Motor Vehicles.
Arrays solve this. They provide indexed access with logarithmic complexity, which in practical terms means you can access any element in a 100,000-element array as quickly as you can access the tenth element. This is achieved through a clever tree structure that would make a botanist weep with joy, though naturally, the implementation details are hidden away like a magician’s card tricks—best appreciated without too much scrutiny.