Advertisement
banovski

Ninety-Nine Haskell Problems, #1

Sep 26th, 2022 (edited)
2,539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Find the last element of a list.
  2.  
  3. one = last
  4.  
  5. two xs = xs !! (length xs - 1)
  6.  
  7. three = head . reverse
  8.  
  9. four [x] = x
  10. four (x:xs) = four xs
  11.  
  12. five xs
  13.   | length xs == 1 = xs !! 0
  14.   | otherwise = five (tail xs)
  15.  
  16. six [x] = x
  17. six xs = six $ tail xs
  18.  
  19. seven = head . aux
  20.   where
  21.     aux [] = []
  22.     aux (x:xs) = aux xs ++ [x]
  23.  
  24. eight = last . id
  25.  
  26. nine xs =
  27.   snd $
  28.   head $ filter (\(a, b) -> a == 0) $ zip [length xs - 1,length xs - 2 .. 0] xs
  29.  
  30. ten xs =
  31.   (\[(a, b)] -> b) $ dropWhile (\(a, b) -> a < length xs - 1) $ zip [0 ..] xs
  32.  
  33. eleven :: [a] -> a
  34. eleven = foldl1 (\x y -> y)
  35.  
  36. twelve = head . scanr1 (\x y -> y)
  37.  
  38. thirteen xs = (snd . maximum . zip [0 ..]) xs
  39.  
  40. fourteen (x:xs) = aux x xs
  41.   where
  42.     aux y l@(z:zs) | null l = y
  43.                    | null zs = z
  44.                    | otherwise = aux z zs
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement