# Pure Functions

Function[x,body] | a pure function in which x is replaced by any argument you provide |

Function[{x_{1},x_{2},...},body] | a pure function that takes several arguments |

body& | a pure function in which arguments are specified as or , , , etc. |

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.

In[1]:= |

In[2]:= |

Out[2]= |

In[3]:= |

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 evaluates the function with argument a.

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

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

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

If you are going to use a particular function repeatedly, then you can define the function using , 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.

# | the first variable in a pure function |

#n | the n variable in a pure function |

## | the sequence of all variables in a pure function |

##n | the sequence of variables starting with the n one |

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.

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

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 "Operator Input Forms" the ampersand notation has fairly low precedence, which means that you can type expressions like 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 .

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.

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |