Last active
February 13, 2018 01:27
-
-
Save ethagnawl/be22db6fb96f201eb95342cdd1e06a8e to your computer and use it in GitHub Desktop.
dedupe a list of lists in haskell without using nub
This file contains 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
-- [[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