%mathpiper,def="Difference"

Function("Difference",{list1,list2})
[
  Local(l2,index,result);
  l2:=FlatCopy(list2);
  result:=FlatCopy(list1);
  ForEach(item,list1)
  [
    Bind(index,Find(l2,item));
    If(index>0,
      [
        DestructiveDelete(l2,index);
        DestructiveDelete(result,Find(result,item));
      ]
      );
  ];
  result;
];

%/mathpiper



%mathpiper_docs,name="Difference",categories="User Functions;Lists (Operations)"
*CMD Difference --- return the difference of two lists
*STD
*CALL
	Difference(l1, l2)

*PARMS

{l1}, {l2} -- two lists

*DESC

The difference of the lists "l1" and "l2" is determined and
returned. The difference contains all elements that occur in "l1"
but not in "l2". The order of elements in "l1" is preserved. If a
certain expression occurs "n1" times in the first list and "n2"
times in the second list, it will occur "n1-n2" times in the result
if "n1" is greater than "n2" and not at all otherwise.

*E.G.

In> Difference({a,b,c}, {b,c,d});
Result: {a};
In> Difference({a,e,i,o,u}, {f,o,u,r,t,e,e,n});
Result: {a,i};
In> Difference({1,2,2,3,3,3}, {2,2,3,4,4});
Result: {1,3,3};

*SEE Intersection, Union
%/mathpiper_docs