Skip to content

Instantly share code, notes, and snippets.

@jFransham
Last active September 27, 2016 15:21
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 jFransham/a78fa9fad3bfbcdab35b07a700314cc1 to your computer and use it in GitHub Desktop.
Save jFransham/a78fa9fad3bfbcdab35b07a700314cc1 to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
data Z
data S n
type L a = forall b. (a -> b -> b) -> b -> b
consL a fold = \cons nil -> cons a (fold cons nil)
(@:) = consL
infixr @:
nilL = \cons nil -> nil
fromList :: [a] -> L a
fromList = foldr consL nilL
showL :: Show a => L a -> String
showL fold = fold (\x rest -> show x ++ " @: " ++ rest) "nilL"
tailL :: L a -> L a
tailL fold = fst $ fold (\x (_, xs) -> (xs, x@:xs)) (undefined, nilL)
main :: IO ()
main = putStrLn $ showL $ tailL $ fromList [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment