SHARE
TWEET

Haskell ZH 2019-12-17 "A"

Kikikan Dec 17th, 2019 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List
  2.  
  3. toBack :: [a] -> [a]
  4. toBack [] = []
  5. toBack [x] = [x]
  6. toBack (x:xs) = xs ++ [x]
  7.  
  8. assocRL :: (a, (b, c)) -> ((a, b), c)
  9. assocRL (x, (y, z)) = ((x, y), z)
  10.  
  11. mirrorFirst :: [a] -> [a]
  12. mirrorFirst [] = []
  13. mirrorFirst [x] = [x]
  14. mirrorFirst (x:xs) = (reverse xs) ++ [x] ++ xs
  15.  
  16. heads :: [[a]] -> [a]
  17. heads [] = []
  18. heads [[]] = []
  19. heads [[x]] = [x]
  20. heads ([]:xs) = heads xs
  21. heads (x:xs) = head x : heads xs
  22.  
  23. isSorted :: Ord a => [a] -> Bool
  24. isSorted xs = xs == reverse (sort xs)
  25.  
  26. countEmpties :: [[a]] -> Int
  27. countEmpties [] = 0
  28. countEmpties [[]] = 1
  29. countEmpties ([]:xs) = 1 + countEmpties xs
  30. countEmpties (x:xs) = 0 + countEmpties xs
  31.  
  32. takeLast :: Int -> [a] -> [a]
  33. takeLast _ [] = []
  34. takeLast x l = se x l []
  35.     where
  36.         se :: Int -> [a] -> [a] -> [a]
  37.         se x [] e = e
  38.         se x l e
  39.             | x == 0 = e
  40.             | otherwise = se (x-1) (init l) (last l : e)
  41.  
  42. apMaybe :: Maybe (a -> b) -> Maybe a -> Maybe b
  43. apMaybe Nothing _ = Nothing
  44. apMaybe _ Nothing = Nothing
  45. apMaybe (Just f) (Just a) = Just (f a)
  46.  
  47. reverseWords :: String -> String
  48. reverseWords a = s a ""
  49.     where
  50.         s :: String -> String -> String
  51.         s a z
  52.             | length (words a) > 1 = s (unwords (tail (words a))) (z ++ reverse (head (words a)) ++ " ")
  53.             | otherwise = z ++ (reverse a)
  54.  
  55. pipeline :: [a -> a] -> a -> a
  56. pipeline [] x = x
  57. pipeline l x = pipeline (init l) ((last l) x)
  58.  
  59. lookupPair :: (k -> v -> Bool) -> [(k,v)] -> Maybe (k,v)
  60. lookupPair _ [] = Nothing
  61. lookupPair f ((x,y):xs)
  62.     | f x y = Just (x,y)
  63.     | otherwise = lookupPair f xs
  64.  
  65. onlyNonEmptySatisfying :: (a -> Bool) -> [[a]] -> [[a]]
  66. onlyNonEmptySatisfying f [] = []
  67. onlyNonEmptySatisfying f [[]] = []
  68. onlyNonEmptySatisfying f ([]:xs) = onlyNonEmptySatisfying f xs
  69. onlyNonEmptySatisfying f (x:xs)
  70.     | length (filter f x) > 0 = filter f x : onlyNonEmptySatisfying f xs
  71.     | otherwise = onlyNonEmptySatisfying f xs
  72.  
  73. data Result a = Error String | Ok a
  74.     deriving (Eq, Show)
  75.  
  76. safeHead :: [a] -> Result a
  77. safeHead [] = Error "empty list"
  78. safeHead (x:xs) = Ok x
  79.  
  80. safeIndex :: [a] -> Int -> Result a
  81. safeIndex [] _ = Error "index out of bounds"
  82. safeIndex (y:ys) x
  83.     | x < 0 = Error "index out of bounds"
  84.     | x == 0 = Ok y
  85.     | otherwise = safeIndex ys (x-1)
RAW Paste Data
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
Top