I'm trying to implement union
from the standard Data.List
library. I know it's reinventing the wheel, but reimplementing functions is how I learn things. Any feedback at all would be helpful!
union' :: (Eq a) => [a] -> [a] -> [a]
union' [] _ = []
union' _ [] = []
union' xs ys = nub' $ xs ++ ys where
nub' [] = []
nub' (x:xs)
| not $ x `elem` xs = x : nub' xs
| otherwise = nub' xs
This code works, but it strikes me as verbose. union
is the opposite of intersect
, which does not require a helper where
statement:
intersect' :: (Eq a) => [a] -> [a] -> [a]
intersect' [] _ = []
intersect' _ [] = []
intersect' (x:xs) list
| x `elem` list = x : intersect' xs list
| otherwise = intersect' xs list
How can I make my union
function resemble the elegance of my intersect
function?
Revision here