Skip to content

Instantly share code, notes, and snippets.

@matoous
Created June 6, 2018 16:43
Show Gist options
  • Save matoous/0732063cd8357892f517d581857e07ae to your computer and use it in GitHub Desktop.
Save matoous/0732063cd8357892f517d581857e07ae to your computer and use it in GitHub Desktop.
module ZipList where
type ZipList a = ([a], [a])
newList :: ZipList a
newList = ([], [])
moveLeft :: ZipList a -> ZipList a
moveLeft ((a:b), c) = (b, (a:c))
moveLeft _ = error "Invalid operation"
moveRight :: ZipList a -> ZipList a
moveRight (a, (b:c)) = ((b:a), c)
moveRight _ = error "Invalid operation"
insert :: a -> ZipList a -> ZipList a
insert x (a, b) = (a, (x:b))
delete :: ZipList a -> ZipList a
delete (a, (b:c)) = (a, c)
delete _ = error "Invalid operation"
update :: a -> ZipList a -> ZipList a
update x (a, (b:c)) = (a, (x:c))
update _ _ = error "Invalid operation"
get :: ZipList a -> a
get (_, (x:_)) = x
get _ = error "Invalid operation"
rewind :: ZipList a -> ZipList a
rewind ([], c) = ([], c)
rewind ((a:b), c) = rewind (b, (a:c))
index :: ZipList a -> Int
index (a, b) = length a
moveTo :: Int -> ZipList a -> ZipList a
moveTo x z
| idx == x = z
| idx > x = moveTo x (moveLeft z)
| otherwise = moveTo x (moveRight z)
where
idx = index z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment