-- -- (c) 2012 Wei Ke -- license: GPL-3 -- license-file: LICENSE -- -- | -- The "Zp" module implements a simple list zipper -- to perform efficient list search and update operations. -- module Zp ( start , finish , rewind , next , find , findm , current , remove , replace , insert ) where data Zp a = Zp [a] [a] start :: [a] -> Zp a start s = Zp [] s finish :: Zp a -> [a] finish (Zp [] s) = s finish (Zp (x:ls) rs) = finish (Zp ls (x:rs)) rewind :: Zp a -> Zp a rewind zp@(Zp [] _) = zp rewind (Zp (x:ls) rs) = rewind (Zp ls (x:rs)) next :: Zp a -> Zp a next (Zp ls (x:rs)) = Zp (x:ls) rs next _ = undefined find :: (a -> Bool) -> Zp a -> Zp a find p zp@(Zp _ (x:_)) | p x = zp | otherwise = find p (next zp) find _ zp@(Zp _ []) = zp findm :: (a -> Maybe b) -> Zp a -> (Zp a, Maybe b) findm f zp@(Zp _ (x:_)) = case f x of m@(Just _) -> (zp, m) _ -> findm f (next zp) findm _ zp@(Zp _ []) = (zp, Nothing) current :: Zp a -> Maybe a current (Zp _ (x:_)) = Just x current _ = Nothing remove :: Zp a -> Zp a remove (Zp ls (_:rs)) = Zp ls rs remove _ = undefined replace :: Zp a -> a -> Zp a replace (Zp ls (_:rs)) x = Zp ls (x:rs) replace _ _ = undefined insert :: Zp a -> a -> Zp a insert (Zp ls rs) x = Zp ls (x:rs) -- -- end of Zp -- -- --$Id: Zp.hs 1182 2012-11-12 10:11:40Z wke@IPM.EDU.MO $