Skip to content

Instantly share code, notes, and snippets.

@zraffer
Forked from pbl64k/para.hs
Created March 3, 2016 13:57
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 zraffer/b9edccaad079f51bc7f2 to your computer and use it in GitHub Desktop.
Save zraffer/b9edccaad079f51bc7f2 to your computer and use it in GitHub Desktop.
Possibly pointless? encoding of lists as their own paramorphisms.
{-# 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