Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure, 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).


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: