1.8 pattern example 3

Given list \(3,4,x,x^2,x^3\), return list of exponents of \(x\), other than \(1\).

In Maple

L:=[3,4,x,x^2,x^3]; 
f:=proc(X::anything,x::symbol) 
   local la,n; 
   if patmatch(X,x^n::nonunit(anything),'la') then 
       eval(n,la); 
    else 
       NULL; 
    fi; 
end proc; 
 
map(X->f(X,x),L) 
 
   [2, 3]
 

Another option is to use inlined if, like this

restart; 
L:=[3,4,x,x^2,x^3]; 
map(proc(X) local la,n; 
 
    if  patmatch(X,x^n::nonunit(anything),'la') then 
        eval(n,la); 
    else 
        NULL; 
    fi; 
    end proc, L); 
 
 
    [2, 3]
 

In Mathematica

Cases[{3, 4, x, x^2, x^3}, x^n_ -> n] 
         {2, 3}