Learn You a Haskell shows the
union also acts like a function on sets. It returns the union of two lists. It pretty much goes over every element in the second list and appends it to the first one if it isn't already in yet. Watch out though, duplicates are removed from the second list!
ghci> [1..7] `union` [5..10] [1,2,3,4,5,6,7,8,9,10]
Please critique my implementation.
import Data.List (nub) union' :: (Eq a) => [a] -> [a] -> [a] union' xs ys = nub (xs ++ ys)
This 1-line implementation seems easy to understand, however I'm concerned at its bad/mediocre performance. Consider that I'm appending
ys, and then performing
nub on the whole list.