【发布时间】:2022-10-13 23:00:16
【问题描述】:
所以我有这个代码片段:where exponent = read (tail (dropWhile (/= '^') (head xs))) :: Int 但tail 中的列表可能为空,这意味着在空列表中查找尾部时会出错。有没有办法做类似的事情:if error: exponent = 1?
如果它有任何帮助,这是完整的功能:)
internalRepresentation :: [String] -> [(Int,Char ,Int)]
internalRepresentation xs
|null xs = []
|all isDigit (head xs) = (read (head xs), ' ', 0) : internalRepresentation (tail xs)
|head (head xs) == '-' = (-read (takeWhile isDigit (pos_mon)) :: Int, head (dropWhile isDigit (pos_mon)), exponent) : internalRepresentation (drop 1 xs)
|otherwise = (read (takeWhile isDigit (head xs)) :: Int, head (dropWhile isDigit (head xs)), exponent) : internalRepresentation (drop 1 xs)
where pos_mon = tail (head xs)
exponent = read (tail (dropWhile (/= '^') (head xs))) :: Int
谢谢你的时间!
【问题讨论】:
标签: haskell