[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 15:10:42 UTC 2010


The following commit has been merged in the master branch:
commit d7c5173569366a37cdaed5ce016e047f3456c425
Author: John Goerzen <jgoerzen at complete.org>
Date:   Thu Jun 29 20:02:50 2006 +0100

    Added List.uniq and tests for it

diff --git a/MissingH/List.hs b/MissingH/List.hs
index 04775c1..635ef87 100644
--- a/MissingH/List.hs
+++ b/MissingH/List.hs
@@ -52,7 +52,7 @@ module MissingH.List(-- * Tests
                      grab,
                      -- * Miscellaneous
                      countElem, elemRIndex, alwaysElemRIndex, seqList,
-                     subIndex
+                     subIndex, uniq
                      -- -- * Sub-List Selection
                      -- sub,
                     ) where
@@ -421,3 +421,22 @@ Examples:
  -}
 subIndex :: Eq a => [a] -> [a] -> Maybe Int
 subIndex substr str = findIndex (isPrefixOf substr) (tails str)
+
+{- | Given a list, returns a new list with all duplicate elements removed.
+For example:
+
+>uniq "Mississippi" -> "Misp"
+
+You should not rely on this function necessarily preserving order, though
+the current implementation happens to.
+
+This function is not compatible with infinite lists. -}
+uniq :: Eq a => [a] -> [a]
+uniq [] = []
+uniq (x:xs) = x : filter (/= x) (uniq xs)
+
+----- same as
+--uniq (x:xs) = x : [y | y <- uniq xs, y /= x]
+
+
+
diff --git a/testsrc/Listtest.hs b/testsrc/Listtest.hs
index 44fc39d..b2a7d4f 100644
--- a/testsrc/Listtest.hs
+++ b/testsrc/Listtest.hs
@@ -94,6 +94,22 @@ test_flipAL =
                          ("e", ["d"])]
         ]
 
+test_uniq =
+    let f inp exp = TestCase $ exp @=? uniq inp in
+    [f ([]::[Int]) [],
+     f "asdf" "asdf",
+     f "aabbcc" "abc",
+     f "abcabc" "abc",
+     f "aaaaaa" "a",
+     f "aaaaaab" "ab",
+     f "111111111111111" "1",
+     f "baaaaaaaaa" "ba",
+     f "baaaaaaaaab" "ba",
+     f "aaacccdbbbefff" "acdbef",
+     f "foo" "fo",
+     f "15553344409" "153409",
+     f "Mississippi" "Misp"]
+
 test_trunc =
     let f len inp exp = TestCase $ exp @=? take len inp in
         [
@@ -199,6 +215,7 @@ test_spanList =
 
 
 tests = TestList [TestLabel "delFromAL" (TestList test_delFromAL),
+                  TestLabel "uniq" (TestList test_uniq),
                   TestLabel "addToAL" (TestList test_addToAL),
                   TestLabel "split" (TestList test_split),
                   TestLabel "join" (TestList test_join),

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list