Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created January 28, 2020 02:41
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 oisdk/9ecdbec1e1ff3a513991ff94ad71dda0 to your computer and use it in GitHub Desktop.
Save oisdk/9ecdbec1e1ff3a513991ff94ad71dda0 to your computer and use it in GitHub Desktop.
{-# 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