CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 05:06:03 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=7cc27e79101e74ec8f5aa0dd786cabab0b3d37cce784b6127789c4d26ef8c60ba%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22C1ha87cC7e9SgVl4FrM-TgQMbCjNX3nd%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d410d20f1f5ace-BLR
Ninety-Nine Haskell Problems: #9 - Pastebin.com
SHARE
TWEET

Ninety-Nine Haskell Problems: #9
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Pack consecutive duplicates of list elements into sublists. If a
- -- list contains repeated elements they should be placed in separate
- -- sublists.
- import Data.List (group)
- -- "group" is imported as a reference function
- main :: IO ()
- main = do
- putStrLn "Test strings: "
- mapM_ print $ chunksOfFour testStringList
- putStrLn "\nTested functions validity check: "
- mapM_ (print . testFunction) testFuncList
- testStringList :: [String]
- testStringList = pure (\a b c d -> [a, b, c, d])
- <*> "ab"
- <*> "ab"
- <*> "ab"
- <*> "ab"
- chunksOfFour :: [a] -> [[a]]
- chunksOfFour [] = []
- chunksOfFour lst = take 4 lst : chunksOfFour (drop 4 lst)
- testFuncList :: Eq a => [[a] -> [[a]]]
- testFuncList = [zero, one, two, three]
- testFunction :: (String -> [String]) -> Bool
- testFunction function =
- -- group from Data.List is used to produce reference results
- map function testStringList == map group testStringList
- -- Functions were tested on a list with duplicates, 1000405 items
- -- long. Time each function takes to complete the task is measured in
- -- ticks.
- -- 128 ticks
- zero :: Eq a => [a] -> [[a]]
- zero = group
- -- 221 ticks
- one :: Eq a => [a] -> [[a]]
- one [] = []
- one [a] = [[a]]
- one lst = go [head lst] (tail lst)
- where
- go a [] = [a]
- go a (x:xs)
- | head a == x = go (a ++ [x]) xs
- | otherwise = a : go [x] xs
- -- 285 ticks
- two :: Eq a => [a] -> [[a]]
- two lst =
- let
- itemsAsLists = map (:[]) lst
- h = head itemsAsLists
- t = tail itemsAsLists
- join a [] = [a]
- join acc (x:xs)
- | head acc == head x = join (acc ++ x) xs
- | otherwise = acc : join x xs
- in
- join h t
- -- 138 ticks
- three :: Eq a => [a] -> [[a]]
- three [] = []
- three [x] = [[x]]
- three lst = foldr go [[last lst]] (init lst)
- where
- go x (y:ys)
- | x == head y = (x : y) : ys
- | otherwise = [x] : y : ys
- -- 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
Advertisement
Add Comment
Please, Sign In to add comment
-
📌 Swapzone +37% glitch ⭐ 2
JavaScript | 2 sec ago | 0.25 KB
-
📌 Swapzone +37% glitch
JavaScript | 10 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ⭐⭐ 9
JavaScript | 11 sec ago | 0.25 KB
-
💵 Make 3000$ in 20 minutes 💵
JavaScript | 20 sec ago | 0.24 KB
-
⭐⭐⭐Make $15OO in 2O minutesV E⭐⭐
Java | 2 min ago | 0.10 KB
-
⭐⭐⭐MAKE $900 INSTANTLY⭐⭐
Java | 2 min ago | 0.10 KB
-
⭐⭐⭐MAKE $500 IN 15 MIN⭐⭐
Java | 2 min ago | 0.10 KB
-
⭐⭐⭐Instant Profit Method⭐⭐
Java | 2 min ago | 0.10 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