Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Control.Applicative
- import Control.Monad (join)
- import qualified Data.Semigroup as S
- import Data.Maybe
- added :: Maybe Integer
- added = (+3) <$> (lookup 3 $ zip [1, 2, 3] [4, 5, 6])
- y :: Maybe Integer
- y = lookup 3 $ zip [1..3] [4..6]
- z :: Maybe Integer
- z = lookup 2 $ zip [1, 2, 3] [4, 5, 6]
- tupled :: Maybe (Integer,Integer)
- tupled = liftA2 (,) y z
- bind :: Monad m => (a -> m b) -> m a -> m b
- bind = ((.) join) . fmap
- data Sum a b = First a | Second b deriving (Eq, Show)
- instance Functor (Sum a) where
- fmap _ (First a) = First a
- fmap f (Second b) = Second (f b)
- instance Applicative (Sum a) where
- pure = Second
- First a <*> _ = First a
- _ <*> First a = First a
- Second f <*> Second v = Second (f v)
- instance Monad (Sum a) where
- return = pure
- First a >>= _ = First a
- Second b >>= f = f b
- l1 :: Monad m => (a -> b) -> m a -> m b
- l1 f v = v >>= return . f
- l2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
- -- l2 f ma mb = ma >>= (\a -> mb >>= (\b -> return $ f a b))
- l2 f ma mb = do {a <- ma; b <- mb; return $ f a b}
- ap :: Monad m => m a -> m (a -> b) -> m b
- -- ap ma mf = ma >>= (\a -> mf >>= (\f -> return $ f a))
- ap ma mf = do {a <- ma; f <- mf; return $ f a}
- mySum :: (Foldable t, Num a) => t a -> a
- mySum = S.getSum . foldMap S.Sum
- myProduct :: (Foldable t, Num a) => t a -> a
- myProduct = S.getProduct . foldMap S.Product
- myMin :: (Foldable t, Ord a) => t a -> Maybe a
- myMin = fmap S.getMin . foldMap (Just . S.Min)
- myMax :: (Foldable t, Ord a) => t a -> Maybe a
- myMax = fmap S.getMax . foldMap (Just . S.Max)
- -- myMax = S.getMax . foldr (\v acc -> S.Max (Just v) <> acc) (S.Max Nothing)
- myNull :: Foldable t => t a -> Bool
- myNull = foldr (\_ _ -> False) True
- myLen :: Foldable t => t a -> Int
- myLen = foldr (\_ acc -> acc + 1) 0
- my2List :: Foldable t => t a -> [a]
- my2List = foldr (:) []
- myFold :: (Foldable t, Monoid m) => t m -> m
- myFold = foldMap id
- myFoldMap :: (Foldable t, Monoid m) => (a -> m) -> t a -> m
- myFoldMap f = foldr (mappend . f) mempty
- data Morse = O | I deriving (Eq, Show)
- charToMorse :: Char -> Maybe Morse
- charToMorse '.' = Just O
- charToMorse '-' = Just I
- charToMorse _ = Nothing
- stringToMorse :: String -> [Morse]
- stringToMorse = fromMaybe [] . traverse charToMorse
- data Option a = Nada | Solo a
- instance Functor Option where
- fmap _ Nada = Nada
- fmap f (Solo a) = Solo (f a)
- instance Applicative Option where
- pure = Solo
- Nada <*> _ = Nada
- _ <*> Nada = Nada
- Solo f <*> Solo a = Solo (f a)
- instance Foldable Option where
- foldr _ z Nada = z
- foldr f z (Solo a) = f a z
- foldMap _ Nada = mempty
- foldMap f (Solo a) = f a
- instance Traversable Option where
- sequenceA Nada = pure Nada
- sequenceA (Solo a) = Solo <$> a
- traverse _ Nada = pure Nada
- traverse f (Solo a) = Solo <$> f a
- data Tree a =
- Empty
- | Leaf a
- | Node (Tree a) a (Tree a)
- deriving (Eq,Show)
- instance Functor Tree where
- fmap _ Empty = Empty
- fmap f (Leaf a) = Leaf (f a)
- fmap f (Node l c r) = Node (fmap f l) (f c) (fmap f r)
- instance Foldable Tree where
- foldMap _ Empty = mempty
- foldMap f (Leaf a) = f a
- foldMap f (Node l c r) = foldMap f l <> f c <> foldMap f r
- foldr _ z Empty = z
- foldr f z (Leaf a) = f a z
- foldr f z (Node l c r) = foldr f (foldr f (f c z) r) l
- instance Traversable Tree where
- traverse _ Empty = pure Empty
- traverse f (Leaf a) = Leaf <$> f a
- -- traverse f (Node l c r) = Node <$> traverse f l <*> f c <*> traverse f r
- traverse f (Node l k r) = liftA3 Node (traverse f l) (f k) (traverse f r)
- data Identity a = Identity { runIdentity :: a } deriving (Eq, Show)
- instance Functor Identity where
- fmap f (Identity a) = Identity (f a)
- instance Applicative Identity where
- pure = Identity
- (Identity f) <*> (Identity a) = Identity (f a)
- main :: IO ()
- main = undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement