Last active
August 29, 2015 14:25
-
-
Save danclien/b12582bbc93638fba8dd to your computer and use it in GitHub Desktop.
Example of using the `Monad` instance from `Maybe`.
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
data Person = Person { personId :: PersonId, personName :: Name } | |
parseInt :: String -> Maybe PersonId | |
getById :: PersonId -> Maybe Person | |
-- Accepts a raw string from the user | |
-- Attempts to parse it as an `Int` | |
-- Attempts to get a `Person` by that ID | |
-- Returns the `Name` of the Person | |
f :: String -> Maybe Name | |
f userInput = do | |
id' <- parseInt userInput | |
person <- getById id' | |
return $ personName person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment