Questions tagged [zipper]

A zipper is a technique of representing a data structure so that it is convenient for traversal and updates, especially in pure functional languages.

A zipper is a technique of representing an aggregate data structure so that it is convenient for writing programs that traverse the structure arbitrarily and update its contents, especially in purely functional programming languages.

The basic idea is that an existing list- or tree-like data structure is augmented with a representation of a "focus" on a single item in the tree in a way that permits efficient lookups and updates at the focus point.

A simple example is a zipper for a list, consisting of a pair of two lists. For example, the position of 3 in the list (1,2,3,4,5) is represented by the pair ((2,1),(3,4,5)). To insert a new item or replace 3 with another item with this representation can be done in constant time since 3 is at the head of the second list of the pair. Traversing the list is similarly easy: remove the head of either list and cons it with the other list.

You can read more about zipper in Haskell in the Haskell Wikibook.

126 questions
2
votes
4 answers

what is the best C++ alternative to python dictionary and zip?

What is the best C++ alternative to dict(zip(values...)) in python? I'm tutoring a C++ student currently in my off time, and came across a piece of Python code at my work and found I did not know the best answer. The code looks like the following (I…
Brian Deragon
  • 2,929
  • 24
  • 44
2
votes
1 answer

questions on move up method in Zipper

I am reading the Zipper article in Haskell Wiki and I can't understand the up method defined as: data Tree a = Fork (Tree a) (Tree a) | Leaf a data Cxt a = Top | L (Cxt a) (Tree a) | R (Tree a) (Cxt a) type Loc a = (Tree a, Cxt a) up :: Loc a ->…
Sawyer
  • 15,581
  • 27
  • 88
  • 124
2
votes
2 answers

Redundant information in zipper data type in Data.Tree.Zipper?

In Data.Tree.Zipper the zipper data type for a rose tree is data TreePos t a = Loc { _content :: t a -- ^ The currently selected tree. , _before :: Forest a , _after :: Forest a , _parents :: [(Forest a, a, Forest a)] }…
Aton
  • 1,125
  • 8
  • 18
2
votes
1 answer

Printing a tree lazily in Newick format

I wish to print a binary tree in Newick format, showing each node's distance to its parent. At the moment I haven't had an issue with the following code, which uses regular recursion, but a tree too deep may produce a stack overflow. (defn…
Bruno Kim
  • 2,300
  • 4
  • 17
  • 27
2
votes
1 answer

Multiple zippers on big structure using Haskell

Possible Duplicate: Zipper like data structure with more then one cursor Let T a big tree (or another big data structure). Suppose you have some interest points in it (P = {p1, p2, ...} where pN in T). You can use zippers for each pN but, what if…
josejuan
  • 9,338
  • 24
  • 31
2
votes
1 answer

Densely packed tree of signals

I collect realtime signals, compute derived signals and store both raw and derived data in a circular buffer, so I hold only last million of samples. Sometimes I need to serialize current values for all signals. So I need something like: type D0 a…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
2
votes
2 answers

Multiple mutations to an XML document in Clojure

I'm trying clojure (for the first time) for a simple project. I need to update an xml tree given a csv file. I'm reading the csv file line by line, extract some values, loop up a node given some values and insert a child node with another…
Jeroen
  • 557
  • 1
  • 7
  • 15
1
vote
1 answer

Traverse a rose tree until some condition is met, then modify tree

I have two rose trees in Haskell of m and n nodes, respectively. I want to replace the ith node of the first tree with the jth node of the second tree. e.g. tree 1: R …
ayaye
  • 11
  • 1
1
vote
4 answers

How to get the part of a Clojure zipper already visited in depth-first traversal?

When you iterate through an arbitrarily nested Clojure zipper in a depth-first fashion via z/next, can you get or reconstruct the already visited part of the zipper, preserving its structure? For example, let's have a vector zipper of [0 [1 2] 3].…
Jindřich Mynarz
  • 1,563
  • 1
  • 16
  • 31
1
vote
1 answer

Why is the second type of a Zipper a list of data not not pure data?

I made my way thru the Tutorial available at Learn your Haskell and I ask myself why the Author uses a list as the second type for the implemented Zipper? Here is the relevant Code: type Name = String type Data = String data FSItem = File Name Data …
Koschi13
  • 549
  • 1
  • 4
  • 21
1
vote
3 answers

Clojure zipper to remove all right siblings

I want to write a function for a zipper that removes all right siblings of a node while staying at the same location. (defn remove-all-rights-1 [loc] (if (zip/right loc) (recur (zip/remove (zip/right loc))) loc)) The problem here is that…
erdos
  • 3,135
  • 2
  • 16
  • 27
1
vote
2 answers

Clojure parse nested vectors

I am looking to transform a clojure tree structure into a map with its dependencies For example, an input like: [{:value "A"} [{:value "B"} [{:value "C"} {:value "D"}] [{:value "E"} [{:value "F"}]]]] equivalent to: :A :B :C :D …
Rahul S
  • 11
  • 1
1
vote
5 answers

Handling for redundancy in a list

Lets say I have a list of tuples with states and counties: stList = [('NJ', 'Burlington County'), ('NJ', 'Middlesex County'), ('VA', 'Frederick County'), ('MD', 'Montgomery County'), ('NC', 'Lee County'), ('NC', 'Alamance County')] For each of…
gwydion93
  • 1,681
  • 3
  • 28
  • 59
1
vote
1 answer

Ways to (refine? is it?) type variables to concrete types in Idris? For a dependently typed HOAS Zipper

I have the following: data Expr : Type -> Type where Lift : a -> Expr a Add : Num a => Expr a -> Expr a -> Expr a And : Expr Bool -> Expr Bool -> Expr Bool Cnst : Expr a -> Expr b -> Expr a data Context : Type -> Type where Root :…
1
vote
1 answer

Empty children and Clojure zippers

Why the last expression retruns {:a :foo, :args [{:id :XX}], :id :XX} instead of: {:a :foo, :args [], :id :XX} (require '[clojure.zip :as zip]) (defn my-zipper [tree] (zip/zipper (fn branch? [node] (:args node)) (fn children…
Lambder
  • 2,953
  • 1
  • 26
  • 20
1 2 3
8 9