Introduction to Programming with Mathematica, Third Edition

4.6: Pure functions

4.6 Pure functions

A pure function is a function that does not have a name and that can be used "on the spot"; that is, at the moment it is created. This is often convenient, especially if the function is only going to be used once or as an argument to a higher-order function, such as Map, Fold, or Nest. The built-in function Function is used to create a pure function.

The basic form of a pure function is Function [ x, body] for a pure function with a single variable x (any symbol can be used for the variable), and Function [{ x, y, }, body] for a pure function with more than one variable. The body looks like the right-hand side of a user-defined function definition, with the variables x, y, , where argument names would be.

As an example, the square function we created earlier can be written as a pure function.

    <i class="emphasis">In[1]</i>:= <b class="bold">Function[z, z<sup2</sup>]</b>    <i class="emphasis">Out[1]</i>= Function[z, z<sup2</sup>]

There is also a standard input form that can be used in writing a pure function which is easier to write than the Function notation but can be a bit cryptic to read. The right-hand side of the function definition is rewritten by replacing the variable by the pound symbol ( #) and ending the expression with the ampersand symbol ( &

UNLIMITED FREE ACCESS TO THE WORLD'S BEST IDEAS

SUBMIT
Already a GlobalSpec user? Log in.

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.

Customize Your GlobalSpec Experience

Category: Industrial Valves
Finish!
Privacy Policy

This is embarrasing...

An error occurred while processing the form. Please try again in a few minutes.