Skip to content

Instantly share code, notes, and snippets.

@buggymcbugfix
Created November 11, 2017 22:07
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 buggymcbugfix/ec431b0291031482ffd00136caa1ffde to your computer and use it in GitHub Desktop.
Save buggymcbugfix/ec431b0291031482ffd00136caa1ffde to your computer and use it in GitHub Desktop.
import Data.Monoid
data List a = List | List a :• a
xs • x = xs :• x
infixl •
instance Show a => Show (List a) where
show List = "List"
show (xs :• x) = show xs ++ " •" ++ show x
ex1 = List • 1 • 2 • 3 • 4 • 5
ex2 = List
• "Eggs"
• "Milk"
• "Flour"
• "Cabbage"
instance Monoid (List a) where
mempty = List
mappend List ys = ys
mappend xs List = xs
mappend xs (ys :• y) = mappend xs ys :• y
instance Functor List where
fmap f List = List
fmap f (xs :• x) = (fmap f xs) • f x
ex3 = (++ "!") <$> ex2 <> fmap show ex1
-- List •"Eggs!" •"Milk!" •"Flour!" •"Cabbage!" •"1!" •"2!" •"3!" •"4!" •"5!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment