This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

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 Mathematica using Map.
This applies f separately to each element in a list.
Click for copyable input
This defines a function which takes the first two elements from a list.
Click for copyable input
You can use Map to apply take2 to each element of a list.
Click for copyable input
Map[f,{a,b,...}]apply f to each element in a list, giving {f[a], f[b], ...}

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.
This applies f to each element in the sum.
Click for copyable input
This applies Sqrt to each argument of g.
Click for copyable input
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.
This defines a 2x2 matrix m.
Click for copyable input
Map applies f to the first level of m, in this case the rows of the matrix.
Click for copyable input
MapAll applies f at all levels in m. If you look carefully at this expression, you will see an f wrapped around every part.
Click for copyable input
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.
This applies f only to the parts of m at level 2.
Click for copyable input
Setting the option Heads->True wraps f around the head of each part, as well as its elements.
Click for copyable input
Map[f,expr] or f/@exprapply f to the first-level parts of expr
MapAll[f,expr] or f//@exprapply 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".
Here is a 2x3 matrix.
Click for copyable input
This applies f to parts {1, 2} and {2, 3}.
Click for copyable input
This gives a list of the positions at which b occurs in mm.
Click for copyable input
You can feed the list of positions you get from Position directly into MapAt.
Click for copyable input
To avoid ambiguity, you must put each part specification in a list, even when it involves only one index.
Click for copyable input
MapAt[f,expr,{part1,part2,...}]apply f to specified parts of expr

Applying a function to specific parts of an expression.

Here is an expression.
Click for copyable input
This is the full form of t.
Click for copyable input
You can use MapAt on any expression. Remember that parts are numbered on the basis of the full forms of expressions.
Click for copyable input
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.

This applies f to each element in a list, giving the index of the element as a second argument to f.
Click for copyable input
This applies f to both levels in a matrix.
Click for copyable input
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,{expr1,expr2,...}]apply f to corresponding elements in each of the expri
MapThread[f,{expr1,expr2,...},lev]apply f to parts of the expri at the specified level

Applying a function to several expressions at once.

This applies f to corresponding pairs of list elements.
Click for copyable input
MapThread works with any number of expressions, so long as they have the same structure.
Click for copyable input
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.

Map constructs a new list in which f has been applied to each element of the list.
Click for copyable input
Scan evaluates the result of applying a function to each element, but does not construct a new expression.
Scan visits the parts of an expression in a depth-first walk, with the leaves visited first.