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
6
votes
2 answers

Is recursion a smell (in idiomatic Clojure) because of of zippers and HOFs?

The classic book The Little Lisper (The Little Schemer) is founded on two big ideas You can solve most problems in a recursive way (instead of using loops) (assuming you have Tail Call Optimisation) Lisp is great because it is easy to implement in…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
6
votes
1 answer

zippers: mapping over last breadcrumb

I've bumped into an issue using zippers and lens. Consider following example: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeOperators #-} import Control.Lens import Control.Zipper data A = AA { _aa :: A } | AB { _ab :: B } …
remdezx
  • 2,939
  • 28
  • 49
6
votes
1 answer

stepping into zipper with `to` lens

I'm struggling with lens and zippers. Consider below code run in ghci > import Control.Lens > import Control.Zipper > > :t within (ix 1) $ zipper ([1,2,3] :: [Int]) > within (ix 1) $ zipper ([1,2,3] :: [Int]) :: Control.Monad.MonadPlus m => m…
remdezx
  • 2,939
  • 28
  • 49
6
votes
2 answers

Haskell Zipper for ADT with many constructors

I have a few ADT's that represent a simple geometry tree in Haskell. Something about having my operation types separate from the tree structure is bothering me. I'm thinking of making the Tree type contain constructors for the operators,it just…
MFlamer
  • 2,340
  • 2
  • 18
  • 25
5
votes
1 answer

Postorder tree traversal with clojure.zip to edit nodes

I have a tree represented as a nested vector. I want to have a generalization of indexed for trees, showing the index of each node like this, (visit 42); => [0 42] (visit [6 7]); => [0 ; [[0 6] ; [1 7]]] The…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
5
votes
1 answer

What evidence is there that Clojure Zippers would benefit from being expressed as comonads?

In this presentation [2005] we read at slide 32: The zipper datatype hides a comonad. This is exactly the comonad one needs to structure attribute evaluation. So it seems you can express Zippers in terms of Comonads. This even seems possible in…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
5
votes
1 answer

Implementing efficient zipper like data structure in Haskell with O(1) elements access

The question I want to create a datatype, which will allow for fast access and modification of its elements. Is it possible to create in Haskell a structure and functions, which will perform as fast as simple C++ implementation? Problem details I'm…
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
5
votes
1 answer

Traversable and zippers. Necessity and sufficiency

Every type T overloading the Traversable brings forth a Zipper T. I.e. existence of the instance Traversable T is the sufficient condition of the Zipper T. Is there a proof that this is also a necessary condition? (I suppose it must be pretty…
user3974391
  • 701
  • 4
  • 14
5
votes
5 answers

How to build Abstract Syntax Trees from grammar specification in Haskell?

I'm working on a project which involves optimizing certain constructs in a very small subset of Java, formalized in BNF. If I were to do this in Java, I would use a combination of JTB and JavaCC which builds an AST. Visitors are then used to…
Vamshi Surabhi
  • 437
  • 4
  • 15
4
votes
2 answers

Haskell: Creating Type Classes for Zippers

So I've been reading a bit about the Zipper pattern in Haskell (and other functional languages, I suppose) to traverse and modify a data structure, and I thought that this would be a good chance for me to hone my skills at creating type classes in…
rampion
  • 87,131
  • 49
  • 199
  • 315
4
votes
1 answer

PHP can't access newly created zip file

Im using Laravel 5.2 and Zipper to create ZIP archive in a fly and download it by user. I think this problem is generall not strictly related to Laravel or Zipper. Steps: User click link download_all. First php script create archive. Next the same…
Adiasz
  • 1,624
  • 1
  • 12
  • 21
4
votes
1 answer

Clojure: zipper -> html

After a few days of trying to wrap my brain around zippers, I think I finally understand how to create them from sequential data. However, after searching for a few days, I can't seem to find any resources on how convert a zipper into something…
Sean Hagen
  • 752
  • 10
  • 30
4
votes
1 answer

How do you zip through a bidimensional grid purely functionally?

Using the list zipper, for example, one is able to walk through a unidimensional space. Is there any similarly elegant and efficient way to encode the notion of walking (with no pattern) through a bidimensional grid?
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
4
votes
1 answer

Clojure data zip xml composition

Here's an example to illustrate what I wanted to do: (ns sample (:require [clojure.zip :as zip] [clojure.data.zip.xml :refer [attr text xml-> xml1->]] [clojure.data.xml :as xml])) ;; From…
levant pied
  • 3,886
  • 5
  • 37
  • 56
4
votes
2 answers

returning multiple values using clojure xml zipper

Lets suppose we have some XML like so: text text ... lots of cruft here .. ... Now, looking through the…
Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
1 2
3
8 9