Skip to content

Instantly share code, notes, and snippets.

@axionbuster
Created August 16, 2025 06:56
Show Gist options
  • Save axionbuster/299cf1f6bb3c23e77c7b1f5de1c8695b to your computer and use it in GitHub Desktop.
Save axionbuster/299cf1f6bb3c23e77c7b1f5de1c8695b to your computer and use it in GitHub Desktop.

Revisions

  1. axionbuster created this gist Aug 16, 2025.
    19 changes: 19 additions & 0 deletions Zipper.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    {-# LANGUAGE ParallelListComp #-}
    module Data.Zipper (zipper) where

    import Data.List
    import qualified Data.List.NonEmpty as NE

    -- helper
    dropLast :: [a] -> [a]
    dropLast = maybe [] fst . unsnoc

    zipper :: [a] -> [([a], a, [a])]
    zipper as =
    [ (dropLast bs, a, drop 1 cs)
    | bs <- NE.toList <$> inits1 as
    | cs <- NE.toList <$> tails1 as
    | a <- as
    ]

    {- ex: zipper [1, 2, 4] = [([],1,[2,4]),([1],2,[4]),([1,2],4,[])] -}