Skip to content

Instantly share code, notes, and snippets.

@bradparker
Created April 2, 2019 05:12
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 bradparker/75dba2b56a154cf18f4e5128b6d9e135 to your computer and use it in GitHub Desktop.
Save bradparker/75dba2b56a154cf18f4e5128b6d9e135 to your computer and use it in GitHub Desktop.
Newtype Lists
{-# LANGUAGE TupleSections #-}
newtype NonEmpty a = NonEmpty { getNonEmpty :: (a, List a) }
newtype List a = List { getList :: Maybe (NonEmpty a) }
nil :: List a
nil = List Nothing
cons :: a -> List a -> List a
cons a =
List . fmap (NonEmpty . (a,) . uncurry cons . getNonEmpty) . getList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment