开发者

Remove duplicates from list

I have datatype:

data SidesType = Sides Int Int Int deriving (Show)

And I need a function which get a list of SidesType and remove duplicates from it.

*Main> let a = [Sides 3 4 5,Sides 3 4 5,Sides 5 12 13,Sides 6 8 10,Sides 6 8 10,Sides 8 15 17,Sides 9 12 15,Sides 5 12 13,Sides 9 12 15,Sides 12 16 20,Sides 8 15 17,Sides 15 20 25,Sides 12 16 20,Sides 15 20 25]
*Main> removeDuplicateFromList [] a
[Sides 3 4 5,Sides 5 12 13,Sides 6 8 10,Sides 6 8 10,Sides 8 15 17,Sides 9 12 15,Sides 5 12 13,Sides 9 12 15,Sides 12 16 20,Sides 8 15 17,Sides 15 20 25,Sides 12 16 20,Sides 15 20 25]

Here is my solution:

removeElementFromList :: [SidesType] -> SidesType -> [SidesType]
removeElementFromList lst element  = 
                      let (Sides a b c) = element
                      in [(Sides x y z) | (Sides x y z) <- lst, (x /= a) || (y /= b)]

removeDuplicateFromList :: [SidesType] -> [SidesType] -> [SidesType]
removeDuplicateFromList inlist outlist 
                        | (length outlist) == 0 = inlist
                        | otherwise = 
                          let element = head outlist
                              b开发者_JAVA技巧 = tail outlist
                              filtered = removeElementFromList b element
                      in removeDuplicateFromList (inlist ++ [element]) filtered

I am just wondering if there is any other way to write this code in more haskell-way ?


As usual there is "By" function which adds flexibility:

nubBy :: (a -> a -> Bool) -> [a] -> [a]

PS Although it's O(n^2)


You're already deriving Show for your datatype. If you also derive Eq, you can use nub from module Data.List.


Use Data.List.nub


First derive the order class also:

data XYZ = XYZ .... deriving (Show, Eq, Ord)

Or write your on Eq instance:

instance Eq XYZ where
a == b = ...

Then be intelligent and use a Tree! [Computer Science Trees grow from top to bottom!][1]

import qualified Data.Map.Strict as Map

removeDuplicates ::[a] -> [a]
removeDuplicates list = map fst $ Map.toList $ Map.fromList $ map (\a -> (a,a)) list

Complexity (from right to left) for list with length N:

  1. map of the list: O(N)
  2. Map.fromList: O(N*log N)
  3. Map.toList: O(log N)
  4. map over list with list length smaller or equal to N: O(N)

They are called consecutively, this means, there are pluses between the complexities of the parts => O(2 * N + N * log N + log N) = O(N * log N)

This is way better than traversing N^2 times over the list! See: wolframAlpha plots. I included 2*N also for comparison reasons.

2+3: http://hackage.haskell.org/package/containers-0.5.4.0/docs/Data-Map-Strict.html

[1]: Search wikipedia for Computer Science Tree

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜