Advertisement
banovski

Ninety-Nine Haskell Problems: #3

Dec 21st, 2024 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 2.66 KB | Source Code | 0 0
  1. -- Find the K'th element of a list. The first element in the list is
  2. -- number 1.
  3.  
  4. main :: IO ()
  5. main = mapM_ (putStr . (\x -> if x == "eggs" then x ++ ";\n" else x ++ ", ")) $
  6.     [ one
  7.     , two
  8.     , three
  9.     , four
  10.     , five
  11.     , six
  12.     , seven
  13.     , eight
  14.     , nine
  15.     , ten
  16.     , eleven
  17.     , twelve
  18.     , thirteen
  19.     , fourteen
  20.     , fifteen
  21.     , sixteen
  22.     , seventeen] <*> [1 .. 6] <*> [lst]
  23.  
  24. lst :: [String]
  25. lst = ["foo", "bar", "baz", "ham", "spam", "eggs"]
  26.  
  27. one :: Int -> [a] -> a
  28. one n xs = xs !! (n - 1)
  29.  
  30. two :: Int -> [a] -> a
  31. two n xs = head $ drop (n - 1) xs
  32.  
  33. three :: Int -> [a] -> a
  34. three n xs = last $ take n xs
  35.  
  36. four :: Int -> [a] -> a
  37. four n xs = reverse xs !! (length xs - n)
  38.  
  39. five :: Int -> [a] -> a
  40. five n xs = head $ drop (length xs - n) (reverse xs)
  41.  
  42. six :: Int -> [a] -> a
  43. six n xs = last $ take (length xs + 1 - n) (reverse xs)
  44.  
  45. seven :: Int -> [a] -> a
  46. seven n xs = last $ fst $ splitAt n xs
  47.  
  48. eight :: Int -> [a] -> a
  49. eight n xs = head $ snd $ splitAt (n - 1) xs
  50.  
  51. nine :: Int -> [a] -> a
  52. nine = aux 0
  53.   where
  54.     aux l n (y:ys)
  55.       | l == (n - 1) = y
  56.       | otherwise = aux (l + 1) n ys
  57.  
  58. ten :: Int -> [a] -> a
  59. ten n xs = snd $
  60.            head $
  61.            filter (\t -> n == fst t) $ zip [1..] xs
  62.  
  63. eleven :: Int -> [a] -> a
  64. eleven n xs = snd $
  65.   foldl1 (\(f, s) (f1, s1) -> if f == n then (f, s) else (f1, s1)) $
  66.   zip [1..] xs
  67.  
  68. twelve :: Int -> [a] -> a
  69. twelve n xs = snd $
  70.   head $
  71.   zip [1..] xs >>= (\(f, s) -> [(f, s) | f == n])
  72.  
  73. thirteen :: Int -> [a] -> a
  74. thirteen n xs = snd $ last $ zip [1 .. n] xs
  75.  
  76. fourteen :: Int -> [a] -> a
  77. fourteen n xs = head [snd x | x <- zip [1..] xs, fst x == n]
  78.  
  79. fifteen :: Int -> [a] -> a
  80. fifteen n (x:xs) = if n == 1 then x else fifteen (n - 1) xs
  81.  
  82. sixteen :: Int -> [a] -> a
  83. sixteen n xs = snd $
  84.   head $
  85.   scanr1 (\(f, s) (f1, s1) -> if f == n then (f, s) else (f1, s1)) $
  86.   zip [1..] xs
  87.  
  88. seventeen :: Int -> [a] -> a
  89. seventeen n xs = aux (length xs) n xs
  90.   where
  91.     aux ll n (y:ys) = if ll - length ys == n then y else aux ll n ys
  92.  
  93. -- foo, bar, baz, ham, spam, eggs;
  94. -- foo, bar, baz, ham, spam, eggs;
  95. -- foo, bar, baz, ham, spam, eggs;
  96. -- foo, bar, baz, ham, spam, eggs;
  97. -- foo, bar, baz, ham, spam, eggs;
  98. -- foo, bar, baz, ham, spam, eggs;
  99. -- foo, bar, baz, ham, spam, eggs;
  100. -- foo, bar, baz, ham, spam, eggs;
  101. -- foo, bar, baz, ham, spam, eggs;
  102. -- foo, bar, baz, ham, spam, eggs;
  103. -- foo, bar, baz, ham, spam, eggs;
  104. -- foo, bar, baz, ham, spam, eggs;
  105. -- foo, bar, baz, ham, spam, eggs;
  106. -- foo, bar, baz, ham, spam, eggs;
  107. -- foo, bar, baz, ham, spam, eggs;
  108. -- foo, bar, baz, ham, spam, eggs;
  109. -- foo, bar, baz, ham, spam, eggs;
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement