Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active December 25, 2016 09:56
Show Gist options
  • Save deque-blog/bb6d46a7118c82bec81506d40620a900 to your computer and use it in GitHub Desktop.
Save deque-blog/bb6d46a7118c82bec81506d40620a900 to your computer and use it in GitHub Desktop.
foldMonoidTree :: (Monoid a) => [a] -> a
foldMonoidTree =
foldl1 (flip (<>)) . map snd . foldl addToCounter []
where
addToCounter counter x = propagate ((1::Int,x) : counter)
propagate [] = []
propagate [x] = [x]
propagate counter@(x:y:xs) -- x arrived last => combine on right
| fst x == fst y = propagate ((fst x + fst y, snd y <> snd x) : xs)
| otherwise = counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment