任何人都知道为什么这会导致错误Non-exhaustive patterns in function getCityPopulation
?
type Name = String
type Coordinates = (Int, Int)
type Pop = Int
type TotalPop = [Pop]
type City = (Name, (Coordinates, TotalPop))
testData :: [City]
testData = [("New York City", ((1,1), [5, 4, 3, 2])),
("Washingotn DC", ((3,3), [3, 2, 1, 1])),
("Los Angeles", ((2,2), [7, 7, 7, 5]))]
getCityPopulation :: [City] -> Name -> Int -> Maybe (Coordinates, TotalPop)
getCityPopulation [] nameIn yearIn = error "Can't be empty"
getCityPopulation [cs] nameIn yearIn
| nameIn == "" = error "Input City name"
| yearIn == 0 || yearIn < 0 = error "invalid year"
| otherwise = lookup nameIn [cs]
如您所见,我尝试添加一个案例,以说明任何参数可能为空或对于查找函数无效。还能是什么?
另外,我知道该yearIn
变量目前是多余的,稍后将与预期的函数用途相关,即获取yearIn
TotalPop 列表的元素。
提前感谢您提供的任何和所有帮助:)