2
$\begingroup$

I want to define a function f such that $f(x[1,2]^{-1/3})=\{1,2\}, f(x[1,2])=\{1,2\}, f(x[1])=\{1\}, f(x[1]^{1/3}) =\{1\}$, f(x)=empty set. How could I do this in Mathematica? Thank you very much.

$\endgroup$
3
$\begingroup$

I don't know how versatile this is but it works on your examples,

f[a_[b__]^c_] := {b};
f[a_[b__]] := {b}
f[a_] := {}

f /@ {x[1, 2]^(-1/3), x[1, 2], x[1], x[1]^(1/3), x}
(* {{1, 2}, {1, 2}, {1}, {1}, {}} *)
$\endgroup$
1
$\begingroup$

Bit more general.

f[_?AtomQ] := {}
f[_[x___]] := {x}
f[_[_[x___], ___]] := {x}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.