Skip to content

Commit 4f048c9

Browse files
author
Anastasios Valtinos
committedOct 6, 2022
Checking Instance knowledge
1 parent f95507a commit 4f048c9

File tree

1 file changed

+23
-0
lines changed
  • Haskell Excercises & Code/MFAM - Practice

1 file changed

+23
-0
lines changed
 
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module MonadicStuff where
2+
3+
-- Checking udnerstanding of Monad instance for Maybe
4+
-- To write Monad. we need to write ApplICATIVE
5+
-- To write applicative we need Functor
6+
--import Data.Maybe
7+
8+
instance Functor Maybe where
9+
fmap f Nothing = Nothing
10+
fmap f (Just x) = Just (f x)
11+
12+
instance Applicative Maybe where
13+
pure x = Just x
14+
-- pure id <*> v = v
15+
-- pure should not change the underlying values
16+
Nothing <*> _ = Nothing
17+
_ <*> Nothing = Nothing
18+
Just f <*> Just n = Just (f n)
19+
20+
instance Monad Maybe where
21+
return = Just
22+
Nothing >>= f = Nothing
23+
(Just n) >>= f = f n

0 commit comments

Comments
 (0)