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

Chapter 06 — Functional Data Structures: List

In this chapter we introduce functional data structures in their canonical form. While Java provides List, ArrayList, and LinkedList, none of these are functional: they are mutable, operationally defined, and do not express structure in types.

A functional List is defined by two constructors only:

  • Nil — the empty list
  • Cons(head, tail) — an element followed by the rest of the list

This is the same structure as in Haskell or Scala, and underlies structural recursion.

6.1 The Algebraic Data Type

public sealed interface List<A> permits List.Nil, List.Cons {

    record Nil<A>() implements List<A> {}

    record Cons<A>(A head, List<A> tail) implements List<A> {}
}

This gives us:

  • Immutability by design
  • Total structural description of the data
  • A foundation for structural recursion

Compared to Java’s built‑in collections, the important difference is the absence of hidden mutation. All operations return new lists rather than modifying existing ones.

6.2 Basic Operations

The classical operations are:

  • prepend
  • map
  • foldRight and foldLeft
  • append
  • reverse

Example: map

public static <A, B> List<B> map(List<A> list, Function<A, B> f) {
    return switch (list) {
        case List.Nil<A> ignored -> new List.Nil<>();
        case List.Cons<A> cons   -> new List.Cons<>(f.apply(cons.head()), map(cons.tail(), f));
    };
}

Because the ADT is small and total, recursion is exhaustive — covering all possible cases — making missing cases structurally impossible.

6.3 Folding as the Core Abstraction

foldRight expresses the essence of structural recursion:

public static <A, B> B foldRight(List<A> list, B z, BiFunction<A, B, B> f) {
    return switch (list) {
        case List.Nil<A> ignored -> z;
        case List.Cons<A> cons   -> f.apply(cons.head(), foldRight(cons.tail(), z, f));
    };
}

Many operations—including map, length, and append—can be implemented via folds.

This is the “functional data structure lesson”:
data type dictates recursion scheme.

6.4 Performance Considerations

Functional lists have:

  • O(1) prepend
  • O(n) append
  • O(n) indexing

Unlike Java’s LinkedList, our ADT has no hidden machinery and no mutability. The tradeoff is intentional: structural clarity over operational tuning.

In FP languages, lists serve as semantic glue: they appear in interpreters, compilers, symbolic computing, and DSLs.

6.5 Why This Matters for FP in Java

Java 21’s sealed types make this pattern much more natural:

  • Exhaustive switch becomes safe
  • ADTs become explicit instead of accidental
  • Recursion is clearer and less error‑prone

Even so, Java’s pattern-matching remains less expressive than that of Scala, Kotlin, or Haskell, and its type system does not support higher-kinded abstraction. Within these constraints, however, the core ideas translate well, and the resulting code remains surprisingly clean.


Next we extend these ideas to Tree structures and structural recursion, which generalizes the same principles to branching shapes.