Advertisement
banovski

Ninety-Nine Haskell Problems: #4

Jan 24th, 2025 (edited)
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.30 KB | Source Code | 0 0
  1. -- Problem 4: find the number of elements in a list.
  2.  
  3. -- Samples and displaying
  4.  
  5. main :: IO ()
  6. main = mapM_ putStrLn result
  7.  
  8. result :: [String]
  9. result = zipWith3 formatter [1 .. ] toEmptyList toNonEmptyList
  10.  
  11. formatter :: (Show a) => Int -> a -> a -> String
  12. formatter fn el nel = "Function #"
  13.   ++ show fn
  14.   ++ " applied to an empty list: "
  15.   ++ show el
  16.   ++ ", and to a non-empty list: "
  17.   ++ show nel
  18.  
  19. toEmptyList = functionsList <*> [emptySampleList]
  20. toNonEmptyList = functionsList <*> [nonEmptySampleList]
  21.  
  22. functionsList :: [[a] -> Int]
  23. functionsList = [one, two, three, four, five, six, seven, eight, nine, ten, eleven]
  24.  
  25. nonEmptySampleList :: [Int]
  26. nonEmptySampleList = [10, 9 .. 0]
  27.  
  28. emptySampleList :: [a]
  29. emptySampleList = []
  30.  
  31. -- Solutions
  32.  
  33. one :: [a] -> Int
  34. one = length
  35.  
  36. two :: [a] -> Int
  37. two = aux 0
  38.   where
  39.     aux c [] = c
  40.     aux c (_:xs) = aux (c + 1) xs
  41.  
  42. three :: [a] -> Int
  43. three [] = 0
  44. three list = fst $ last $ zip [1 .. ] list
  45.  
  46. four :: [a] -> Int
  47. four = foldl (\x _ -> x + 1) 0
  48.  
  49. five :: [a] -> Int
  50. five list = last $ scanl (\x _ -> x + 1) 0 list
  51.  
  52. six :: [a] -> Int
  53. six list = sum [1 | _ <- list]
  54.  
  55. seven :: [a] -> Int
  56. seven list = sum $ list >>= aux
  57.   where
  58.     aux _ = [1]
  59.  
  60. eight :: [a] -> Int
  61. eight [] = 0
  62. eight list = maximum $ map fst $ zip [1 .. ] list
  63.  
  64. nine :: [a] -> Int
  65. nine list = sum $ map (const 1) list
  66.  
  67. ten :: [a] -> Int
  68. ten list = sum $ pure (const 1) <*> list
  69.  
  70. eleven :: [a] -> Int
  71. eleven [] = 0
  72. eleven lst = (foldl1 (.) $ map (const (+ 1)) lst) 0
  73.  
  74. -- Function #1 applied to an empty list: 0, and to a non-empty list: 11
  75. -- Function #2 applied to an empty list: 0, and to a non-empty list: 11
  76. -- Function #3 applied to an empty list: 0, and to a non-empty list: 11
  77. -- Function #4 applied to an empty list: 0, and to a non-empty list: 11
  78. -- Function #5 applied to an empty list: 0, and to a non-empty list: 11
  79. -- Function #6 applied to an empty list: 0, and to a non-empty list: 11
  80. -- Function #7 applied to an empty list: 0, and to a non-empty list: 11
  81. -- Function #8 applied to an empty list: 0, and to a non-empty list: 11
  82. -- Function #9 applied to an empty list: 0, and to a non-empty list: 11
  83. -- Function #10 applied to an empty list: 0, and to a non-empty list: 11
  84. -- Function #11 applied to an empty list: 0, and to a non-empty list: 11
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement