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.
Easy nonrecursive list zipper
{-# 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,[])] -}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment