【发布时间】:2021-05-02 10:18:31
【问题描述】:
如果有人可以帮助解决此错误,将不胜感激。代码是:
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 -> Int
getCityPopulation cs nameIn yearIn = head ([ z !! (yearIn - 1) | (x,z) <- lookup nameIn cs])
预期行为:
getCityPopulation testData "New York City" 2
>>> 4
实际行为:
• Couldn't match expected type ‘[(a0, [Int])]’
with actual type ‘Maybe (Coordinates, TotalPop)’
• In the expression: lookup nameIn cs
In a stmt of a list comprehension: (x, z) <- lookup nameIn cs
In the first argument of ‘head’, namely
‘([z !! (yearIn - 1) | (x, z) <- lookup nameIn cs])’
|
55 | getCityPopulation cs nameIn yearIn = head ([ z !! (yearIn - 1) | (x,z) <- lookup nameIn cs])
| ^^^^^^^^^^^^^^^^
【问题讨论】:
-
查找返回
Maybe City,因为这可能会失败。 -
@WillemVanOnsem 获得预期输出的解决方案是什么?
-
如果您输入
getCityPopulation testData "New York City" 2000,预期的输出是什么?getCityPopulation testData "Boston" 2呢? -
@n.'pronouns'm。它应该说类似
Data unavailable, check inputs again
标签: function haskell lookup type-mismatch algebraic-data-types