我正在学习haskell,目前正在尝试从字符串中解析整数和浮点数。
但是,当在“342”或任何没有单个或多个非数字字符的“数字”上尝试我的 readNum 函数时,ghci 会向我报告:
*例外:parse.hs:125:18-46: 模式 (i, (a : as)) 的无可辩驳的模式失败
data Token
= IntTok Int | FloatTok Float | EOF
readNum :: String->(Token, String)
readNum [] = (EOF, [])
readNum xs = let (i, (a:as)) = span isDigit xs --This is line 125
in (case a of
('.') -> let (j, (b:c:bs)) = span isDigit as
in (if ((toLower b) == 'e' && (c == '+' || c == '-' || (isDigit c)))
then (let (k, d) = span isDigit bs in (FloatTok (read (concat [i,[a],j, [b],[c],k])::Float), d))
else (FloatTok (read (concat [i,[a],j])::Float), (b:c:bs)))
_ -> (IntTok (read i::Int), (a:as)))
span isDigit xs
当返回一个空列表作为元组的第二个元素时,是否有更好的方法来处理这种情况?
-谢谢