Created
December 15, 2020 21:18
-
-
Save derrickturk/46873ad378f6de2422973d75bfa2def3 to your computer and use it in GitHub Desktop.
more bad lists
This file contains 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 #-} | |
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