20 Chapter00.
nb
Apply (@@) is another fundamental function of which you should be aware. Keeping in mind that
every expression in Mathematica consists of a head followed by a list of arguments in brackets,
Apply (@@) simply replaces the head of an expression.
We can make this explicit by using Apply (@@) with two undefined heads. First we define an expres-
sion applyExample.
In[80]:= applyExample = head1@1, 2, 3, 4, 5D
Out[80]= head1@1, 2, 3, 4, 5D
Since head1 is undefined, Mathematica simply echoes the definition. Using Apply (@@), we can
replace the head head1 with a new head, say head2.
In[81]:= head2 üü applyExample
Out[81]= head2@1, 2, 3, 4, 5D
One important use of Apply (@@) is to apply a function to a list. More precisely, given a list and a
function, Apply (@@) can be used to evaluate the function with the elements of the list as arguments.
For example, using Apply (@@) and the Plus function, we can sum the elements of a list.
In[82]:= Plus üü 81, 2, 3, 4, 5<
Out[82]= 15
Using symbols instead of actual numbers and FullForm reveals that, just as before, Apply (@@) is
causing the head of the expression to be replaced, this time List is replaced by Plus.
In[83]:= applyExample2 = 8val1, val2, val3, val4, val5<
Out[83]= 8val1, val2, val3, val4, val5<
In[84]:= FullForm@applyExample2D
Out[84]//FullForm=
List@val1, val2, val3, val4, val5D
In[85]:= Plus üü applyExample2
Out[85]= val1 + val2 + val3 + val4 + val5
In[86]:= FullForm@%D
Out[86]//FullForm=
Plus@val1, val2, val3, val4, val5D
Printing
We end this section with a brief description of the Print function. In most cases, the only information
we need displayed is the final result of some computation. But occasionally we may want to explicitly
cause some information to be displayed. For example, in troubleshooting functions you create, it is
common to have information printed as the function is evaluated so as to track internal variables.
The Print function can be applied to any number of arguments. The result is that the values of the
arguments are displayed together on a single line of output. For example, the following displays the
value of 2 + 3, the string “hello”, and the list L from above.