Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created January 29, 2021 08:04
Show Gist options
  • Save aiya000/ff77d50b97f79bc7fc62a09425f213f9 to your computer and use it in GitHub Desktop.
Save aiya000/ff77d50b97f79bc7fc62a09425f213f9 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
newtype Fix f = Fix
{ unFix :: f (Fix f)
}
cata :: Functor f => (f a -> a) -> Fix f -> a
cata f (Fix x) = f $ fmap (cata f) x
data ListF a b = Cons a b | Nil
deriving (Show)
instance Functor (ListF a) where
fmap _ Nil = Nil
fmap f (Cons x y) = Cons x $ f y
type List a = Fix (ListF a)
instance Show a => Show (List a) where
show (Fix Nil) = "Nil"
show (Fix (Cons x y)) = show x <> ", " <> show y
xs :: List Int
xs = Fix (Cons 10 $ Fix (Cons 20 $ Fix Nil))
main :: IO ()
main = print xs
-- 10, 20, Nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment