6.1 How to find all exponents of list of expressions?
Given \(\{f^2, g^3, k^p, h\}\) write code to return \(\{2,3,p,1\}\) as the exponents.
| Mathematica
ClearAll[f, g, k, p, h];
lst = {f^2, g^3, k^p, h};
Cases[lst, x_^n_. :> n]
|
{2, 3, p, 1}
|
| Maple
unassign('f,g,k,p,h');
foo:=proc(expr)
if nops(expr)=1 then
return 1;
elif hastype(expr,`^`) then
return( op(2,expr) );
fi;
return(NULL);
end proc:
expr:=[f^2, g^3, k^p, h]:
map(foo,expr)
|
[2, 3, p, 1]
|