CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 02:17:38 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=760e3e25af5486a8fa2dbe9f997669566319ea399325a5886f041e1dd429f3f1a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22wVGJ2i3DWq7sWEII2KHO60dG5Be8gWMy%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cadcbb2af29ac4-BLR
Ninety-Nine Haskell Problems: #8 - Pastebin.com
SHARE
TWEET

Ninety-Nine Haskell Problems: #8
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Problem 8: eliminate consecutive duplicates of list elements. If a
- -- list contains repeated elements they should be replaced with a
- -- single copy of the element. The order of the elements should not be
- -- changed.
- import Data.List
- import qualified Data.Set as Set
- main :: IO ()
- main = do
- putStrLn "Test strings: "
- mapM_ print $ chunksOfFour testStringList
- putStrLn "\nTested functions validity check: "
- mapM_ (print . testFunction) testFuncList
- testFuncList :: (Eq a, Ord a) => [[a] -> [a]]
- testFuncList = [one, two, three, four, five, six, seven]
- testStringList :: [String]
- testStringList = pure (\a b c d -> [a, b, c, d])
- <*> "ab"
- <*> "ab"
- <*> "ab"
- <*> "ab"
- testFunction :: (String -> String) -> Bool
- testFunction function =
- map function testStringList == map uniq testStringList
- where
- -- uniq from Data.List.Unique is used to produce reference results
- uniq = map head . group
- chunksOfFour :: [a] -> [[a]]
- chunksOfFour [] = []
- chunksOfFour lst = take 4 lst : chunksOfFour (drop 4 lst)
- -- Tested functions.
- -- Test results are measured in ticks. Functions are tested on a list
- -- of 1000000 duplicate items. The result of uniq is 122
- -- 51
- one :: Eq a => [a] -> [a]
- one [] = []
- one [x] = [x]
- one (x:y:ys)
- | x == y = one (y : ys)
- | otherwise = x : one (y : ys)
- -- 152
- two :: Eq a => [a] -> [a]
- two [] = []
- two [w] = [w]
- two (x:xs) =
- reverse $ foldl (\(y:ys) z -> if y == z then z:ys else z:y:ys) [x] xs
- -- 129
- three :: Eq a => [a] -> [a]
- three [] = []
- three [x] = [x]
- three list = let
- lastItem = last list
- in
- foldr (\x (y:ys) ->
- if x == y
- then y:ys
- else x:y:ys) [lastItem] list
- -- 140
- four :: Eq a => [a] -> [a]
- four [] = []
- four list@(x:xs) = x : (zip list xs >>= noDupesTuples)
- where
- noDupesTuples (a, b)
- | a == b = []
- | otherwise = [b]
- -- 110
- five :: Eq a => [a] -> [a]
- five [] = []
- five lst = pure head <*> group lst
- -- 178
- six :: Eq a => [a] -> [a]
- six [] = []
- six lst = concatMap nub (group lst)
- -- 191
- seven :: (Eq a, Ord a) => [a] -> [a]
- seven [] = []
- seven lst = concatMap (Set.toList . Set.fromList) (group lst)
- -- Test strings:
- -- ["aaaa","aaab","aaba","aabb"]
- -- ["abaa","abab","abba","abbb"]
- -- ["baaa","baab","baba","babb"]
- -- ["bbaa","bbab","bbba","bbbb"]
- --
- -- Tested functions validity check:
- -- True
- -- True
- -- True
- -- True
- -- True
- -- True
- -- True
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐✅ Swapzone Glitch ✅ Working⭐⭐⭐ O
JavaScript | 4 sec ago | 0.24 KB
-
✅ Make $2500 in 20 minutes⭐⭐⭐ F
JavaScript | 15 sec ago | 0.24 KB
-
⭐⭐Exchange Exploit⭐⭐ 3
JavaScript | 27 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ D
JavaScript | 38 sec ago | 0.24 KB
-
⭐✅ MAKE $2500 IN 15 MIN⭐⭐⭐ G
JavaScript | 49 sec ago | 0.24 KB
-
⭐✅ Exploit 2500$ in 15 Minutes⭐⭐⭐ B
JavaScript | 1 min ago | 0.24 KB
-
Free Crypto Method (NEVER SEEN BEFORE)⭐⭐ E
JavaScript | 1 min ago | 0.24 KB
-
⭐⭐Exchange Exploit⭐⭐ 3
JavaScript | 1 min ago | 0.24 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand