Skip to content

Instantly share code, notes, and snippets.

@gregberns
Created July 3, 2021 05:56
Show Gist options
  • Save gregberns/cf30abeb8a692a4f2faa823d8c447105 to your computer and use it in GitHub Desktop.
Save gregberns/cf30abeb8a692a4f2faa823d8c447105 to your computer and use it in GitHub Desktop.
ListZipper duplicate
-- duplicate $ ListZipper (2:.1:.Nil) 3 (4:.5:.Nil)
-- [[1] >2< [3,4,5],[] >1< [2,3,4,5]] >[2,1] >3< [4,5]< [[3,2,1] >4< [5],[4,3,2,1] >5< []]
-- data ListZipper a = ListZipper [a] a [a]
duplicate :: ListZipper a -> ListZipper (ListZipper a)
duplicate z@(ListZipper l i r) =
let moveLeft (x :. xs) i rs = ListZipper xs x (i :. rs) :. moveLeft xs x (i :. rs)
moveLeft Nil _ _ = Nil
moveRight ls i (x :. xs) = ListZipper (i :. ls) x xs :. moveRight (i :. ls) x xs
moveRight _ _ Nil = Nil
l' = moveLeft l i r
r' = moveRight l i r
in ListZipper l' z r'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment