Last active
November 8, 2020 07:19
-
-
Save jeovazero/93d653da28a8db9c2961d29e4d11c690 to your computer and use it in GitHub Desktop.
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
import qualified Data.Set as Set | |
old = ['A', 'B', 'C'] | |
current = ['B', 'D', 'E', 'E', 'D'] | |
-- diff old current = (['A', 'C'], ['D', 'E']) | |
-- to remove: ['A', 'C'] | |
-- to add: ['D', 'E'] | |
diff [] [] = ([], []) | |
diff old current = diff' current (Set.fromList old) Set.empty [] | |
diff' :: Ord a => [a] -> Set.Set a -> Set.Set a -> [a] -> ([a], [a]) | |
diff' [] oldSet _ toAdd = (Set.elems oldSet, toAdd) | |
diff' (x:xs) oldSet nextSet toAdd = | |
let | |
(oldSet', toAdd') | |
| Set.member x oldSet = (Set.delete x oldSet, toAdd) | |
| Set.member x nextSet = (oldSet, toAdd) | |
| otherwise = (oldSet, x:toAdd) | |
nextSet' = Set.insert x nextSet | |
in | |
diff' xs oldSet' nextSet' toAdd' | |
main = do | |
print $ diff old current |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment