This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Functional Operations  /

2.2.5 Pure Functions


Pure functions.

When you use functional operations such as Nest and Map, you always have to specify a function to apply. In all the examples above, we have used the "name" of a function to specify the function. Pure functions allow you to give functions which can be applied to arguments, without having to define explicit names for the functions.

  • This defines a function h.
  • In[1]:= h[x_] := f[x] + g[x]

  • Having defined h, you can now use its name in Map.
  • In[2]:= Map[h, {a, b, c}]

    Out[2]=

  • Here is a way to get the same result using a pure function.
  • In[3]:= Map[ f[#] + g[#] &, {a, b, c} ]

    Out[3]=

    There are several equivalent ways to write pure functions in Mathematica. The idea in all cases is to construct an object which, when supplied with appropriate arguments, computes a particular function. Thus, for example, if fun is a pure function, then fun[a] evaluates the function with argument a.

  • Here is a pure function which represents the operation of squaring.
  • In[4]:= Function[x, x^2]

    Out[4]=

  • Supplying the argument n to the pure function yields the square of n.
  • In[5]:= %[n]

    Out[5]=

    You can use a pure function wherever you would usually give the name of a function.

  • You can use a pure function in Map.
  • In[6]:= Map[ Function[x, x^2], a + b + c ]

    Out[6]=

  • Or in Nest.
  • In[7]:= Nest[ Function[q, 1/(1+q)], x, 3 ]

    Out[7]=

  • This sets up a pure function with two arguments and then applies the function to the arguments a and b.
  • In[8]:= Function[{x, y}, x^2 + y^3] [a, b]

    Out[8]=




    If you are going to use a particular function repeatedly, then you can define the function using f[x_]:=body, and refer to the function by its name f. On the other hand, if you only intend to use a function once, you will probably find it better to give the function in pure function form, without ever naming it.
    If you are familiar with formal logic or the LISP programming language, you will recognize Mathematica pure functions as being like


    expressions or anonymous functions. Pure functions are also close to the pure mathematical notion of operators.


    Short forms for pure functions.




    Just as the name of a function is irrelevant if you do not intend to refer to the function again, so also the names of arguments in a pure function are irrelevant. Mathematica allows you to avoid using explicit names for the arguments of pure functions, and instead to specify the arguments by giving "slot numbers" #n. In a Mathematica pure function, #n stands for the n argument you supply. #


    stands for the first argument.

  • #^2& is a short form for a pure function that squares its argument.
  • In[9]:= Map[ #^2 &, a + b + c ]

    Out[9]=

  • This applies a function that takes the first two elements from each list. By using a pure function, you avoid having to define the function separately.
  • In[10]:= Map[Take[#, 2]&, {{2, 1, 7}, {4, 1, 5}, {3, 1, 2}}]

    Out[10]=

  • Using short forms for pure functions, you can simplify the definition of tonumber given in Section 2.2.2.
  • In[11]:= tonumber[digits_] := Fold[(10 #1 + #2)&, 0, digits]




    When you use short forms for pure functions, it is very important that you do not forget the ampersand. If you leave the ampersand out, Mathematica will not know that the expression you give is to be used as a pure function.
    When you use the ampersand notation for pure functions, you must be careful about the grouping of pieces in your input. As shown in Section A.2.7 the ampersand notation has fairly low precedence, which means that you can type expressions like #1+#2& without parentheses. On the other hand, if you want, for example, to set an option to be a pure function, you need to use parentheses, as in option->(fun&).
    Pure functions in Mathematica can take any number of arguments. You can use ## to stand for all the arguments that are given, and ##n to stand for the n


    and subsequent arguments.

  • ## stands for all arguments.
  • In[12]:= f[##, ##]& [x, y]

    Out[12]=

  • ##2 stands for all arguments except the first one.
  • In[13]:= Apply[f[##2, #1]&, {{a, b, c}, {ap, bp}}, {1}]

    Out[13]=