Your second solution, a part from non preserving the ordering of the initial sequence, also requires the type of the list elements to be an instance of Ord.
I've fixed a bug in your first version, where the return values of isIn where reversed.
Here they are:
module Main where
import Data.List (sort, group)
-- Need ordering on "a"
uniqueS :: Ord a => [a] -> [a]
uniqueS = concat . filter (null . drop 1) . group . sort
-- Fixed Chaddai's solution
-- Only need equivalent relation on "a"
unique :: Eq a => [a] -> [a]
unique xs = [x | x <- xs, isIn x xs 2]
where isIn :: Eq a => a -> [a] -> Int -> Bool
isIn _ _ 0 = False
isIn _ [] _ = True
isIn y (x:xs) n
| y == x = isIn y xs (n-1)
| otherwise = isIn y xs n
main :: IO ()
main = do
print $ uniqueS xs
print $ unique xs
where xs = [1,2,3,3,5,2,1,4]
L.
On Thu, Mar 29, 2012 at 9:30 AM, Chaddaï Fouché
<chaddai.fouche@gmail.com> wrote:
oops, I meant :
> unique = concat . filter (null . drop 1) . group . sort
--
Jedaï