Created
December 5, 2012 20:14
-
-
Save trobertson/4219104 to your computer and use it in GitHub Desktop.
zipper type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Data.Zipper where | |
data ZipperList a = ZL { | |
prev :: [a] | |
, next :: [a] | |
} | |
-- it's an efficient list traversal, so we should be able to map over it | |
instance Functor ZipperList where | |
fmap foo (ZL p n) = ZL (fmap foo p) (fmap foo n) | |
-- create a Zipper out of a list | |
mkZipperList :: [a] -> ZipperList a | |
mkZipperList xs = ZL [] xs | |
-- insert an element at the current index | |
zInsertAt :: a -> ZipperList a -> ZipperList a | |
zInsertAt x (ZL p n) = ZL (head n:p) (tail n) | |
-- delete element at current index | |
zDeleteAt :: ZipperList a -> ZipperList a | |
zDeleteAt (ZL p n) = ZL p (tail n) | |
-- element at current index | |
zCurrent :: ZipperList a -> a | |
zCurrent (ZL p n) = head n | |
-- move the index forward one | |
zForward :: ZipperList a -> ZipperList a | |
zForward z@(ZL p []) = z | |
zForward (ZL p n) = ZL (head n:p) (tail n) | |
-- move the index backwards one | |
zBackward :: ZipperList a -> ZipperList a | |
zBackward z@(ZL [] n) = z | |
zBackward (ZL p n) = ZL (tail p) (head p:n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment