Copyright | (C) 2015 mniip |
---|---|
Maintainer | mniip <mniip@mniip.com> |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
License : BSD3 Portability : portable Stability : experimental
Implementation of the Z-function on cons-cells.
Z-function has a simple implementation using arrays, the challenge, however
was to implement it on haskell lists (which are cons cells) without losing
any complexity. The following code uses a tying-the-knot data passing
structure, however no complicated laziness mechanisms are used: a ZState
only depends on previous ZState
s and therefore this algorithm can be safely
implemented in a strict language with pointers.
- data ZState a = ZState {}
- data GenericZState i a = GenericZState {
- gzTail :: [a]
- gzLength :: Maybe i
- gzPrev :: [GenericZState i a]
- zTraverse :: Eq a => [a] -> [ZState a]
- zTraverseBy :: (a -> a -> Bool) -> [a] -> [ZState a]
- gzTraverse :: (Num i, Eq a) => [a] -> [GenericZState i a]
- gzTraverseBy :: Num i => (a -> a -> Bool) -> [a] -> [GenericZState i a]
Documentation
data GenericZState i a Source
ZState
generalized of the number type. Nothing
represents zero and is
used to avoid the Eq
constraint
GenericZState | |
|
zTraverse :: Eq a => [a] -> [ZState a] Source
O(N). Compute the list of Z-function states for a given input.
zTraverseBy :: (a -> a -> Bool) -> [a] -> [ZState a] Source
O(N) and O(N) calls to the predicate. Compute the list of Z-function states using a given equality predicate. See "Data.List.Zalgo.zFunBy" for a detailed explanation of what predicates are allowed.
gzTraverse :: (Num i, Eq a) => [a] -> [GenericZState i a] Source
O(N) and O(N) plus-1's. Compute the list of Z-function states using a given number type.
gzTraverseBy :: Num i => (a -> a -> Bool) -> [a] -> [GenericZState i a] Source
O(N), O(N) calls to the predicate, and O(N) plus-1's. Compute the list of Z-function states using a given number type and a given equality predicate.