SHARE
TWEET

Untitled

a guest Apr 20th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Maybe (fromMaybe)
  2.  
  3. notThe :: String -> Maybe String
  4. notThe "the" = Nothing
  5. notThe s = Just s
  6.  
  7. replaceThe :: String -> String
  8. replaceThe "" = ""
  9. replaceThe (' ':s) = ' ':replaceThe s
  10. replaceThe s =
  11.     let
  12.         (word, words) = span(/=' ') s
  13.         head = fromMaybe "a" $ notThe word
  14.     in
  15.         head ++ (replaceThe words)
  16.  
  17. vowels = "aeuioy"
  18. isVowel :: Char -> Bool
  19. isVowel = flip elem vowels
  20.  
  21. countTheBeforeVowel :: String -> Integer
  22. countTheBeforeVowel (' ':s) = countTheBeforeVowel s
  23. countTheBeforeVowel "" = 0
  24. countTheBeforeVowel s =
  25.     let
  26.         (word, words) = span(/=' ') s
  27.         headCount = case (word, words) of
  28.             ("the", (' ':a:_)) | isVowel a-> 1
  29.             _ -> 0
  30.     in
  31.         headCount + (countTheBeforeVowel words)
  32.  
  33.  
  34. countVowels :: String -> Integer
  35. countVowels = toInteger . length . filter isVowel
  36.  
  37. newtype Word' = Word' String deriving (Eq, Show)
  38.  
  39. mkWord :: String -> Maybe Word'
  40. mkWord s = if numVowels > numConsonants then Nothing else Just $ Word' s
  41.     where
  42.         numVowels = countVowels s
  43.         numConsonants = (toInteger $ length s) - numVowels
  44.  
  45. data Nat = Zero | Succ Nat deriving (Eq, Show)
  46.  
  47. natToInteger :: Nat -> Integer
  48. natToInteger Zero = 0
  49. natToInteger (Succ n) = 1 + (natToInteger n)
  50.  
  51. integerToNat :: Integer -> Maybe Nat
  52. integerToNat 0 = Just Zero
  53. integerToNat n | n > 0 =  integerToNat (n - 1) >>= Just . Succ
  54. integerToNat _ = Nothing
  55.  
  56. isJust :: Maybe a -> Bool
  57. isJust (Just _) = True
  58. isJust Nothing = False
  59.  
  60. isNothing :: Maybe a -> Bool
  61. isNothing = not . isJust
  62.  
  63. maybbee :: b -> (a -> b) -> Maybe a -> b
  64. maybbee _ f (Just a) = f a
  65. maybbee b _ Nothing = b
  66.  
  67. listToMaybe :: [a] -> Maybe a
  68. listToMaybe (h:_) = Just h
  69. listToMaybe [] = Nothing
  70.  
  71. maybeToList :: Maybe a -> [a]
  72. maybeToList (Just h) = [h]
  73. maybeToList Nothing = []
  74.  
  75. catMaybes :: [Maybe a] -> [a]
  76. catMaybes [] = []
  77. catMaybes (Just x:xs) = x:catMaybes xs
  78. catMaybes (Nothing:xs) = catMaybes xs
  79.  
  80. flipMaybe :: [Maybe a] -> Maybe [a]
  81. flipMaybe [] = Just []
  82. flipMaybe (Nothing:_) = Nothing
  83. flipMaybe ((Just x):xs)= (flipMaybe xs) >>=  Just . (x:)
  84.  
  85.  
  86.  
  87. eitherLeftToList :: Either a b -> [a]
  88. eitherLeftToList (Left a) = [a]
  89. eitherLeftToList (Right _) = []
  90.  
  91. reverseEither :: Either a b -> Either b a
  92. reverseEither (Left a) = Right a
  93. reverseEither (Right a) = Left a
  94.  
  95. lefts' :: [Either a b] -> [a]
  96. lefts' l = foldr (\e l-> (eitherLeftToList e) ++ l) [] l
  97.  
  98. rights' :: [Either a b] -> [b]
  99. rights' = lefts' . map reverseEither
  100.  
  101. either' :: (a -> c) -> (b -> c) -> Either a b -> c
  102. either' lf _ (Left l) = lf l
  103. either' _ rf (Right r) = rf r
  104.  
  105. eitherMaybe'' :: (b -> c) -> Either a b -> Maybe c
  106. eitherMaybe'' f = either' (const Nothing) (Just . f)
  107.  
  108. myIterate :: (a -> a) -> a -> [a]
  109. myIterate f start = start:myIterate f (f start)
  110.  
  111. myUnfoldr :: (b -> Maybe (a, b)) -> b -> [a]
  112. myUnfoldr f start = case f start of
  113.     Nothing -> []
  114.     Just (elem, newStart) -> elem:(myUnfoldr f newStart)
  115.  
  116. betterIterate :: (a -> a) -> a -> [a]
  117. betterIterate f = myUnfoldr (\e -> Just (f e, f e))
  118.  
  119. data BinaryTree a = Leaf | Node (BinaryTree a) a (BinaryTree a) deriving Show
  120.  
  121. unfold :: (a -> Maybe (a, b, a)) -> a -> BinaryTree b
  122. unfold f start = case f start of
  123.     Nothing -> Leaf
  124.     Just (ls, r, rs) -> Node (unfold f ls) r (unfold f rs)
  125.  
  126. treeBuild :: Integer -> BinaryTree Integer
  127. treeBuild n = unfold (\n' -> if n == n' then Nothing else Just (n' + 1, n' , n' + 1)) 0
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