Created
December 17, 2013 18:45
-
-
Save banacorn/8010408 to your computer and use it in GitHub Desktop.
some algebra shit
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
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