Created
November 3, 2019 21:42
-
-
Save tmkelly28/a0dff8f0ca3be002b8b86542c0d8b78e 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