Created
January 29, 2021 08:04
-
-
Save aiya000/ff77d50b97f79bc7fc62a09425f213f9 to your computer and use it in GitHub Desktop.
This file contains 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
{-# 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