Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Variables[] in general expressions

  • To: mathgroup at
  • Subject: [mg20053] Re: [mg20037] Re: [mg20024] Variables[] in general expressions
  • From: "Andrzej Kozlowski" <andrzej at>
  • Date: Wed, 29 Sep 1999 03:33:19 -0400
  • Sender: owner-wri-mathgroup at

I just realized the correct answer to this question is a bit controversial.
As a mathematician I would think that a function can be a variable just as
well as anything else so I do not think my previous answer is really right.
For example, what are the variables in f[x,g[y,z]]? I would say they should
be {f,x,g,y,z} ( though I suppose some would only consider {x,y,z} to be the
right answer).

Anyway, a version of my code that gives what I now think is the right answer

variables[expr_] :=
Union[Cases[expr, _Symbol?(FreeQ[Attributes[#], Protected] &),

This gives

variables[f[x, g[y, z]]]
{f, x, g, y, z}
Andrzej Kozlowski
Toyama International University

>From: "Andrzej Kozlowski" <andrzej at>
>To: mathgroup at
>Subject: [mg20053] [mg20037] Re: [mg20024] Variables[] in general expressions
>Date: Sat, 25 Sep 1999 18:46:04 -0400

> Here is one definition that seems to work:
> In[12]:=
> variables[expr_] :=
>   Cases[expr, _Symbol?(FreeQ[Attributes[#], Protected] &), Infinity]
> The idea is that a variable should be something that is a Symbol but does
> not have the attribute Protected, which all built-in symbols like Plus,
> Power etc have.
> It works in your example:
> In[13]:=
> variables[y + Exp[x]]
> Out[13]=
> {x, y}
> I think it will always work unless WRI has forgotten to give the attribute
> protected to some built-in symbol.
> --
> Andrzej Kozlowski
> Toyama International University
> ----------
>>From: Ken Rice <kmr1004 at>
To: mathgroup at
> To: mathgroup at
>>To: mathgroup at
>>Subject: [mg20053] [mg20037] [mg20024] Variables[] in general expressions
>>Date: Sat, Sep 25, 1999, 3:40 PM
>> Hi there,
>> Is there any way of extracting a list of variables from a general
>> expression, not just a polynomial? I want something which will do,
>> e.g.
>> In AllVariables[y + Exp[x]]
>> Out {y,x}
>> Apologies if I'm just being dim.
>> Ken Rice
>> _____________________________________________________
>> Kenneth Rice
>> Churchill College
>> Cambridge
>> CB3 0DS, UK
>> E-mail: kmr1004 at

  • Prev by Date: Calculus of variations
  • Next by Date: Mathematica 4.0.1 Front End crashes my Mac
  • Previous by thread: Re: Variables[] in general expressions
  • Next by thread: Keyboard trouble in Mathematica 3.0