-
-
Save cds-amal/a9a3cf06d61de488aece67ad3260a8da to your computer and use it in GitHub Desktop.
Doing some haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Functors | |
-- fmap :: (a -> b) -> f a -> f b | |
class MyFunctor f where | |
fmap :: (a -> b) -> f a -> f b | |
data Perhaps a = Surely a | Naught deriving (Show) | |
instance Eq a => Eq (Perhaps a) where | |
(Surely x) == (Surely y) = x == y | |
Naught == Naught = True | |
_ == _ = False | |
instance MyFunctor Perhaps where | |
fmap _ Naught = Naught | |
fmap f (Surely a) = Surely (f a) | |
-- Applicative Functors | |
-- pure :: a -> f a | |
-- <*> :: f (a -> b) -> f a -> f b | |
class MyApplicative f where | |
pure :: a -> f a | |
apply :: f (a -> b) -> f a -> f b | |
instance MyApplicative Perhaps where | |
pure x = Surely x | |
apply (Surely f) (Surely x) = Surely (f x) | |
apply _ Naught = Naught | |
apply Naught _ = Naught | |
-- Monads | |
-- (\x -> Just 10) <*> (Just 1) | |
-- Main.fmap (Surely (\x -> x + 1)) (Surely 10) | |
main :: IO () | |
main = putStr "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment