Advertisement
banovski

Ninety-Nine Haskell Problems: #8

Mar 10th, 2025 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.43 KB | Source Code | 0 0
  1. -- Problem 8: eliminate consecutive duplicates of list elements. If a
  2. -- list contains repeated elements they should be replaced with a
  3. -- single copy of the element. The order of the elements should not be
  4. -- changed.
  5.  
  6. import Data.List
  7. import qualified Data.Set as Set
  8.  
  9. main :: IO ()
  10. main = do
  11.   putStrLn "Test strings: "
  12.   mapM_ print $ chunksOfFour testStringList
  13.   putStrLn "\nTested functions validity check: "
  14.   mapM_ (print . testFunction) testFuncList
  15.  
  16. testFuncList :: (Eq a, Ord a) => [[a] -> [a]]
  17. testFuncList = [one, two, three, four, five, six, seven]
  18.  
  19. testStringList :: [String]
  20. testStringList = pure (\a b c d -> [a, b, c, d])
  21.   <*> "ab"
  22.   <*> "ab"
  23.   <*> "ab"
  24.   <*> "ab"
  25.  
  26. testFunction :: (String -> String) -> Bool
  27. testFunction function =
  28.   map function testStringList == map uniq testStringList
  29.   where
  30.     -- uniq from Data.List.Unique is used to produce reference results
  31.     uniq = map head . group
  32.  
  33. chunksOfFour :: [a] -> [[a]]
  34. chunksOfFour [] = []
  35. chunksOfFour lst = take 4 lst : chunksOfFour (drop 4 lst)
  36.  
  37. -- Tested functions.
  38.  
  39. -- Test results are measured in ticks. Functions are tested on a list
  40. -- of 1000000 duplicate items. The result of uniq is 122
  41.  
  42. -- 51
  43. one :: Eq a => [a] -> [a]
  44. one [] = []
  45. one [x] = [x]
  46. one (x:y:ys)
  47.   | x == y = one (y : ys)
  48.   | otherwise = x : one (y : ys)
  49.  
  50. -- 152
  51. two :: Eq a => [a] -> [a]
  52. two [] = []
  53. two [w] = [w]
  54. two (x:xs) =
  55.   reverse $ foldl (\(y:ys) z -> if y == z then z:ys else z:y:ys) [x] xs
  56.  
  57. -- 129
  58. three :: Eq a => [a] -> [a]
  59. three [] = []
  60. three [x] = [x]
  61. three list = let
  62.   lastItem = last list
  63.   in
  64.   foldr (\x (y:ys) ->
  65.            if x == y
  66.            then y:ys
  67.            else x:y:ys) [lastItem] list
  68.  
  69. -- 140
  70. four :: Eq a => [a] -> [a]
  71. four [] = []
  72. four list@(x:xs) = x : (zip list xs >>= noDupesTuples)
  73.   where
  74.     noDupesTuples (a, b)
  75.       | a == b = []
  76.       | otherwise = [b]
  77.  
  78. -- 110
  79. five :: Eq a => [a] -> [a]
  80. five [] = []
  81. five lst = pure head <*> group lst
  82.  
  83. -- 178
  84. six :: Eq a => [a] -> [a]
  85. six [] = []
  86. six lst = concatMap nub (group lst)
  87.  
  88. -- 191
  89. seven :: (Eq a, Ord a) => [a] -> [a]
  90. seven [] = []
  91. seven lst = concatMap (Set.toList . Set.fromList) (group lst)
  92.  
  93. -- Test strings:
  94. -- ["aaaa","aaab","aaba","aabb"]
  95. -- ["abaa","abab","abba","abbb"]
  96. -- ["baaa","baab","baba","babb"]
  97. -- ["bbaa","bbab","bbba","bbbb"]
  98. --
  99. -- Tested functions validity check:
  100. -- True
  101. -- True
  102. -- True
  103. -- True
  104. -- True
  105. -- True
  106. -- True
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement