2

こんにちは、タプルの最後の要素をインクリメントする方法を教えてください。現在、私はこのタプルのリストを持っています

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をインクリメントしたとしましょう。出力されるのはこれです

[("Apple", "Fruit Section", 2),("Water", "Drink Section", 1),("Squash", "Drink Section", 1), ("Apple", "Fruit Section", 1)]

1を追加するだけでなく、レコードを複製します

4

1 に答える 1