Skip to content

Instantly share code, notes, and snippets.

@jnape
Created October 31, 2012 05:23
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 jnape/3984985 to your computer and use it in GitHub Desktop.
Save jnape/3984985 to your computer and use it in GitHub Desktop.
Symmetric set difference in Haskell
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