Created
April 3, 2019 07:37
-
-
Save jaspervdj/f43d93bf5abfa2af5e67b04612884199 to your computer and use it in GitHub Desktop.
Is List or NonEmpty "simpler"? Neither.
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
-------------------------------------------------------------------------------- | |
-- Is List or NonEmpty "simpler"? Neither: | |
type NonEmpty a = Fix (Compose ((,) a) Maybe) | |
type List a = Fix (Compose Maybe ((,) a)) | |
-------------------------------------------------------------------------------- | |
newtype Fix f = Fix {unFix :: f (Fix f)} | |
newtype Compose f g a = Compose {unCompose :: f (g a)} | |
instance (Functor f, Functor g) => Functor (Compose f g) where | |
fmap f = Compose . fmap (fmap f) . unCompose | |
-------------------------------------------------------------------------------- | |
ne1, ne2 :: NonEmpty Int | |
ne1 = Fix (Compose (1, Nothing)) | |
ne2 = Fix (Compose (1, Just (Fix (Compose (2, Nothing))))) | |
l0, l1, l2 :: List Int | |
l0 = Fix (Compose Nothing) | |
l1 = Fix (Compose (Just (1, Fix (Compose Nothing)))) | |
l2 = Fix (Compose (Just (1, Fix (Compose (Just (2, Fix (Compose Nothing))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment