Created
August 16, 2025 06:56
-
-
Save axionbuster/299cf1f6bb3c23e77c7b1f5de1c8695b to your computer and use it in GitHub Desktop.
Easy nonrecursive list zipper
This file contains hidden or 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
| {-# 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