Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. module Adversity where
  2.  
  3. import Data.Maybe (maybe)
  4.  
  5. -- Determine the kinds
  6. -- 1. Given id :: a -> a
  7. -- :k a = *
  8. -- 2. r :: a -> f a
  9. -- :k a = *
  10. -- :k f a = *
  11. -- :k f = * -> *
  12. -- String processing
  13. -- 1.
  14. notThe :: String -> Maybe String
  15. notThe "the" = Nothing
  16. notThe x = Just x
  17.  
  18. replaceThe :: String -> String
  19. replaceThe = unwords . map (maybe "a" id . notThe) . words
  20.  
  21. -- 2.
  22. countTheBeforeVowel :: String -> Integer
  23. countTheBeforeVowel input = sum $ zipWith matchTHe notThes (tail notThes)
  24. where notThes = fmap (notThe) $ words input
  25. matchTHe Nothing (Just s) = if isVowel (head s) then 1 else 0
  26. matchTHe _ _ = 0
  27. isVowel = flip elem "aeiou"
  28. -- 3.
  29. isVowel = flip elem "aeiou"
  30. countVowels :: String -> Int
  31. countVowels = length . filter isVowel
  32.  
  33. -- Validate the word
  34. newtype Word' = Word' String deriving (Eq, Show)
  35. mkWord :: String -> Maybe Word'
  36. mkWord word = if valid then Just $ Word' word else Nothing
  37. where valid = countVowels word < length word - countVowels word
  38.  
  39. -- It’s only Natural
  40. data Nat = Zero | Succ Nat
  41. deriving (Eq, Show)
  42. natToInteger :: Nat -> Integer
  43. natToInteger Zero = 0
  44. natToInteger (Succ x) = 1 + natToInteger x
  45.  
  46. integerToNat :: Integer -> Maybe Nat
  47. integerToNat x = if valid then Just $ convert x else Nothing
  48. where valid = x > 0
  49. convert 0 = Zero
  50. convert x = Succ (convert $ x-1)
  51.  
  52. -- Small library for Maybe
  53. -- 1.
  54. isJust :: Maybe a -> Bool
  55. isJust Nothing = False
  56. isJust _ = True
  57.  
  58. isNothing :: Maybe a -> Bool
  59. isNothing = not . isJust
  60.  
  61. -- 2.
  62. mayybee :: b -> (a -> b) -> Maybe a -> b
  63. mayybee def f Nothing = def
  64. mayybee def f (Just x) = f x
  65.  
  66. -- 3.
  67. fromMaybe :: a -> Maybe a -> a
  68. fromMaybe def Nothing = def
  69. fromMaybe def (Just x) = x
  70.  
  71. -- 4.
  72. listToMaybe :: [a] -> Maybe a
  73. listToMaybe [] = Nothing
  74. listToMaybe (x:_) = Just x
  75.  
  76. maybeToList :: Maybe a -> [a]
  77. maybeToList Nothing = []
  78. maybeToList (Just x) = [x]
  79.  
  80. --5.
  81. catMaybes :: [Maybe a] -> [a]
  82. catMaybes (Nothing:xs) = catMaybes xs
  83. catMaybes ((Just x): xs) = x : catMaybes xs
  84.  
  85. -- 6.
  86. flipMaybe :: Eq a => [Maybe a] -> Maybe [a]
  87. flipMaybe xs = if Nothing `elem` xs then Nothing else Just $ catMaybes xs
  88.  
  89. -- Small library for Either
  90. -- 1.
  91. lefts' :: [Either a b] -> [a]
  92. lefts' = foldr (\val acc -> if isLeft val then (fromLeft val):acc else acc) []
  93. where
  94. isLeft (Left _) = True
  95. isLeft _ = False
  96. fromLeft (Left x) = x
  97.  
  98. -- 2.
  99. rights' :: [Either a b] -> [b]
  100. rights' = foldr (\val acc -> if isRight val then (fromRight val):acc else acc) []
  101. where
  102. isRight (Right _) = True
  103. isRight _ = False
  104. fromRight (Right x) = x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement