From 4f048c922563189a1b87ee1df2e17f6fc150259f Mon Sep 17 00:00:00 2001 From: Anastasios Valtinos Date: Thu, 6 Oct 2022 14:24:07 +0300 Subject: [PATCH] Checking Instance knowledge --- .../MFAM - Practice/monad.hs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Haskell Excercises & Code/MFAM - Practice/monad.hs diff --git a/Haskell Excercises & Code/MFAM - Practice/monad.hs b/Haskell Excercises & Code/MFAM - Practice/monad.hs new file mode 100644 index 0000000..9792b83 --- /dev/null +++ b/Haskell Excercises & Code/MFAM - Practice/monad.hs @@ -0,0 +1,23 @@ +module MonadicStuff where + +-- Checking udnerstanding of Monad instance for Maybe +-- To write Monad. we need to write ApplICATIVE +-- To write applicative we need Functor +--import Data.Maybe + +instance Functor Maybe where + fmap f Nothing = Nothing + fmap f (Just x) = Just (f x) + +instance Applicative Maybe where + pure x = Just x + -- pure id <*> v = v + -- pure should not change the underlying values + Nothing <*> _ = Nothing + _ <*> Nothing = Nothing + Just f <*> Just n = Just (f n) + +instance Monad Maybe where + return = Just + Nothing >>= f = Nothing + (Just n) >>= f = f n