-
-
Save zraffer/b9edccaad079f51bc7f2 to your computer and use it in GitHub Desktop.
Possibly pointless? encoding of lists as their own paramorphisms.
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 ExistentialQuantification #-} | |
{-# LANGUAGE Rank2Types #-} | |
import Data.Maybe | |
data List a = L (forall b. (Maybe (a, (b, List a)) -> b) -> b) | |
nil :: List a | |
nil = L (\f -> f Nothing) | |
cons :: a -> List a -> List a | |
cons x xs@(L xs') = L (\f -> f (Just (x, (xs' f, xs)))) | |
elim :: List a -> (Maybe (a, List a) -> b) -> b | |
elim (L xs) f = xs (f Nothing `maybe` uncurry (\x -> uncurry (\r xs -> f (Just (x, xs))))) | |
hd :: List a -> Maybe a | |
hd xs = elim xs (Nothing `maybe` (Just . fst)) | |
tl :: List a -> Maybe (List a) | |
tl xs = elim xs (Nothing `maybe` (Just . snd)) | |
xs = cons 1 (cons 2 (cons 3 nil)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment