Use an association list as a bag
Construct an empty list
empty --> []Construct a list with a single element
singleton "🍎" --> [ ( "🍎", 1 ) ]Insert an element into the list
insert 1 "🍎" [] --> [("🍎",1)]
insert 2 "🍎" [ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍎",3),("🍇",1)]
insert 1 "🍎" [ ( "🍊", 1 ), ( "🍇", 1 ) ] --> [("🍎",1),("🍊",1),("🍇",1)]Remove an element from the list
remove 1 "🍎" [] --> []
remove 1 "🍎" [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍎",1),("🍇",1)]
remove 1 "🍎" [ ( "🍊", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍇",1)]
remove 1 "🍎" [ ( "🍊", 1 ), ( "🍎", 1 ), ( "🍇", 1 ) ] --> [("🍊",1),("🍇",1)]Combine two lists. The order of the second list gets preserved.
plus [ ( "🍒", 1 ), ( "🍎", 1 ) ] [ ( "🍇", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍊",1),("🍒",1),("🍎",1)]
plus [ ( "🍎", 1 ) ] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",2),("🍊",1)]
plus [ ( "🍎", 1 ), ( "🍒", 1 ) ] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",2),("🍊",1),("🍒",1)]subtracts the first list from the second.
minus [] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍇",1),("🍎",1),("🍊",1)]
minus [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] [] --> []
minus [ ( "🍎", 2 ), ( "🍊", 1 ) ] [ ( "🍇", 2 ), ( "🍎", 2 ), ( "🍊", 2 ) ] --> [("🍇",2),("🍊",1)]Intersect two lists. The order of the second list gets preserved.
intersection [] [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> []
intersection [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] [] --> []
intersection [ ( "🍇", 1 ), ( "🍎", 1 ) ] [ ( "🍎", 1 ), ( "🍊", 1 ) ] --> [("🍎",1)]
intersection [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 3 ) ] [ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ] --> [("🍇",1),("🍎",2),("🍊",1)]Check if the list is empty
isEmpty [] --> True
isEmpty [ ( "🍎", 42 ) ] --> FalseCompute if the element is a member
member "🍎" [] --> False
member "🍎" [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> True
member "🍒" [ ( "🍇", 1 ), ( "🍎", 1 ), ( "🍊", 1 ) ] --> FalseCounts all elements in the list
size [] --> 0
size [ ( "🍇", 1 ), ( "🍎", 2 ), ( "🍊", 3 ) ] --> 6Counts the occurrences of elements in a list.
fromList [] --> []
fromList [ "🍎", "🍎" ] --> [("🍎",2)]
fromList [ "🍇", "🍎", "🍊" ] --> [("🍇",1),("🍎",1),("🍊",1)]
fromList [ "🍇", "🍎", "🍊", "🍎", "🍊", "🍎" ] --> [("🍇",1),("🍎",3),("🍊",2)]Convert into a list with repeating elements
toList [] --> []
toList [ ( "🍎", 2 ) ] --> ["🍎","🍎"]
toList [ ( "🍊", 1 ), ( "🍎", 2 ), ( "🍇", 1 ) ] --> ["🍊","🍎","🍎","🍇"]
Count the amounts of an element in the list