Hi could you please help me on how to increment the last element of a tuple.
Currently i have this list of tuples
items :: [Grocery]
items = [("Water", "Drink Section", 1),
("Squash", "Drink Section", 1),
("Apple", "Fruit Section", 1),
("Plates", "Disposable Section", 1),
("Plates", "Ceramic Section", 1)]
and what i wanted to do is increment it by 1 every time the item is bought and output the database. currently i have this
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 = []
im still in the bit of incrementing it and im stuck. please help me i’m still a newbie on this language. thank you!
Edit
its all working now but how do you output the rest of the list? i tried recordSale xs trackArtist trackTitle but when i test it the old record that i incremented gets printed as well instead of getting modified? lets say that i incremented apple what it’ll print is this
[("Apple", "Fruit Section", 2),("Water", "Drink Section", 1),("Squash", "Drink Section", 1), ("Apple", "Fruit Section", 1)]
it duplicates the record instead of just adding 1
That’s not bad, but lets pretend you’re trying to increment “Squash”, the second element down in your example list of items. What does
salesdo? It checks if the first item in the list, Water, equals Squash. Since water doesn’t equal squash it hits theotherwisecase and returns[].So all of that seems right up until we got a
[]back – lets change the code in otherwise. Obviously we don’t want to throw away the entire list, that would be stupid. You should rewrite it to keep the item that was just compared and concatenate it on to the result ofsalesapplied to the rest of the list (xs).After you get that
otherwisebranch fixed you’re going to notice the entire list after the item you increment is thrown away – I think that one will pop right out at you once you finish this issue.P.S. Fire the employee that put Squash in the drinking section.