Skip to content

Instantly share code, notes, and snippets.

@ethagnawl
Last active February 13, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethagnawl/be22db6fb96f201eb95342cdd1e06a8e to your computer and use it in GitHub Desktop.
Save ethagnawl/be22db6fb96f201eb95342cdd1e06a8e to your computer and use it in GitHub Desktop.
dedupe a list of lists in haskell without using nub
-- [[1,2],[2,3],[4,5]] becomes [[1,2],[3],[4,5]]
-- this file can be run using: `stack runghc dedupe-nested-lists.hs`
dedupe_against_self =
foldr
(\member memo ->
if (any (== member) memo)
then memo
else member : memo)
[]
dedupe_against_memo memo = filter (\x -> not $ any (== x) memo)
dedupe_nested_lists lst =
reverse $ foldl
(\memo x ->
let memo' = concat memo
x' = dedupe_against_self x
x'' = dedupe_against_memo memo' x'
in
x'' : memo)
[]
lst
main = do
let lst = dedupe_nested_lists [[1,2],[2,3],[4,5]]
putStrLn $ show $ lst -- [[1,2],[3],[4,5]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment