Created
October 31, 2012 05:23
-
-
Save jnape/3984985 to your computer and use it in GitHub Desktop.
Symmetric set difference in Haskell
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
| module Example ( | |
| (∆) | |
| ) where | |
| unique :: (Eq a) => [a] -> [a] | |
| unique [] = [] | |
| unique (x:xs) = x:(unique (filter (/= x) xs)) | |
| without :: (Eq a) => [a] -> [a] -> [a] | |
| xs `without` ys = [ x | x <- xs, not (x `elem` ys)] | |
| (∆) :: (Eq a) => [a] -> [a] -> [a] | |
| (∆) xs ys = | |
| let uniqueXs = unique xs | |
| uniqueYs = unique ys | |
| in (uniqueXs `without` uniqueYs) ++ (uniqueYs `without` uniqueXs) | |
| main :: IO () | |
| main = print $ [1, 2, 3, 4, 5] ∆ [3, 4, 5, 6, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment