33 Expressions and Their Structure
Symbolic expressions are a very general way to represent structure, potentially with meaning associated with that structure. f[x,y] is a simple example of a symbolic expression. On its own, this symbolic expression doesnt have any particular meaning attached, and if you type it into the Wolfram Language, itll just come back unchanged.
f[x, y] is a symbolic expression with no particular meaning attached:
 In[1]:=
 Out[1]=
The symbolic expression List[x, y, z] displays as {x, y, z}:
 In[2]:=
 Out[2]=
Symbolic expressions are often nested:
 In[3]:=
 Out[3]=
FullForm shows you the internal form of any symbolic expression.
 In[4]:=
 Out[4]//FullForm=
Graphics[Circle[{0,0}]] is another symbolic expression, that just happens to display as a picture of a circle. FullForm shows its internal structure.
This symbolic expression displays as a circle:
 In[5]:=
 Out[5]=
FullForm shows its underlying symbolic expression structure:
 In[6]:=
 Out[6]//FullForm=
Symbolic expressions often dont just display in special ways, they actually evaluate to give results.
 In[7]:=
 Out[7]=
The elements of the list evaluate, but the list itself stays symbolic:
 In[8]:=
 Out[8]=
Heres the symbolic expression structure of the list:
 In[9]:=
 Out[9]//FullForm=
This is just a symbolic expression that happens to evaluate:
 In[10]:=
 Out[10]=
You could write it like this:
 In[11]:=
 Out[11]=
Things like x, y, f, Plus, Graphics and Table are all symbols. Every symbol has a unique name. Sometimes itll also have a meaning attached. Sometimes itll be associated with evaluation. Sometimes itll just be part of defining a structure that other functions can use. But it doesnt have to have any of those things; it just has to have a name.
In the Wolfram Language, x can just be x, without having to evaluate to anything:
 In[12]:=
 Out[12]=
x does not evaluate, but the addition is still done, here according to the laws of algebra:
 In[13]:=
 Out[13]=
Given symbols like x, y and f, one can build up an infinite number of expressions from them. Theres f[x], and f[y], and f[x,y]. Then theres f[f[x]] or f[x,f[x,y]], or, for that matter, x[x][y,f[x]] or whatever.
An expression shown in tree form:
 In[14]:=
 Out[14]//TreeForm=
Heres a graphics expression shown in tree form:
 In[15]:=
 Out[15]//TreeForm=
This is equivalent to {x, y, z}[[2]], which extracts the second element in a list:
 In[16]:=
 Out[16]=
Extracting parts works exactly the same way for this expression:
 In[17]:=
 Out[17]=
This extracts the circle from the graphics:
 In[18]:=
 Out[18]=
This goes on and extracts the coordinates of its center:
 In[19]:=
 Out[19]=
This works exactly the same:
 In[20]:=
 Out[20]=
The head of a list is List:
 In[21]:=
 Out[21]=
Every part of an expression has a head, even its atoms.
The head of an integer is Integer:
 In[22]:=
 Out[22]=
The head of an approximate real number is Real:
 In[23]:=
 Out[23]=
The head of a string is String:
 In[24]:=
 Out[24]=
 In[25]:=
 Out[25]=
In patterns, you can ask to match expressions with particular heads. represents any integer, any string and so on.
_Integer is a pattern that matches only objects with head Integer:
 In[26]:=
 Out[26]=
Named patterns can have specified heads too:
 In[27]:=
 Out[27]=
 In[28]:=
 Out[28]//FullForm=
When you apply the pure function, it appears as a head:
 In[29]:=
 Out[29]=
Select appears as a head here:
 In[30]:=
 Out[30]=
Both Cases and Select appear as heads here:
 In[31]:=
 Out[31]=
Length does not care what the head of an expression is; it just counts arguments:
 In[32]:=
 Out[32]=
/@ does not care about the head of an expression either; it just applies a function to the arguments:
 In[33]:=
 Out[33]=
Since there are lots of functions that generate lists, its often convenient to build up structures as lists even if eventually one needs to replace the lists with other functions.
@@ effectively replaces the head of the list with f:
 In[34]:=
 Out[34]=
This yields Plus[1, 1, 1, 1], which then evaluates:
 In[35]:=
 Out[35]=
This turns a list into a rule:
 In[36]:=
 Out[36]=
Heres a simpler alternative, without the explicit pure function:
 In[37]:=
 Out[37]=
A surprisingly common situation is to have a list of lists, and to want to replace the inner lists with some function. Its possible to do this with @@ and /@. But @@@ provides a convenient direct way to do it.
Replace the inner lists with f:
 In[38]:=
 Out[38]=
 In[39]:=
 Out[39]=
Heres an example of how @@@ can help construct a graph from a list of pairs.
This generates a list of pairs of characters:
 In[40]:=
 Out[40]=
 In[41]:=
 Out[41]=
 In[42]:=
 Out[42]=
33.1Find the head of the output from ListPlot»
Expected output:
 Out[]=
33.2Use @@ to compute the result of multiplying together integers up to 100. »
Expected output:
 Out[]=
33.3Use @@@ and Tuples to generate {f[a, a], f[a, b], f[b, a], f[b, b]}»
Expected output:
 Out[]=
33.4Make a list of tree forms for the results of 4 successive applications of starting from x»
Expected output:
 Out[]=
33.5Find the unique cases where i^2/(j^2+1) is an integer, with i and j going up to 20. »
Expected output:
 Out[]=
33.6Create a graph that connects successive pairs of numbers in Table[Mod[n^2+n, 100], {n, 100}]»
Expected output:
 Out[]=
33.7Generate a graph showing which word can follow which in the first 200 words of the Wikipedia article on computers. »
Sample expected output:
 Out[]=
33.8Find a simpler form for f@@#&/@{{1, 2}, {7, 2}, {5, 4}}»
Sample expected output:
 Out[]=
How are @@ and @@@ interpreted?
f@@expr is Apply[f, expr]. f@@@expr is Apply[f, expr, {1}]. Theyre usually just read as double at and triple at.
At a structural level, yes. When there are variables with values assigned (see Section 38), though, they can behave more like directed graphs. And of course one can use Graph to represent any graph as an expression in the Wolfram Language.