Skip to content

Instantly share code, notes, and snippets.

@lironsade
Created January 1, 2019 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lironsade/3b72dd070f3ff8fd0776dd7579b98f16 to your computer and use it in GitHub Desktop.
Save lironsade/3b72dd070f3ff8fd0776dd7579b98f16 to your computer and use it in GitHub Desktop.
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