这段代码:
import Data.Char (digitToInt)
myInt :: String -> Int
myInt [] = error "bad input: empty string"
myInt (x:xs)
| x == '-' = -1 * myInt xs
| otherwise = foldl convert 0 (x:xs)
where convert acc x
| x `elem` ['0'..'9'] = 10 * acc + digitToInt x
| otherwise = error ("bad input: not an int - " ++ [x])
Run Code Online (Sandbox Code Playgroud)
失败:
import Data.Char (digitToInt)
myInt :: String -> Int
myInt [] = error "bad input: empty string" …Run Code Online (Sandbox Code Playgroud)