# Wolfram Language & System 10.0 (2014)|Legacy Documentation

# Applying Functions to Parts of Expressions

If you have a list of elements, it is often important to be able to apply a function separately to each of the elements. You can do this in the Wolfram Language using Map.

In[1]:= |

Out[1]= |

In[2]:= |

In[3]:= |

Out[3]= |

Map[f,{a,b,…}] | apply f to each element in a list, giving |

Applying a function to each element in a list.

What Map[f,expr] effectively does is to "wrap" the function f around each element of the expression expr. You can use Map on any expression, not just a list.

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

Map[f,expr] applies f to the first level of parts in expr. You can use MapAll[f,expr] to apply f to *all* the parts of expr.

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

*all*levels in . If you look carefully at this expression, you will see an wrapped around every part.

In[8]:= |

Out[8]= |

In general, you can use level specifications as described in "Levels in Expressions" to tell Map to which parts of an expression to apply your function.

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

Map[f,expr] or f/@expr | apply f to the first‐level parts of expr |

MapAll[f,expr] or f//@expr | apply f to all parts of expr |

Map[f,expr,lev] | apply f to each part of expr at levels specified by lev |

Ways to apply a function to different parts of expressions.

Level specifications allow you to tell Map to which levels of parts in an expression you want a function applied. With MapAt, however, you can instead give an explicit list of parts where you want a function applied. You specify each part by giving its indices, as discussed in "Parts of Expressions".

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

MapAt[f,expr,{part_{1},part_{2},…}] | apply f to specified parts of expr |

Applying a function to specific parts of an expression.

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]//FullForm= | |

In[18]:= |

Out[18]= |

MapIndexed[f,expr] | apply f to the elements of an expression, giving the part specification of each element as a second argument to f |

MapIndexed[f,expr,lev] | apply f to parts at specified levels, giving the list of indices for each part as a second argument to f |

Applying a function to parts and their indices.

In[19]:= |

Out[19]= |

In[20]:= |

Out[20]= |

Map allows you to apply a function of one argument to parts of an expression. Sometimes, however, you may instead want to apply a function of several arguments to corresponding parts of several different expressions. You can do this using MapThread.

MapThread[f,{expr_{1},expr_{2},…}] | apply f to corresponding elements in each of the |

MapThread[f,{expr_{1},expr_{2},…},lev] | apply f to parts of the at the specified level |

Applying a function to several expressions at once.

In[21]:= |

Out[21]= |

In[22]:= |

Out[22]= |

Functions like Map allow you to create expressions with parts modified. Sometimes you simply want to go through an expression, and apply a particular function to some parts of it, without building a new expression. A typical case is when the function you apply has certain “side effects”, such as making assignments, or generating output.

Scan[f,expr] | evaluate f applied to each element of expr in turn |

Scan[f,expr,lev] | evaluate f applied to parts of expr on levels specified by lev |

Evaluating functions on parts of expressions.

In[23]:= |

Out[23]= |