Created
January 1, 2019 11:40
-
-
Save lironsade/3b72dd070f3ff8fd0776dd7579b98f16 to your computer and use it in GitHub Desktop.
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
data MyList a = Empty | List (a, (MyList a)) deriving (Show, Eq) | |
instance Functor MyList where | |
fmap _ Empty = Empty | |
fmap f (List (x, xs)) = List (f x, fmap f xs) | |
flatten :: (MyList (MyList (a))) -> (MyList a) | |
flatten Empty = Empty | |
flatten (List (x, xs)) = myConcat x (flatten xs) | |
myConcat :: (MyList a) -> (MyList a) -> (MyList a) | |
myConcat xs Empty = xs | |
myConcat Empty ys = ys | |
myConcat (List (x, xs)) ys = List (x ,(myConcat xs ys)) | |
addToEnd :: a -> (MyList a) -> (MyList a) | |
addToEnd a Empty = List (a, Empty) | |
addToEnd a xs = myConcat xs (List (a, Empty)) | |
instance Monad MyList where | |
return x = List (x, Empty) | |
Empty >>= _ = Empty | |
xs >>= f = flatten $ fmap f xs | |
instance Applicative MyList where | |
pure = return | |
(<*>) Empty xs = Empty | |
(<*>) xs Empty = Empty | |
(<*>) (List (f, fs)) xs = myConcat (fmap f xs ) ((<*>) fs xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment