Advertisement
Guest User

Untitled

a guest
Aug 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Applicative
  2. import Control.Monad (join)
  3. import qualified Data.Semigroup as S
  4. import Data.Maybe
  5.  
  6. added :: Maybe Integer
  7. added = (+3) <$> (lookup 3 $ zip [1, 2, 3] [4, 5, 6])
  8.  
  9. y :: Maybe Integer
  10. y = lookup 3 $ zip [1..3] [4..6]
  11.  
  12. z :: Maybe Integer
  13. z = lookup 2 $ zip [1, 2, 3] [4, 5, 6]
  14.  
  15. tupled :: Maybe (Integer,Integer)
  16. tupled = liftA2 (,) y z
  17.  
  18. bind :: Monad m => (a -> m b) -> m a -> m b
  19. bind = ((.) join) . fmap
  20.  
  21. data Sum  a b = First a | Second b deriving (Eq, Show)
  22.  
  23. instance Functor (Sum a) where
  24.   fmap _ (First a) = First a
  25.   fmap f (Second b) = Second (f b)
  26.  
  27. instance Applicative (Sum a) where
  28.   pure = Second
  29.   First a <*> _ = First a
  30.   _ <*> First a = First a
  31.   Second f <*> Second v = Second (f v)
  32.  
  33. instance Monad (Sum a) where
  34.   return = pure
  35.   First a >>= _ = First a
  36.   Second b >>= f = f b
  37.  
  38. l1 :: Monad m => (a -> b) -> m a -> m b
  39. l1 f v = v >>= return . f
  40.  
  41. l2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
  42. -- l2 f ma mb = ma >>= (\a -> mb >>= (\b -> return $ f a b))
  43. l2 f ma mb = do {a <- ma; b <- mb; return $ f a b}
  44.  
  45. ap :: Monad m => m a -> m (a -> b) -> m b
  46. -- ap ma mf = ma >>= (\a -> mf >>= (\f -> return $ f a))
  47. ap ma mf = do {a <- ma; f <- mf; return $ f a}
  48.  
  49. mySum :: (Foldable t, Num a) => t a -> a
  50. mySum = S.getSum . foldMap S.Sum
  51.  
  52. myProduct :: (Foldable t, Num a) => t a -> a
  53. myProduct = S.getProduct . foldMap S.Product
  54.  
  55. myMin :: (Foldable t, Ord a) => t a -> Maybe a
  56. myMin = fmap S.getMin . foldMap (Just . S.Min)
  57.  
  58. myMax :: (Foldable t, Ord a) => t a -> Maybe a
  59. myMax = fmap S.getMax . foldMap (Just . S.Max)
  60. -- myMax = S.getMax . foldr (\v acc -> S.Max (Just v) <> acc) (S.Max Nothing)
  61.  
  62. myNull :: Foldable t => t a -> Bool
  63. myNull = foldr (\_ _ -> False) True
  64.  
  65. myLen :: Foldable t => t a -> Int
  66. myLen = foldr (\_ acc -> acc + 1) 0
  67.  
  68. my2List :: Foldable t => t a -> [a]
  69. my2List = foldr (:) []
  70.  
  71. myFold :: (Foldable t, Monoid m) => t m -> m
  72. myFold = foldMap id
  73.  
  74. myFoldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m
  75. myFoldMap f = foldr (mappend . f) mempty
  76.  
  77. data Morse = O | I deriving (Eq, Show)
  78.  
  79. charToMorse :: Char -> Maybe Morse
  80. charToMorse '.' = Just O
  81. charToMorse '-' = Just I
  82. charToMorse  _  = Nothing
  83.  
  84. stringToMorse :: String -> [Morse]
  85. stringToMorse = fromMaybe [] . traverse charToMorse
  86.  
  87. data Option a = Nada | Solo a
  88.  
  89. instance Functor Option where
  90.   fmap _ Nada = Nada
  91.   fmap f (Solo a) = Solo (f a)
  92.  
  93. instance Applicative Option where
  94.   pure = Solo
  95.  
  96.   Nada <*> _ = Nada
  97.   _ <*> Nada = Nada
  98.   Solo f <*> Solo a = Solo (f a)
  99.  
  100. instance Foldable Option where
  101.   foldr _ z Nada     = z
  102.   foldr f z (Solo a) = f a z
  103.  
  104.   foldMap _ Nada     = mempty
  105.   foldMap f (Solo a) = f a
  106.  
  107. instance Traversable Option where
  108.   sequenceA Nada     = pure Nada
  109.   sequenceA (Solo a) = Solo <$> a
  110.  
  111.   traverse _  Nada    = pure Nada
  112.   traverse f (Solo a) = Solo <$> f a
  113.  
  114. data Tree a =
  115.     Empty
  116.   | Leaf a
  117.   | Node (Tree a) a (Tree a)
  118.   deriving (Eq,Show)
  119.  
  120. instance Functor Tree where
  121.   fmap _ Empty        = Empty
  122.   fmap f (Leaf a)     = Leaf (f a)
  123.   fmap f (Node l c r) = Node (fmap f l) (f c) (fmap f r)
  124.  
  125. instance Foldable Tree where
  126.   foldMap _ Empty        = mempty
  127.   foldMap f (Leaf a)     = f a
  128.   foldMap f (Node l c r) = foldMap f l <> f c <> foldMap f r
  129.  
  130.   foldr _ z Empty        = z
  131.   foldr f z (Leaf a)     = f a z
  132.   foldr f z (Node l c r) = foldr f (foldr f (f c z) r) l
  133.  
  134. instance Traversable Tree where
  135.   traverse _ Empty    = pure Empty
  136.   traverse f (Leaf a) = Leaf <$> f a
  137.   -- traverse f (Node l c r) = Node <$> traverse f l <*> f c <*> traverse f r
  138.   traverse f (Node l k r) = liftA3 Node (traverse f l) (f k) (traverse f r)
  139.  
  140. data Identity a = Identity { runIdentity :: a } deriving (Eq, Show)
  141.  
  142. instance Functor Identity where
  143.   fmap f (Identity a) = Identity (f a)
  144.  
  145. instance Applicative Identity where
  146.   pure = Identity
  147.   (Identity f) <*> (Identity a) = Identity (f a)
  148.  
  149. main :: IO ()
  150. main = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement