嗨,您能否帮助我了解如何增加元组的最后一个元素。目前我有这个元组列表
items :: [Grocery]
items = [("Water", "Drink Section", 1),
("Squash", "Drink Section", 1),
("Apple", "Fruit Section", 1),
("Plates", "Disposable Section", 1),
("Plates", "Ceramic Section", 1)]
我想做的是每次购买商品时将其增加 1 并输出数据库。目前我有这个
sales:: [database] -> String -> String-> [database]
sales db itemName sectionName = []
sales ((item, section, qty): xs) itemName sectionName
| item == itemName && section== sectionName = [(item, section, qty + 1)]
| otherwise = []
我仍然在增加它并且我卡住了。请帮助我,我还是这门语言的新手。谢谢你!
编辑
现在一切正常,但你如何输出列表的其余部分?我试过recordSale xs trackArtist trackTitle
了,但是当我测试它时,我增加的旧记录也会被打印而不是被修改?假设我增加了苹果它将打印的是这个
[("Apple", "Fruit Section", 2),("Water", "Drink Section", 1),("Squash", "Drink Section", 1), ("Apple", "Fruit Section", 1)]
它复制记录而不是仅仅添加 1