Jargon: What is Persistent Data Structure (2016)
“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.
- Python: Copy Nested List, Shallow Copy vs Deep Copy
- Python: dictionary.clear vs Set Dictionary to Empty
- JS: Array Compare Equality
- JS: Test equality of array and object by content 📜
- JS: Clone Object
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
- Ephemeral (ordinary mutable structures):
list.append(x)ortree.insert(key)mutates the existing object. After the change, the previous state is gone. - Persistent: The same operation returns a new structure. The original is still valid and can be used or inspected independently. Multiple versions coexist.
How They Stay Efficient
Naïve copying of the entire structure would be too slow. Real persistent structures use structural sharing:
- Only the nodes along the path from the root to the changed location are copied.
- Unchanged subtrees are shared between the old and new versions (they are immutable, so sharing is safe).
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
- Immutable linked lists / stacks (cons cells in Lisp/Haskell)
- Persistent maps and vectors (Clojure, Scala, PureScript, etc.)
- Persistent red-black trees, tries, ropes
- Versioned data in systems such as Git, databases with MVCC, or undo stacks
Why Use Them
- Safe concurrent access (no locks needed for readers)
- Easy undo/redo and time-travel debugging
- Referential transparency and simpler reasoning in functional code
- Natural fit for pure functional languages
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?
- Its core collections (vectors, maps, sets, lists) are persistent by default.
- Every “update” (
conj,assoc,dissoc, etc.) returns a new version while the old one stays fully usable. - You simply keep a reference to an earlier version and you have access to that historical state — no special API required.
(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.