Created
January 28, 2020 02:41
-
-
Save oisdk/9ecdbec1e1ff3a513991ff94ad71dda0 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 BangPatterns #-} | |
{-# LANGUAGE DeriveFoldable, DeriveTraversable, DeriveFunctor #-} | |
import Data.List (mapAccumL) | |
data Stream a = a :< Stream a deriving (Functor, Foldable, Traversable) | |
infixr 5 :< | |
instance Show a => Show (Stream a) where | |
showsPrec n = showsPrec n . foldr (:) [] | |
catalan :: Stream Integer | |
catalan = 1 :< snd (mapAccumL f const catalan) | |
where | |
f k x = (xs, xs 0 catalan) | |
where | |
xs !r (y :< ys) = k (r + x * y) ys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment