kill(all)$

maxima:programming intersection,union ,version 1.0
peter.vlasschaert@gmail.com,17/06/2022

ref :https://maxima.sourceforge.io/docs/manual/maxima_21.html

use:member,cons,endcons,setify,listify,first,second,rest,last,flatten
      union(,),intersection(,),delete(,),firstn(,),lastn(,),setdifference(,).

→ given : two list use :[]

la:[d,f,g,a,h]$
lb:[g,h,m,n,a]$

la[1];

→ given :two set use :{}

las:setify(la);
lbs:setify(lb);

rem : list operations & set operations
relation between : (full)_listify⇔(full)setify

---------------------------------------------------------------------

→ properties

number:

[first(la),second(la),fourth(la)];

rest:

[rest(la),rest(lb)];

cons,endcons:

p1:cons(x,la);
p2:endcons(x,lb);

member:→{true,false}

[member(x,p1),member(f,p2)];

[not member(x,p1),not member(f,p2)];

delete :

delete (y, [w, x, y, z, z, y,y, x, w]);

v1:delete(la,lb);

v2:delete(lb,la);

Function: firstn (expr, count)
Function: lastn (expr, count)

unique ([1, %pi, a + b, 2, 1, %e, %pi, a + b, [1]]);

rem : all w delete from the list

→ intersection
------------------

method 1:

lc:[]$

for i:1 thru length(la) do(
if member(la[i],lb)  then lc:endcons(la[i],lc))$

lc:sort(lc);

kill(lc)$

lc;

method 2:

wb1:listify(setdifference (las, lbs));
wb2:listify(setdifference (lbs, las));
wb3:setify(flatten(cons(wb1,wb2)));
wb4:setify(flatten(cons(la,lb)));
wb5:sort(listify(setdifference (wb4,wb3)));

rem : setdifference(set1,set2)

method 3:? recursion

method 4:intersection need a "set".

wb:sort(listify(intersection(las,lbs)));

→ union

method 1:

lc1:[]$

for i:1 thru length(la) do(
if member(la[i],lb) = false then lc1:endcons(la[i],lc1))$
lc1:flatten(cons(lc1,lb))$

la;
lb;

lc1:sort(lc1);

method 2:

pa1:flatten(cons(la,lb));

→ list to set.

pa2:setify(pa1);

→ set to list.

pa3:sort(listify(pa2));

method 3:recursion

w2(l1,l2):=if l1=[] then l2 else
                 if member(first(l1),w2(rest(l1),l2)) then
                   w2(rest(l1),l2)
                 else
                   cons(first(l1),w2(rest(l1),l2))$

→ optimize(code).

grind(w2)$

wba:sort(w2(la,lb));

method 4: union need a "set"

wba1:sort(listify(union(las,lbs)));

rem :https://maxima.sourceforge.io/docs/manual
/maxima_378.html#Category_003a-Sets