Skip to content

Instantly share code, notes, and snippets.

@banacorn
Created December 17, 2013 18:45
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 banacorn/8010408 to your computer and use it in GitHub Desktop.
Save banacorn/8010408 to your computer and use it in GitHub Desktop.
some algebra shit
module Catamorphism where
import Prelude hiding (sum)
data Mu f = InF { outF :: f (Mu f) }
catamorphism :: Functor f => (f a -> a) -> Mu f -> a
catamorphism f = f . fmap (catamorphism f) . outF
--------------------------------------------------
data ListF a b = Nil | Cons a b
instance Functor (ListF a) where
fmap f Nil = Nil
fmap f (Cons a b) = Cons a (f b)
--------------------------------------------------
sum' :: ListF Int Int -> Int
sum' Nil = 0
sum' (Cons x xs) = x + xs
sum :: Mu (ListF Int) -> Int
sum = catamorphism sum'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment