[Haskell-cafe] function unique

Dan Weston westondan at imageworks.com
Thu Jul 12 17:37:06 EDT 2007


Now that I mention it, the base case is much less often called than the 
recursive case, so I would in hindsight flip the order of the two 
(mutually exclusive) partial function definitions:

unique :: Eq a => [a] -> [a]
unique (x:xs) = x : (unique . filter (/= x) $ xs)
unique []     = []

This should be marginally more efficient. I doubt that GHC would 
automatically detect that a) they are mutually exclusive and b) the 
second is called less often than the first!

Dan

Dan Weston wrote:
 >
> Close. Actually, the author upstream (i.e. me) had in mind:
> 
>  > uniq :: Eq a => [a] -> [a]
>  > uniq []     = []
>  > uniq (x:xs) = x : uniq (filter (/= x) xs)
> 




More information about the Haskell-Cafe mailing list