liveBook · Manning

Persistent list

As explained in Clojure: The Essential Reference

clojure.lang.PersistentList is Clojure's immutable persistent singly linked-list implementation.

A persistent list is Clojure’s immutable, singly linked list. The list function creates a clojure.lang.PersistentList from zero or more arguments, including nil; with no arguments it returns the empty list. Lists are central to Clojure code and functional arguments, but vectors, sets, maps, and lazy sequences are often more flexible for everyday collection work. section 9.4.1

Understand its behavior through the operations that make lists useful:

  • Links elements into a chain, with each newly added element at the head; the empty list uses a specialized EmptyList tail. section 9.4.1
  • Uses conj to add at the head, so repeated additions print in reverse insertion order. section 9.4.1
  • Supports peek and pop, making it a natural last-in-first-out stack. section 9.4.1
  • Provides the concrete result of reverse: a persistent list containing the input items in reverse order. section 8.3.8
  • Requires sequential input for reverse and throws IllegalArgumentException for values without a sequential version. section 8.3.8

Head insertion and reversal

(-> () (conj 1) (conj 2) (conj 3)) ;; (3 2 1) (defn rev [coll] (into () coll)) (rev (range 10)) ;; (9 8 7 6 5 4 3 2 1 0) Because into repeatedly applies conj, inserting into an empty list reverses the source collection. section 9.4.1

A stack for nearest-smaller values

A list can hold values that are pushed and removed from the head while processing a sequence. The nearest-smaller algorithm pushes each input value, pops larger candidates, and peeks at the nearest remaining smaller value. For [0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15], it returns (0 0 4 0 2 2 6 0 1 1 5 1 3 3 7 7). section 9.4.1

Figure 9.6. A list formed from a chain of PersistentList objects. The EmptyList is a specialized version of PersistentList to handle the tail position.
31 lists cells

Inspect the linked chain of PersistentList objects and the specialized EmptyList tail. section 9.4.1

How Clojure: The Essential Reference covers it

How deep it goes
Explains construction, head insertion, stack operations, reversal, exceptions, and linked-list representation, with performance details deferred for reverse.
Examples
Demonstrates head insertion, collection reversal, nearest-smaller search, DNA reverse-complement processing, and delimiter checking.
What you can do after
Build it from scratch with list, conj, into, peek, and pop to implement reversal and last-in-first-out processing.
How it connects
Connects lists with sequences, lazy sequences, vectors, sets, hash-maps, stacks, conj, peek, pop, cons, and reverse.

Also explained in

Compare how 2 books explain persistent list →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.