Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Last active August 12, 2018 20:04
Show Gist options
  • Save mvaldesdeleon/4d355cd27dcd3f6aab0a972a17adbeee to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/4d355cd27dcd3f6aab0a972a17adbeee to your computer and use it in GitHub Desktop.
asInt :: String -> Either String Int
asInt ('-':xs) = negate <$> asInt xs
asInt xs =
foldl
(\acc x -> acc >>= addNextDigit x)
(Right 0)
xs
where addNextDigit x acc = if isDigit x
then Right $ acc * 10 + digitToInt x
else Left $ "non-digit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment