The need to create intersections, differences, and unions, comes up when we need to know values in common, exclusive, or unique to two or more data sets. For this month, create three functions:
- intersection(aList, bList) – Returns a list of values contained in both aList and bList.
- difference(aList, bList) – Returns two lists. The first contains the values in aList that are not in bList, the second contains the values in bList that are not in aList.
- union(aList, bList) – Returns a list of all values in aList and bList, removing any duplicates.
For example, if:
aList = {“a”,”b”,”c”,”d”,”e”,”e”}
bList = {“a”,”a”,”c”,”f”,”g”}
then,
intersection(aList,bList) => {“a”,”c”}
difference(aList,bList) => {{“b”,”d”,”e”},{“f”,”g”}}
union(aList,bList) => {“a”,”b”,”c”,”d”,”e”,”f”,”g”}
For extra credit, extend intersection and union to take a list of three or more lists.