## 2.2.3 Applying Functions to Lists and Other Expressions

In an expression like f[{a, b, c}] you are giving a list as the argument to a function. Often you need instead to apply a function directly to the elements of a list, rather than to the list as a whole. You can do this in Mathematica using Apply.

This makes each element of the list an argument of the function f.
 In[1]:=  Apply[f, {a, b, c}]
 Out[1]=
This gives Plus[a, b, c] which yields the sum of the elements in the list.
 In[2]:=  Apply[Plus, {a, b, c}]
 Out[2]=
Here is the definition of the statistical mean, written using Apply.
 In[3]:=  mean[list_] := Apply[Plus, list] / Length[list]

 Apply[f, {a, b, ... }] apply f to a list, giving f[a, b, ... ] Apply[f, expr] or f @@ expr apply f to the top level of an expression Apply[f, expr, {1}] or f @@@ expr apply f at the first level in an expression Apply[f, expr, lev] apply f at the specified levels in an expression

Applying functions to lists and other expressions.
What Apply does in general is to replace the head of an expression with the function you specify. Here it replaces Plus by List.
 In[4]:=  Apply[List, a + b + c]
 Out[4]=
Here is a matrix.
 In[5]:=  m = {{a, b, c}, {b, c, d}}
 Out[5]=
Using Apply without an explicit level specification replaces the top-level list with f.
 In[6]:=  Apply[f, m]
 Out[6]=
This applies f only to parts of m at level 1.
 In[7]:=  Apply[f, m, {1}]
 Out[7]=
This applies f at levels 0 through 1.
 In[8]:=  Apply[f, m, {0, 1}]
 Out[8]=

THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.