3 ms·
Let's take that further: you wouldn't want to use your code above either, because it would be impossible to tell why the parser failed. Which is going to be rea
by runeks 2y ago
Let's take that further: you wouldn't want to use your code above either, because it would be impossible to tell why the parser failed. Which is going to be really frustrating once it fails. And if it doesn't fail the first version is fine.
- Tainnor 2y agoSure, you can improve this by e.g. import Data.Either.Combinators -- from the either package strSum :: String -> Either String Int strSum = fmap sum . sequence . fmap tryReadInt . words where tryReadInt w = maybeToRight ("not an integer " ++ w) (readMaybe w) This keeps the bulk of the method the same. Being able to go e.g. from Maybe to Either with only few changes (as long as your code is sufficiently general) is one of the nice things you get from all the Haskell abstraction. You can't really do that if you start with exceptions (unless you're in IO).