       /** deletion of elements in expressions **/

/*K: list; remove; delete; del; XAllbut; allbut; lists; complement; deletion */
/*A: John Gottschalk */
/*S: University of Western Australia */
/*D: May 1985 */
/*P: XMask is loaded */

If[~P[_XLoadonce[Loaded]],<XLoadonce]
_Mask[Init] :: Loadonce[XMask]

/*: DelE[$form,$expr,($lev:Inf),($n:Inf)]
	yields $expr with $n occurences of $form deleted. 
	If $form is a list all occurences of the elements of $form are
	removed. This should replace Allbut in the external file 
	XAllbut. */

DelE_:Tier

DelE[$form,$$o]                         :: Del[$form,$$o]
DelE[$form_=Listp[$form],$expr,$lev,$n] :: (Lcl[%t]; %t : $expr; \
                  Do[%#i,1,Len[$form],,%t :  Del[$form[%#i],%t,$lev,$n]]; %t)
DelE[$form_=Listp[$form],$expr,,$n]     :: DelE[$form,$expr,Inf,$n]
DelE[$form_=Listp[$form],$expr,$lev]    :: DelE[$form,$expr,$lev,Inf]
DelE[$form_=Listp[$form],$expr]         :: DelE[$form,$expr,Inf,Inf]

/*: DelL[$temp,$list,($n:Inf)]
    deletes $n elements of $list that do not yield 1 when $temp is applied. */
_DelL[Smp] : 0
DelL_:Tier

DelL[$temp,$list_=Contp[$list],$n] :: \
     DelE[Proj[Mask[Map[$temp,$list],$list],{1}],$list,,$n]
DelL[$temp,$list_=Contp[$list]] :: DelL[$temp,$list_=Contp[$list],Inf] 

/*: RemL[$a,$list] 
      removes each element of list $a from $list. Only one copy
      of each element is deleted. */
RemL[$a_=Contp[$a],$list_=Contp[$list]] :: (Lcl[%t]; %t : $list; \
       Do[%#i,1,Len[$a],,%t : Rem[$a[%#i],%t]])

/*: Rem[$a,$list] 
      removes one element of $list that equals $a.*/
Rem[$a,$list_=Contp[$list]] :: \
       (Lcl[%in]; Cat[Ar[Len[$list],$list,,Sel[%in,1,$1=$a,%in : 1; 0]]])

_XDelete[Loaded] : 1

/*E:
#I[1]::  <XDelete

#I[2]::  t:(a+a c+b^2)^(a+b c)

		     2 a + b c
#O[2]:   (a + a c + b )

#I[3]::  DelE[a,t,,1]

		 2 a + b c
#O[3]:   (a c + b )

#I[4]::  DelE[{a,c},t]

	  2b
#O[4]:   b

#I[5]::  DelE[{a,c},t,,1]

	       2 a + b c
#O[5]:   (a + b )

#I[6]::  x : {1,1,2,4,5/2}

#O[6]:   {1,1,2,4,5/2}

#I[7]::  DelE[$x_=~Intp[$x],x,,1]

#I[8]::  DelL[~Intp[$x],x,1]

#O[8]:   {1,1,2,4}
*/
