<p>A zipper, in general, is a data structure with a hole in it. Zippers are used for traversing/manipulating data structures, and the hole corresponds to the current <em>focus</em> of the traversal. Typically there is also an element of the data structure under consideration, so that one has a (list) zipper and a list or a (tree) zipper and a tree. The zipper allows the programmer to efficiently move around the data structure, even replacing the element at the focus. The pair of the zipper and the element in the focus satisfy the constraint that placing the element at the focus in the hole gives the original data structure.</p>

<p>Zippers can be generalised to arbitrary inductive data types. The concept can be defined in type-indexed fashion (See <a href="http://www.cs.nott.ac.uk/~gmh/wgp01/jeuring-paper.pdf">type-indexed data types</a>). They are also related to the idea of <a href="http://strictlypositive.org/diff.pdf">the derivative of a data structure</a>, and has been studied from a <a href="http://www.cs.nott.ac.uk/~txa/publ/tlca03.pdf">Category Theoretic perspective</a>.</p>

CC BY-SA 3.0
2012-12-08T21:11:03.140Z
2012-12-08T21:17:10.777Z
2012-12-08T21:17:10.777Z
Dave Clarke
<p>A zipper is in general a <em>pair</em> of things: it's a structure-with-a-hole, a <em>focus</em>, representing where in the structure you are, together with a <em>path</em>, recording how you got to that focus. (This path is LYAH's trail of breadcrumbs.)</p>

<p>The path is how you actually apply changes to the structure: "go down, go left, increment the value". By repeatedly applying "go up" (<code>go_up</code> in <a href="https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf" rel="nofollow noreferrer">Huet's paper</a>) at this point, you can retrace your steps and end up with a new, mutated, copy of the original structure.</p>

<p>They can indeed be generalised to other structures:</p>

<ul>
<li><a href="http://michaeldadams.org/papers/scrap_your_zippers/" rel="nofollow noreferrer">Scrap Your Zippers</a> uses generic programming in Haskell</li>
<li><a href="http://clojure.github.io/clojure/clojure.zip-api.html" rel="nofollow noreferrer">Clojure's zipper</a> gives you a "pluggable" zipper: when you <a href="http://clojure.github.io/clojure/clojure.zip-api.html#clojure.zip/zipper" rel="nofollow noreferrer">create a zipper on a structure</a>, you pass in functions specific to your structure</li>
</ul>

CC BY-SA 3.0
2014-08-12T10:26:45.173Z
2017-10-06T16:28:41.950Z
2017-10-06T16:28:41.950Z
Frank Shearar
<p>I was reading a chapter in <a href="http://learnyouahaskell.com/zippers#taking-a-walk">LYAH</a> which didn't really make sense to me. I understand that zippers can arbitrarily traverse a tree-like structure, but I need some clarification on it. Also, can zippers be generalized to any data structure?</p>

CC BY-SA 3.0
2012-12-08T20:59:32.353Z
This is probably more appropriate for [CS.SE], though the work generalising zippers involves quite some technical machinery.
Dave Clarke
CC BY-SA 3.0
2012-12-08T22:44:02.027Z
A zipper is something that you should always keep closed, especially when traversing a tree.
Andrej Bauer
<ds.data-structures><functional-programming><tree>
What is a zipper, and how does it relate to a tree-like structure?
n_ary_crazy
# What is a zipper, and how does it relate to a tree-like structure?

(Asked by: username_0 on 2012-12-08)

I was reading a chapter in [LYAH](http://learnyouahaskell.com/zippers#taking-a-walk) which didn't really make sense to me. I understand that zippers can arbitrarily traverse a tree-like structure, but I need some clarification on it. Also, can zippers be generalized to any data structure?

**Tags:** ds.data-structures, functional-programming, tree
> **Comments:**
> **username_1**: This is probably more appropriate for [CS.SE], though the work generalising zippers involves quite some technical machinery.  
> **username_2**: A zipper is something that you should always keep closed, especially when traversing a tree.  

---

## 2 answers

### Answer by username_1 on 2012-12-08. Score: 15

A zipper, in general, is a data structure with a hole in it. Zippers are used for traversing/manipulating data structures, and the hole corresponds to the current *focus* of the traversal. Typically there is also an element of the data structure under consideration, so that one has a (list) zipper and a list or a (tree) zipper and a tree. The zipper allows the programmer to efficiently move around the data structure, even replacing the element at the focus. The pair of the zipper and the element in the focus satisfy the constraint that placing the element at the focus in the hole gives the original data structure.

Zippers can be generalised to arbitrary inductive data types. The concept can be defined in type-indexed fashion (See [type-indexed data types](http://www.cs.nott.ac.uk/~gmh/wgp01/jeuring-paper.pdf)). They are also related to the idea of [the derivative of a data structure](http://strictlypositive.org/diff.pdf), and has been studied from a [Category Theoretic perspective](http://www.cs.nott.ac.uk/~txa/publ/tlca03.pdf).


---

### Answer by username_3 on 2014-08-12. Score: 2

A zipper is in general a *pair* of things: it's a structure-with-a-hole, a *focus*, representing where in the structure you are, together with a *path*, recording how you got to that focus. (This path is LYAH's trail of breadcrumbs.)

The path is how you actually apply changes to the structure: "go down, go left, increment the value". By repeatedly applying "go up" (`go_up` in [Huet's paper](https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-zipper.pdf)) at this point, you can retrace your steps and end up with a new, mutated, copy of the original structure.

They can indeed be generalised to other structures:

* [Scrap Your Zippers](http://michaeldadams.org/papers/scrap_your_zippers/) uses generic programming in Haskell
* [Clojure's zipper](http://clojure.github.io/clojure/clojure.zip-api.html) gives you a "pluggable" zipper: when you [create a zipper on a structure](http://clojure.github.io/clojure/clojure.zip-api.html#clojure.zip/zipper), you pass in functions specific to your structure


---

