0
我已经宣布了以下类型错误类型匹配哈斯克尔
data MonthData = Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec deriving (Eq, Show, Enum, Ord)
type Year = Int
type Month = (MonthData, Year)
type Gap = Int
type Average = Double
type HistoryElem = (Date, Gap, Average)
type History = [ HistoryElem ]
然后,我已经宣布了以下功能
event_tests = [ ((28, (Nov, 2016)), 0, 0.0), ((27, (Nov, 2016)), 0, 0.0) ]
history :: Int -> HistoryElem
history 0 = head(event_tests)
当我尝试加载我的文件,我有以下错误。
ERROR "ass16-1.hs":90 - Type error in explicitly typed binding
*** Term : history
*** Type : Int -> ((Integer,(MonthData,Integer)),Integer,Double)
*** Does not match : Int -> HistoryElem
它似乎并不认为HistoryElem先前已定义的,因为如果我们看carefuly,我们可以看到,
((Integer,(MonthData,Integer)),Integer,Double)
是一样的定义比HistoryElem
想不通我做错了什么。
'Date'定义在哪里? – Alec
当发生类型错误时,将类型签名添加到所有顶级绑定可以大大简化它。大多数Haskellers将其作为最佳实践。事实上,有时候这个错误是在后来发现的,在一个令人惊讶的地方,一切看起来都很好 - 但是真正的错误要早得多,而且没有被发现只是因为编译器推断出了与你期望的类型不同的类型。使用类型注释会使编译器报告错误的位置。 – chi