# Except[] with levelspec

I am trying to determine all the variables used in list. For This I use:

DeleteDuplicates[Cases[l1,_Symbol,-1]]


This is great except that \Pi etc. are symbols too. I want to add Except to this Cases command to discard numeric symbols but I'm not sure where to add it.

I tried different combinations, but I either get errors, or it will interpret it differently to what I intended.

Alternatively, is there a better way of achieving this?

• Something like DeleteDuplicates[Cases[l1 , _?(Not[NumericQ[#]] &), {-1}]]? Observe the curly brackets around the level specification. – Fred Simons Jan 31 '16 at 16:59
• What about Variables? – Kuba Jan 31 '16 at 17:17
• @FredSimons In such cases, I really like operators: _?(Not@*NumericQ) – Kuba Jan 31 '16 at 17:20
• @Kuba I agree, with both your comments. – Fred Simons Jan 31 '16 at 17:32
• Thanks. Feel free to add it as answer? – Shb Jan 31 '16 at 17:56

As an example, let us consider the following expression:

expr= Log[ 3 Sin[x] + 2 Exp[Pi+ 4 a b + 1/7]];


This is not a polynomial, so the function Variables cannot be used. On level -1 we have the atoms:

Cases[expr,_, {-1}]

(* {2,E,1/7,4,a,b,\[Pi],3,x} *)


Observe that 1/7 is an atom! We restrict ourselves to symbols:

Cases[expr,_Symbol, {-1}]

(* {E,a,b,\[Pi],x} *)


This is not restrictive enough; we only want the symbols that do not have a value. Using Kuba's advice to use function composition:

Cases[ expr, _Symbol?(Not @*  NumericQ), {-1}]

(* {a,b,x} *)


You can use WolframLanguageData with Alternatives to built the Except.

numConstFunc = Except[Alternatives @@ Symbol /@
WolframLanguageData[
EntityClass[
"WolframLanguageSymbol", {"FunctionalityArea", "NumericConstantFunctions"}],
"Name"], _Symbol];


Then

expr= Log[ 3 Sin[x] + 2 Exp[Pi+ 4 a b + 1/7]];

Cases[expr, numConstFunc, {-1}]
(* {a, b, x} *)


Hope this helps.