Advertisement
banovski

Ninety-Nine Haskell Problems: #8

Mar 10th, 2025 (edited)
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.25 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. one :: Eq a => [a] -> [a]
  40. one [] = []
  41. one [x] = [x]
  42. one (x:y:ys)
  43.   | x == y = one (y : ys)
  44.   | otherwise = x : one (y : ys)
  45.  
  46. two :: Eq a => [a] -> [a]
  47. two [] = []
  48. two [w] = [w]
  49. two (x:xs) =
  50.   reverse $ foldl (\(y:ys) z -> if y == z then z:ys else z:y:ys) [x] xs
  51.  
  52. three :: Eq a => [a] -> [a]
  53. three [] = []
  54. three [x] = [x]
  55. three list = let
  56.   lastItem = last list
  57.   in
  58.   foldr (\x (y:ys) ->
  59.            if x == y
  60.            then y:ys
  61.            else x:y:ys) [lastItem] list
  62.  
  63. four :: Eq a => [a] -> [a]
  64. four [] = []
  65. four list@(x:xs) = x : (zip list xs >>= noDupesTuples)
  66.   where
  67.     noDupesTuples (a, b)
  68.       | a == b = []
  69.       | otherwise = [b]
  70.  
  71. five :: Eq a => [a] -> [a]
  72. five [] = []
  73. five lst = pure head <*> group lst
  74.  
  75. six :: Eq a => [a] -> [a]
  76. six [] = []
  77. six lst = concatMap nub (group lst)
  78.  
  79. seven :: (Eq a, Ord a) => [a] -> [a]
  80. seven [] = []
  81. seven lst = concatMap (Set.toList . Set.fromList) (group lst)
  82.  
  83. -- Test strings:
  84. -- ["aaaa","aaab","aaba","aabb"]
  85. -- ["abaa","abab","abba","abbb"]
  86. -- ["baaa","baab","baba","babb"]
  87. -- ["bbaa","bbab","bbba","bbbb"]
  88. --
  89. -- Tested functions validity check:
  90. -- True
  91. -- True
  92. -- True
  93. -- True
  94. -- True
  95. -- True
  96. -- True
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement