Jargon: What is Persistent Data Structure (2016)

By Xah Lee. Date: . Last updated: .

“Persistent Data Structure” should be called “Data Structure by Copy”

xah's edu corner extempore, on persistent data structure

wouldn't it be nice, if your lang's array/list/hash/dictionary etc functions, all just return a copy instead?

what's wrong with references? you got the problem of shallow/deep copy, pass by value/ref, val of ref

basically, you can't be sure when you modified a list, some other list is modified too or not. diff language deal with this differently. very confusing.

why didn't all langs just always do copy? cuz that's too slow. programer haven't been able to solve the problem in the past.

this is the primary reason, the concept of reference came into programer's brain. by-product of computer engineering, so to speak.

persistent data structure is about 2 things. (1) all functions return a copy. (2) implementation to make this very efficient.

all functions return a copy isn't new. Mathematica since 1990 does that.

all you need to know about persistent data structure is just all list functions always return a copy. nice n simple.

persistent data structure is rather new. popularized by clojure. JavaScript has immutable.js

[see Clojure Tutorial]

PostScript: the word persistent is a misnomer. It's better called “data structure by copy”.

explain persistent data structure

explain persistent data structure

(ai answer)

Persistent data structures keep every previous version of the data when you “modify” it. Instead of overwriting the old structure in place, an update creates a new version while the old one remains accessible and unchanged.

Ephemeral vs Persistent

How They Stay Efficient

Naïve copying of the entire structure would be too slow. Real persistent structures use structural sharing:

This makes updates typically (O(\log n)) for trees (or (O(1)) amortized for some structures) instead of (O(n)).

Degrees of Persistence

Type What you can do
Partial Query any past version; only the latest version can be updated
Full Update any version (each update creates a new branch)
Confluent Merge versions together (like in version-control systems)

Common Examples

Why Use Them

In short: a persistent data structure treats every update as the creation of a new version, made practical by sharing unchanged parts of the structure.

which programming language actually lets you access the history of a data structure structure

Clojure is the language that most directly and idiomatically gives you access to the history of data structures.

Why Clojure?

(def v1 [1 2 3])
(def v2 (conj v1 4))   ; new version
(def v3 (assoc v2 0 10))

;; All three versions remain available and independent:
v1  ; => [1 2 3]
v2  ; => [1 2 3 4]
v3  ; => [10 2 3 4]

Because of structural sharing, this is efficient (logarithmic time, not full copies).

Other languages with strong support

Language Support level Notes
Haskell Excellent Everything is immutable; history is just retained references
Scala Very good Immutable collections + libraries (e.g. ScalaZ, Cats)
F# Good Immutable collections; more imperative culture
Elixir Good Immutable data + process isolation
PureScript / Elm Excellent Pure functional, persistent by nature

Bottom line

Any language with immutable data and structural sharing can do it, but Clojure is the one where “keep the old version and you have the history” is the normal, everyday way of writing code.

so to access history of a data structure you actually have to save it as a variable , is that right?

Yes, exactly.

In a persistent data structure, the language doesn’t automatically keep a log of every change for you. You have to hold onto the previous versions yourself by keeping references (variables, atoms, list entries, etc.) to them.

When you “update” the structure, the operation returns a new version. The old one only survives if something still points to it.

(def history (atom []))          ; we’ll store the versions ourselves

(def v1 [1 2 3])
(swap! history conj v1)          ; save v1

(def v2 (conj v1 4))
(swap! history conj v2)          ; save v2

(def v3 (assoc v2 0 10))
(swap! history conj v3)          ; save v3

;; Now you can look at any past version:
@history
;; => [[1 2 3] [1 2 3 4] [10 2 3 4]]

Without explicitly retaining those references, the old versions become unreachable and are garbage-collected — just like any other unused data. The persistence only guarantees that if you keep a reference, that version stays valid and unchanged.