Created
June 13, 2018 06:19
-
-
Save bas080/44788d261d4fd55e262cb8dfe09b6d40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Char (digitToInt, isDigit) | |
| type Position = Int | |
| safeAsInt :: String -> Either (Position, Char) Int | |
| safeAsInt [] = Right 0 | |
| safeAsInt ('-':xs) = fmap negate (safeAsInt xs) | |
| safeAsInt (c:xs) | |
| | isDigit c = fmap (+ (digitToInt c * (10 ^ length xs))) (safeAsInt xs) | |
| | otherwise = Left (length xs, c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment