Skip to content

Instantly share code, notes, and snippets.

@glaebhoerl
Created May 19, 2016 19:52
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 glaebhoerl/599406572040aaefa977205bf6cf0499 to your computer and use it in GitHub Desktop.
Save glaebhoerl/599406572040aaefa977205bf6cf0499 to your computer and use it in GitHub Desktop.
module Person (Person, null, newPerson, isNull, getAge, getName) where
-- constructor is not exported! only this module has access to the internal Maybe
newtype Person = Person (Maybe (Int, String))
null :: Person
null = Person Nothing
newPerson :: Int -> String -> Person
newPerson age name = Person (Just (age, name))
isNull :: Person -> Bool
isNull (Person maybeData) = isJust maybeData
getAge :: Person -> Int
getAge (Person (Just (age, _))) = age
getAge _ = error "NullPointerException"
getName :: Person -> String
getName (Person (Just (_, name))) = name
getName _ = error "NullPointerException"
@glaebhoerl
Copy link
Author

Or heck it would probably be even cleaner as data Person = Person { age :: Int, name :: String } | Null, and then you don't even need to hide anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment