From 48639513952f32df6c88031218135f638413cab1 Mon Sep 17 00:00:00 2001 From: Dzyuba Vlad Date: Tue, 13 Dec 2016 08:59:58 +0200 Subject: [PATCH 1/3] Added first example --- Haskell/maybe.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Haskell/maybe.hs diff --git a/Haskell/maybe.hs b/Haskell/maybe.hs new file mode 100644 index 0000000..07a2846 --- /dev/null +++ b/Haskell/maybe.hs @@ -0,0 +1,19 @@ +import Prelude hiding (Maybe(..), Functor, fmap, (<$>)) + +data Maybe a = Just a | Nothing + deriving (Show) + +class Functor f where + fmap :: (a -> b) -> f a -> f b + +(<$>) :: Functor f => (a -> b) -> f a -> f b +(<$>) = fmap +infixl 4 <$> + +instance Functor Maybe where + fmap f (Just x) = Just $ f x + fmap _ Nothing = Nothing + +main = do + print $ (* 2) <$> Just 5 + print $ (* 2) <$> Nothing From 700e8212c702f88f8f6ca5095165fe92f429d594 Mon Sep 17 00:00:00 2001 From: Dzyuba Vlad Date: Tue, 13 Dec 2016 09:10:59 +0200 Subject: [PATCH 2/3] Add second example --- Haskell/maybe2.hs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Haskell/maybe2.hs diff --git a/Haskell/maybe2.hs b/Haskell/maybe2.hs new file mode 100644 index 0000000..803222f --- /dev/null +++ b/Haskell/maybe2.hs @@ -0,0 +1,3 @@ +main = do + print $ (* 2) . (+ 5) . (^ 2) <$> Just 5 + print $ (* 2) . (+ 5) . (^ 2) <$> Nothing From 493ddfb69893064c262d95ffaf697298c95c2a52 Mon Sep 17 00:00:00 2001 From: Dzyuba Vlad Date: Tue, 13 Dec 2016 09:42:13 +0200 Subject: [PATCH 3/3] Add third example --- Haskell/maybe3.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Haskell/maybe3.hs diff --git a/Haskell/maybe3.hs b/Haskell/maybe3.hs new file mode 100644 index 0000000..ef94131 --- /dev/null +++ b/Haskell/maybe3.hs @@ -0,0 +1,24 @@ +import Prelude hiding (Applicative(..)) + +class Functor f => Applicative f where + pure :: a -> f a + (<*>) :: f (a -> b) -> f a -> f b + (*>) :: f a -> f b -> f b + (<*) :: f a -> f b -> f a + + a *> b = fmap const b <*> a + (<*) = flip (*>) + {-# MINIMAL pure, (<*>) #-} + +instance Applicative Maybe where + pure = Just + (Just f) <*> v = fmap f v + Nothing <*> _ = Nothing + +main = do + let a = Just 5 + b = Just $ (* 2) . (+ 5) . (^ 2) + c = b <*> a + print c + +