Last active
September 27, 2016 15:21
-
-
Save jFransham/a78fa9fad3bfbcdab35b07a700314cc1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 #-} | |
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