多态数据,假设我们有一个名为东西定义的数据:匹配在Haskell
data Stuff = Stuff1 Int
| Stuff2 Int
| Stuff3 Int
sumStuff :: [Stuff] -> Int
sumStuff [] = 0
sumStuff ((Stuff1 x):xs) = x + sumStuff xs
sumStuff ((Stuff2 x):xs) = x + sumStuff xs
sumStuff ((Stuff3 x):xs) = x + sumStuff xs
sumStuff' :: [Stuff] -> Int
sumStuff' [] = 0
sumStuff' ((_ x):xs) = x+sumStuff xs
如何我可以匹配所有类型的无图案像在sumStuff”错误的定义相匹配?
预先感谢您!
这是比其他答案更懒。如果每个'Stuff'都含有'exactStuff',则在使用'exactStuff'之前,不需要确定它是否为'Stuff1','Stuff2'或'Stuff3'。 – Cirdec 2015-02-06 22:58:27