# Variables in Pure Functions and Rules

Module and With allow you to give a specific list of symbols whose names you want to treat as local. In some situations, however, you want to automatically treat certain symbol names as local.

For example, if you use a pure function such as Function[{x},x+a], you want to be treated as a "formal parameter", whose specific name is local. The same is true of the that appears in a rule like , or a definition like .

The Wolfram Language uses a uniform scheme to make sure that the names of formal parameters that appear in constructs like pure functions and rules are kept local, and are never confused with global names. The basic idea is to replace formal parameters when necessary by symbols with names of the form . By convention, is never used as a global name.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

In general, the Wolfram Language renames the formal parameters in an object like Function[vars,body] whenever body is modified in any way by the action of another pure function.

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

The Wolfram Language renames formal parameters in pure functions more liberally than is strictly necessary. In principle, renaming could be avoided if the names of the formal parameters in a particular function do not actually conflict with parts of expressions substituted into the body of the pure function. For uniformity, however, the Wolfram Language still renames formal parameters even in such cases.

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]= |

As mentioned in "Pure Functions", pure functions in the Wolfram Language are like expressions in formal logic. The renaming of formal parameters allows Wolfram Language pure functions to reproduce all the semantics of standard expressions faithfully.

Function[{x,…},body] | local parameters |

lhs->rhs and lhs:>rhs | local pattern names |

lhs=rhs and lhs:=rhs | local pattern names |

With[{x=x_{0},…},body] | local constants |

Module[{x,…},body] | local variables |

Scoping constructs in the Wolfram Language.

The Wolfram Language has several "scoping constructs" in which certain names are treated as local. When you mix these constructs in any way, the Wolfram Language does appropriate renamings to avoid conflicts.

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

The Wolfram Language treats transformation rules as scoping constructs, in which the names you give to patterns are local. You can set up named patterns either using , , and so on, or using .

In[14]:= |

Out[14]= |

In a rule like , the that appears on the right‐hand side goes with the name of the pattern. As a result, this is treated as a variable local to the rule, and cannot be modified by other scoping constructs.

The , on the other hand, is not local to the rule, and *can* be modified by other scoping constructs. When this happens, the Wolfram Language renames the patterns in the rule to prevent the possibility of a conflict.

In[15]:= |

Out[15]= |

When you use With on a scoping construct, the Wolfram Language automatically performs appropriate renamings. In some cases, however, you may want to make substitutions inside scoping constructs, without any renaming. You can do this using the operator.

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

When you apply a rule such as , or use a definition such as , the Wolfram Language implicitly has to substitute for x everywhere in the expression rhs. It effectively does this using the operator. As a result, such substitution does not respect scoping constructs. However, when the insides of a scoping construct are modified by the substitution, the other variables in the scoping construct are renamed.

In[18]:= |

In[19]:= |

Out[19]= |

In[20]:= |

In[21]:= |

Out[21]= |