Skip to content

Instantly share code, notes, and snippets.

@bas080
Created June 13, 2018 06:19
Show Gist options
  • Select an option

  • Save bas080/44788d261d4fd55e262cb8dfe09b6d40 to your computer and use it in GitHub Desktop.

Select an option

Save bas080/44788d261d4fd55e262cb8dfe09b6d40 to your computer and use it in GitHub Desktop.
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