【发布时间】:2016-10-04 23:23:26
【问题描述】:
大家晚上好,我是haskell的新手。我正在尝试总结读取字符串 Unicode 值的列表并将它们存储在列表中,然后将整数相加。
getLetterUnicodeValue :: Char -> Int
getLetterUnicodeValue l = (ord l) - 64
unicodeValueList :: String -> [Int]
unicodeValueList x = getLetterUnicodeValue (head x) : unicodeValueList (tail x)
total :: [Int] -> Int
total [] = 0
total x = (head x) + (total (tail x))
当字符串到达最后一个字符并且求和函数无法成功执行时,我得到了空列表的错误。 unicodeValueList函数结束时有什么方法可以停止它。
*** Exception: Prelude.head: empty list
【问题讨论】:
-
是的,你已经为
total做了它...