Question: Standard ML code Write a function listDiff that takes a single argument (a 2-tuple) where both elements of the tuple are lists. The function should
Standard ML code
Write a function listDiff that takes a single argument (a 2-tuple) where both elements of the tuple are lists. The function should return the difference of the first list with respect to the second. You should treat the lists as bags (i.e., they can have duplicates). If an element appears in both lists and if the number of duplicate copies of the element is bigger in the first list, then this element should appear in the result as many times as the difference of the number of occurrences in the input lists. Your function should have type (''a list * ''a list) -> ''a list. The duplicates should not be eliminated in the result. The elements in the resulting list may have arbitrary order. (Hint: You can make use of countInList function.)
Examples:
> listDiff (["a","b","c"],["b"])
["a", "c"]
> listDiff ([1,2,3],[1,1,2])
[3]
> listDiff ([1,2,2,3,3,3],[1,1,2,3])
[2,3,3]
> listDiff ([[2,3],[1,2],[2,3]],[[1],[2,3]])
[[2,3],[1,2]]
> listDiff ([1,2,3],[])
[1,2,3]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
