Skip to content

Instantly share code, notes, and snippets.

@iurii-kyrylenko
Last active January 7, 2019 18:03
Show Gist options
  • Save iurii-kyrylenko/88665b91bccae4ad87866bc79a69fdc5 to your computer and use it in GitHub Desktop.
Save iurii-kyrylenko/88665b91bccae4ad87866bc79a69fdc5 to your computer and use it in GitHub Desktop.
Functor-Applicative-Monad
--- functor from applicative ---
-- fmap2 (2*) [1,2,3,4]
fmap2 :: Applicative f => (a -> b) -> f a -> f b
-- fmap2 f x = pure f <*> x
-- fmap2 f = (<*>) (pure f)
fmap2 = (<*>) . pure
--- functor from monad (liftM)
-- fmap3 (2*) [1,2,3,4]
fmap3 :: Monad m => (a -> b) -> m a -> m b
-- fmap3 f x = x >>= \t -> return (f t)
-- fmap3 f x = x >>= return . f
-- fmap3 f x = (>>=) x (return . f)
fmap3 f = (=<<) (return . f)
--- applicative from monad
-- app2 [(*2),(+3)] [42]
app2 :: Monad m => m (a -> b) -> m a -> m b
-- app2 g t = do f <- g
-- x <- t
-- return (f x)
-- app2 g t = g >>= \f -> t >>= \x -> return (f x)
app2 g t = g >>= \f -> t >>= return . f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment