This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Parts of ExpressionsExpressions as Trees

2.1.5 Manipulating Expressions like Lists

You can use most of the list operations discussed in Section 1.8 on any kind of Mathematica expression. By using these operations, you can manipulate the structure of expressions in many ways.

Here is an expression that corresponds to a sum of terms.

In[1]:= t = 1 + x + x^2 + y^2


Take[t, 2] takes the first two elements from t, just as if t were a list.

In[2]:= Take[t, 2]


Length gives the number of elements in t.

In[3]:= Length[t]


You can use FreeQ[expr, form] to test whether form appears nowhere in expr.

In[4]:= FreeQ[t, x]


This gives a list of the positions at which x appears in t.

In[5]:= Position[t, x]


You should remember that all functions which manipulate the structure of expressions act on the internal forms of these expressions. You can see these forms using FullForm[expr]. They may not be what you would expect from the printed versions of the expressions.

Here is a function with four arguments.

In[6]:= f[a, b, c, d]


You can add an argument using Append.

In[7]:= Append[%, e]


This reverses the arguments.

In[8]:= Reverse[%]


There are a few extra functions that can be used with expressions, as discussed in Section 2.2.10.

Parts of ExpressionsExpressions as Trees