Created
November 6, 2014 04:11
-
-
Save nuttycom/ac77e76f482ac9567288 to your computer and use it in GitHub Desktop.
Haskell list catamorphism
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now write tail ;)