Skip to content

Instantly share code, notes, and snippets.

@nuttycom
Created November 6, 2014 04:11
Show Gist options
  • Save nuttycom/ac77e76f482ac9567288 to your computer and use it in GitHub Desktop.
Save nuttycom/ac77e76f482ac9567288 to your computer and use it in GitHub Desktop.
Haskell list catamorphism
{-# LANGUAGE RankNTypes #-}
module ListAlgebra
( ListAlgebra(..)
) where
newtype ListAlgebra a = ListAlgebra (forall b. b -> (a -> b -> b) -> b)
nil :: ListAlgebra a
nil = ListAlgebra const
cons :: a -> ListAlgebra a -> ListAlgebra a
cons a (ListAlgebra f) = ListAlgebra (\b accf -> accf a (f b accf))
instance Show a => Show (ListAlgebra a) where
show (ListAlgebra f) =
let accf a b = show a ++ "," ++ b
in "[" ++ (f "]" accf)
@jfischoff
Copy link

Now write tail ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment