MATHEMATICA TUTORIAL

Manipulating Expressions like Lists

You can use most of the list operations discussed in "Lists" 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]:=
Click for copyable input
Out[1]=
Take[t, 2] takes the first two elements from , just as if were a list.
In[2]:=
Click for copyable input
Out[2]=
Length gives the number of elements in .
In[3]:=
Click for copyable input
Out[3]=
You can use FreeQ[expr, form] to test whether form appears nowhere in expr.
In[4]:=
Click for copyable input
Out[4]=
This gives a list of the positions at which appears in .
In[5]:=
Click for copyable input
Out[5]=

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]:=
Click for copyable input
Out[6]=
You can add an argument using Append.
In[7]:=
Click for copyable input
Out[7]=
This reverses the arguments.
In[8]:=
Click for copyable input
Out[8]=

There are a few extra functions that can be used with expressions, as discussed in "Structural Operations".

New to Mathematica? Find your learning path »
Have a question? Ask support »