I have a type
data Foo = Foo { bar :: Bar, baz :: Baz }
and I am trying to parse a file to construct a Foo. I have functions to try parsing the Bar and Baz members
parseBar :: String -> Maybe Bar
parseBar line = ...
parseBaz :: String -> Maybe Baz
parseBaz line = ...
that may fail, and a function that maybe constructs the Foo from the results
parseFoo :: String -> Maybe Foo
parseFoo line = let bar = parseBar line
baz = parseBaz line
in parseFoo' bar baz
parseFoo' :: Maybe Bar -> Maybe Baz -> Maybe Foo
parseFoo' (Just bar) (Just baz) = Just Foo { bar=bar baz=baz }
parseFoo' _ _ = Nothing
This gets the job done, but defining the extra function just to do the pattern matching seems clumsy. Is there a cleaner way to either "unbox" the Maybe Bar and Maybe Baz or return Nothing? Am I going about this all wrong? (I'm still working my way up to monads. I just really wanted to actually write something now that I've made it to Hello, World nine chapters in.)