Erlang / Erlang Basics Interview questions
What are lists in Erlang?
A list is a variable-length, ordered sequence written in square brackets, such as
[1, 2, 3] or [apple, banana]. Internally it's a singly linked chain of cons cells
— each cell holds a head element and a pointer to the rest of the list, ending in the empty list
[].
That linked structure is why prepending is cheap and appending to the end is not:
[Head | Tail] = [1, 2, 3], % Head = 1, Tail = [2, 3] New = [0 | [1, 2, 3]]. % O(1) prepend -> [0,1,2,3]
Because lists have no fixed length, they're the natural fit for sequences of unknown or varying size, while tuples suit fixed-shape data. Most list processing in Erlang is written recursively, walking the head/tail structure one element at a time, or via list comprehensions.
More Related questions...