liveBook · Manning

List

As explained in Clojure: The Essential Reference

A list is a Clojure sequential collection whose elements are traversed in order and which is optimized for access at the ends.

A Clojure list is a concrete collection that is also a sequence. Use `list` to create a `clojure.lang.PersistentList` from zero or more arguments, including `nil`. With no arguments, it returns an empty list. Lists also have a language-level role: Clojure code is transformed into lists and symbols before compilation. section 9.4 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

The diagram shows the linked chain behind a list, including the specialized empty-list tail. section 9.4.1

Create a list and push onto its head

`(list 1 2 3)` → `(1 2 3)`; `(list)` → `()`; `(conj (conj () 1) 2)` → `(2 1)`. Each `conj` adds its item at the head, so successive pushes appear in reverse order. section 9.4.1

That head-first behavior suggests a few practical choices:

  • Use a list as a stack: `conj` pushes, while `peek` and `pop` inspect and remove the top. A nearest-smaller-value search uses this pattern to discard values that cannot be the next answer. section 9.4.1
  • Use `(into () coll)` to reverse a collection by repeatedly pushing its elements onto an empty list. section 9.4.1
  • Distinguish `list` from `cons` and `list*`: they produce compatible sequential results, but `cons` normally builds `clojure.lang.Cons` cells. A list supports constant-time `count` and optimized reduction; `cons` is used chiefly to build lazy sequences. section 9.4 section 9.4.2

How Clojure: The Essential Reference covers it

How deep it goes
Defines construction and return behavior, works through head insertion and stack uses, and contrasts list with cons on counting and reduction.
Examples
Demonstrates head insertion, reversal with into, nearest-smaller-value search, and delimiter checking with a stack.
What it assumes
Assumes familiarity with Clojure collection calls while defining list's inputs, outputs, and empty case.
What you can do after
Build it from scratch with list and conj, then use head-first operations to implement a stack or reverse a collection.
How it connects
Connects lists to sequences, stacks, lazy sequences, cons, list*, conj, and into.

Also explained in

Compare how 21 books explain 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.