Effective Haskell: list-comprehension version of partyBudget has an error if food is duplicated (page 90)

(I may be overthinking corner cases for a little example program)

If you pass in a list of guests with a duplicated food, like [(“Alice”, “hamburger”), (“Bob”, “ice cream”), (“Chris”, “ice cream”)], and a willEat where both Bob and Chris will eat ice cream, then the total number of ice creams bought is 4, not 2, because there are two entries for “ice cream” in the food list.

A way to fix it would be to change the line of the program to:
food ← nub $ map snd guests

1 Like