Skip to content

Instantly share code, notes, and snippets.

@tmkelly28
Created November 3, 2019 21:42
Show Gist options
  • Save tmkelly28/a0dff8f0ca3be002b8b86542c0d8b78e to your computer and use it in GitHub Desktop.
Save tmkelly28/a0dff8f0ca3be002b8b86542c0d8b78e to your computer and use it in GitHub Desktop.
Doing some haskell
-- 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