Skip to content

Instantly share code, notes, and snippets.

@danclien
Last active August 29, 2015 14:25
Show Gist options
  • Save danclien/b12582bbc93638fba8dd to your computer and use it in GitHub Desktop.
Save danclien/b12582bbc93638fba8dd to your computer and use it in GitHub Desktop.
Example of using the `Monad` instance from `Maybe`.
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