Skip to content

Instantly share code, notes, and snippets.

@ChristopherKing42
Created March 15, 2016 22:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristopherKing42/bb61e60c488b22b119ad to your computer and use it in GitHub Desktop.
Save ChristopherKing42/bb61e60c488b22b119ad to your computer and use it in GitHub Desktop.
{-# LANGUAGE Rank2Types, GADTs, LambdaCase #-}
data Stream a res where
Head :: Stream a a
Tail :: Stream a (CoData (Stream a))
newtype CoData con = CoD {unCoD :: forall r. con r -> r}
appendList :: [a] -> CoData (Stream a) -> CoData (Stream a)
appendList [] s = s
appendList (x:xs) s = CoD $ \case
Head -> x
Tail -> appendList xs s
cycle :: [a] -> CoData (Stream a)
cycle xs = let r = appendList xs r in r
toList :: CoData (Stream a) -> [a]
toList (CoD f) = f Head : toList (f Tail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment