Wikipedia / Zipping (computer science)
Wikipedia / Zipping (computer science)
In computer science, zipping is a function which maps a tuple of sequences into a sequence of tuples. This name zip derives from the action of a zipper in that it interleaves two formerly disjoint sequences. The inverse function is unzip.
Example
Given the three words cat, fish and be where |cat| is 3, |fish| is 4 and |be denote the length of the longest word which is fish. The zip of cat, fish, be is then 4 tuples of elements:
where # is a symbol not in the original alphabet. In Haskell:
Definition
Let be an alphabet, # a symbol not in .
Let x1x2... x|x|, y1y2... y|y|, z1z2... z|z|, ... be n words (i.e. finite sequences) of elements of denote the length of the longest word, i.e. the maximum of |x|, |y|, |z|, ... .
The zip of these words is a finite sequence of n-tuples of elements of :
,
where for any index , the wi is #.
The zip of x, y, z, ... is denoted zip(x, y, z, ...) or x ⋆ y ⋆ z ⋆ ...
The inverse to zip is sometimes denoted unzip.
A variation of the zip operation is defined by:
is the minimum.
In programming languages
Zip functions are often available in programming languages, often referred to as . In Lisp-dialects one can simply the desired function over the desired lists, is variadic in Lisp so it can take an arbitrary number of lists as argument. An example from Clojure:
In Common Lisp:
Languages such as Python provide a function. in conjunction with the operator unzips a list:
Haskell has a method of zipping sequences but requires a specific function for each arity ( for two sequences, for three etc.), similarly the functions and are available for unzipping:
Language comparison
List of languages by support of zip:
See also
References