我有以下数据类型,我想创建一个Sport实例,但我不知道如何从数据类型Sport调用类型Football。
`data Football a = Football
{ players :: Players a
, Stadium :: Stadium a
}
data Tennis a = Tennis
{ players1 :: Players a
, Stadium1 :: Stadium a
}
data Sport a = SF Football a | ST Tennis a
当我这样做时:
Instance Show a => Show (Sport a) where
show Football{..} = "<== Football ==>"
show Tennis{..} = "<== Tennis ==>"
我收到一条错误消息:无法匹配预期类型'Sport a' 实际类型'足球t6' 我尝试了其他一些方法并检查了其他一些例子,但我可以弄明白该怎么做...有人有想法吗?
提前致谢:)
答案 0 :(得分:4)
你需要括号
data Sport a = SF (Football a) | ST (Tennis a)
然后你需要对运动进行模式匹配,而不是其他类型:
instance Show a => Show (Sport a) where
show (SF Football{..}) = "<== Football ==>"
show (ST Tennis{..}) = "<== Tennis ==>"