Safe Haskell | None |
---|---|

Language | Haskell2010 |

The feature of this module is `ZigZag`

and its class instances. It
is an abstract data type and can be constructed / deconstructed
by `fromList`

/ `toList`

or `fromDiagonals`

/ `toDiagonals`

. See the
associated documentation for more information.

- diagonals :: [[a]] -> [[a]]
- fromDiagonals :: [[a]] -> ZigZag a
- fromList :: [a] -> ZigZag a
- toDiagonals :: ZigZag a -> [[a]]
- toList :: ZigZag a -> [a]
- data ZigZag a

# Documentation

diagonals :: [[a]] -> [[a]] Source #

Finds the diagonals through a ragged list of lists.

For example, the diagonals of:

[ [0,1,2] , [] , [3,4] , [5,6,7] ]

Are:

[ [0] , [1] , [3,2] , [5,4] , [6] , [7] ]

Which can be seen intuitively.

This algorithm works by storing a list of tails of rows already seen. To find the next diagonal we take the head of the next row plus the head of each stored tail. The tail remainders are stored plus the remainder of the new row.

If there are no more rows but some remaining tails we then iteratively form diagonals from the heads of each tail until there are no tails remaining.

Applied to the example:

Row | Output | Remaining --------+--------+---------------- [0,1,2] | [0] | [[1,2]] [] | [1] | [[2]] [3,4] | [3,2] | [[4]] [5,6,7] | [5,4] | [[6,7]] x | [6] | [[7]] x | [7] | []

fromDiagonals :: [[a]] -> ZigZag a Source #

Convert a list of diagonals to a ZigZag.

fromDiagonals . toDiagonals = id toDiagonals . fromDiagonals = id

fromList :: [a] -> ZigZag a Source #

Convert a list to a ZigZag.

fromList . toList = id toList . fromList = id

toDiagonals :: ZigZag a -> [[a]] Source #

Convert a ZigZag to a list of diagonals.

fromDiagonals . toDiagonals = id toDiagonals . fromDiagonals = id

toList :: ZigZag a -> [a] Source #

Convert a ZigZag to a list.

fromList . toList = id toList . fromList = id

A list but with a balanced enumeration of Cartesian product such that

fmap sum (sequence (replicate n (fromList [0..])))

is monotonically increasing.

Example:

sequence [fromList [0,1], fromList [0,1,2]] = fromDiagonals [ [[0,0]] , [[1,0],[0,1]] , [[1,1],[0,2]] , [[1,2]] ]

This variation is useful in at least two ways. One, it is not stuck on infinite factors. Two, if the factors are ordered then the product is similarly ordered; this can lend to efficient searching of product elements.

Note that this method fails for the infinitary product even if every factor is known to be non-empty. The first element is known but following it are infinite elements that each draw a second element from one of the infinite factors. A product element drawing a third factor element is never reached.

Monad ZigZag Source # | |

Functor ZigZag Source # | |

Applicative ZigZag Source # | |

Foldable ZigZag Source # | |

Traversable ZigZag Source # | |

Eq1 ZigZag Source # | |

Ord1 ZigZag Source # | |

Read1 ZigZag Source # | |

Show1 ZigZag Source # | |

Alternative ZigZag Source # | |

MonadPlus ZigZag Source # | |

IsList (ZigZag a) Source # | |

Eq a => Eq (ZigZag a) Source # | |

Data a => Data (ZigZag a) Source # | |

Ord a => Ord (ZigZag a) Source # | |

Read a => Read (ZigZag a) Source # | |

Show a => Show (ZigZag a) Source # | |

Generic (ZigZag a) Source # | |

Semigroup (ZigZag a) Source # | |

Monoid (ZigZag a) Source # | |

Generic1 * ZigZag Source # | |

type Rep (ZigZag a) Source # | |

type Item (ZigZag a) Source # | |

type Rep1 * ZigZag Source # | |