Structural sharing reuses unchanged substructures between immutable data structures instead of copying them. In a singly linked list, prepending 1 returns Cons(1, xs), reusing xs; removing the head returns its tail. Existing references remain unchanged, so the structure is persistent. section 3.3 section 3.6
Sharing improves some operations while imposing copying costs where the linked structure cannot be reused:
- tail can return the existing tail in constant time, without copying. section 3.3
- drop and dropWhile copy only the portion they traverse, rather than the entire list. section 3.3.1
- Appending a1 to a2 copies the nodes of a1, then points the result at a2. section 3.3.1
- Replacing the tail near the end, as init requires, copies every preceding Cons; efficient functional structures balance such trade-offs across operations. section 3.3.1 section 3.6
Appending by sharing the suffix
def append[A](a1: List[A], a2: List[A]): List[A] = a1 match case Nil => a2 case Cons(h, t) => Cons(h, append(t, a2)) The recursion rebuilds only a1. When a1 is exhausted, the result reuses a2 directly. section 3.3.1
Use the list-sharing figure to visualize how immutable list results retain unchanged structure. section 3.3


