Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created December 15, 2020 21:18
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 derrickturk/46873ad378f6de2422973d75bfa2def3 to your computer and use it in GitHub Desktop.
Save derrickturk/46873ad378f6de2422973d75bfa2def3 to your computer and use it in GitHub Desktop.
more bad lists
{-# LANGUAGE RankNTypes #-}
newtype Listy a = Listy { runListy :: forall b . (a -> b -> b) -> b -> b }
nil :: Listy a
nil = Listy $ \_ init -> init
cons :: a -> Listy a -> Listy a
cons x xs = Listy $ \fn init -> fn x (runListy xs fn init)
foldr :: (a -> b -> b) -> b -> Listy a -> b
foldr fn init xs = runListy xs fn init
-- https://stackoverflow.com/questions/6172004/writing-foldl-using-foldr
foldl :: (b -> a -> b) -> b -> Listy a -> b
foldl fn init xs = runListy xs (\x g a -> g (fn a x)) id $ init
instance Functor Listy where
fmap f xs = runListy xs (\h t -> cons (f h) t) nil
toList :: Listy a -> [a]
toList xs = runListy xs (:) []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment