CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 06:51:29 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=90210e973645c4938625ab96190ff3483b06ab5986363bb771207368569249b0a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22y0l8W3Ks2-TJFQptYFOcSVbdvurSEa1y%22%3B%7D; HttpOnly; Path=/
cf-ray: 98cc6de12923c1f7-BLR
Ninety-Nine Haskell Problems: #4 - Pastebin.com
SHARE
TWEET

Ninety-Nine Haskell Problems: #4
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Problem 4: find the number of elements in a list.
- -- Samples and displaying
- main :: IO ()
- main = mapM_ putStrLn result
- result :: [String]
- result = zipWith3 formatter [1 .. ] toEmptyList toNonEmptyList
- formatter :: (Show a) => Int -> a -> a -> String
- formatter fn el nel = "Function #"
- ++ show fn
- ++ " applied to an empty list: "
- ++ show el
- ++ ", and to a non-empty list: "
- ++ show nel
- toEmptyList = functionsList <*> [emptySampleList]
- toNonEmptyList = functionsList <*> [nonEmptySampleList]
- functionsList :: [[a] -> Int]
- functionsList = [one, two, three, four, five, six, seven, eight, nine, ten, eleven]
- nonEmptySampleList :: [Int]
- nonEmptySampleList = [10, 9 .. 0]
- emptySampleList :: [a]
- emptySampleList = []
- -- Solutions
- one :: [a] -> Int
- one = length
- two :: [a] -> Int
- two = aux 0
- where
- aux c [] = c
- aux c (_:xs) = aux (c + 1) xs
- three :: [a] -> Int
- three [] = 0
- three list = fst $ last $ zip [1 .. ] list
- four :: [a] -> Int
- four = foldl (\x _ -> x + 1) 0
- five :: [a] -> Int
- five list = last $ scanl (\x _ -> x + 1) 0 list
- six :: [a] -> Int
- six list = sum [1 | _ <- list]
- seven :: [a] -> Int
- seven list = sum $ list >>= aux
- where
- aux _ = [1]
- eight :: [a] -> Int
- eight [] = 0
- eight list = maximum $ map fst $ zip [1 .. ] list
- nine :: [a] -> Int
- nine list = sum $ map (const 1) list
- ten :: [a] -> Int
- ten list = sum $ pure (const 1) <*> list
- eleven :: [a] -> Int
- eleven [] = 0
- eleven lst = (foldl1 (.) $ map (const (+ 1)) lst) 0
- -- Function #1 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #2 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #3 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #4 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #5 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #6 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #7 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #8 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #9 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #10 applied to an empty list: 0, and to a non-empty list: 11
- -- Function #11 applied to an empty list: 0, and to a non-empty list: 11
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Exploit 500$ in 15 Minutes⭐⭐
Java | 11 sec ago | 0.10 KB
-
⭐⭐⭐ G2A Payment Exploit ✅ NEVER SEEN BEFORE ⭐...
JavaScript | 16 sec ago | 0.24 KB
-
⭐✅ MAKE $2500 IN 15 MIN ✅ NEVER SEEN BEFORE ⭐...
JavaScript | 26 sec ago | 0.24 KB
-
⭐ ✅ Free Products Method ✅ ✅ NEVER SEEN BEFOR...
JavaScript | 36 sec ago | 0.24 KB
-
⭐✅ Swapzone Glitch ✅ Working⭐⭐⭐ M
JavaScript | 43 sec ago | 0.24 KB
-
⭐ G2A Bug ⭐ (Get more on BTC swaps) ✅ NEVER S...
JavaScript | 45 sec ago | 0.24 KB
-
✅ Make $2500 in 20 minutes⭐⭐⭐ H
JavaScript | 54 sec ago | 0.24 KB
-
⭐✅ Jack's Profit Method ✅ NEVER SEEN BEF...
JavaScript | 54 sec 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