Dan Weston westondan at imageworks.com
Thu Jul 12 17:41:53 EDT 2007

```While we're at it, the second pattern match is superfluous. Once you
take out nonempty lists from the list type, the only thing left is an
empty one!

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

One question left: would the second definition be more efficient if written:

unique e = e

Inquiring minds want to know...

Dan

Dan Weston wrote:
> 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)
>>
>
>

```