import Data.Char
type Games = (String, String, Int)
test :: [Games]
test =
[
("Minecraft","mojang",100),
("GTA V","rockstar",500),
("Portal","valve",200),
("GTA IV","rockstar",100)
]
-- give the total number of sales for a studio
studioSales :: String -> [Games] -> [Int]
studioSales studioName [] = []
studioSales studioName ((name,studio,quantitySold):xs)
| studioName == studio = quantitySold: studioSales studioName xs
| otherwise = studioSales studioName xs
當調用函數「studioSales」rockstar「test」時,返回值爲「[500,100]」。如何從這個函數輸出一個[Int]?
我該如何修改這個,所以當調用「studioSales」rockstar「test」時,返回的值是「[600]」,其中兩個Int是相加的。
另外我怎麼能加起來所有的銷售?所以一個函數會返回所有加起來的整數?
謝謝,很好地解釋。很好的幫助。 –
那麼foldl基本上減少了名單? –
是的,'foldl'和'foldr'都用一些指定的操作來減少列表,這是在這種情況下的總和。看看這個主題的更多信息:http://www.haskell.org/haskellwiki/Fold –