Skip to content

Instantly share code, notes, and snippets.

@mgttlinger
Created January 14, 2018 18:09
Show Gist options
  • Save mgttlinger/2d51888caee155f78b84fd36bb1c417f to your computer and use it in GitHub Desktop.
Save mgttlinger/2d51888caee155f78b84fd36bb1c417f to your computer and use it in GitHub Desktop.
zip with index on rose tree
data RT a = N a [RT a] deriving (Show)
no a ns = N a ns
lea a = N a []
sz (N _ cs) = length cs
l (N a cs) = foldl (\accu b -> b) a (l <$> cs)
slast l = if length l > 0 then Just (last l) else Nothing
inol :: RT a -> RT (Int, a)
inol = inol' 0
where
inol' :: Int -> RT a -> RT (Int, a)
inol' s t@(N a cs) = let ls = take (sz t `div` 2) cs
rs = drop (sz t `div` 2) cs
ls' = foldr (\a accu -> accu ++ [(inol' (maybe s (1 +) (fst . l <$> slast accu)) a)]) [] ls
nn = (maybe s (1 +) (fst . l <$> slast ls'))
rs' = foldr (\a accu -> accu ++ [(inol' (1 + maybe nn (fst) (l <$> slast accu)) a)]) [] rs
in
N (nn, a) $ ls' ++ rs'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment