Created
May 19, 2016 19:52
-
-
Save glaebhoerl/599406572040aaefa977205bf6cf0499 to your computer and use it in GitHub Desktop.
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
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.