With
With[{x=x0,y=y0,…},expr]
specifies that all occurrences of the symbols x, y, … in expr should be replaced by x0, y0, ….
Details
- With allows you to define local constants.
- With replaces symbols in expr only when they do not occur as local variables inside scoping constructs.
- With[{x:=x0,…},expr] inserts the unevaluated form x0 into expr.
- You can use With[{vars},body/;cond] as the right‐hand side of a transformation rule with a condition attached. »
- With has attribute HoldAll.
- With constructs can be nested in any way, with inner variables being renamed if necessary.
- With is a scoping construct that implements read‐only lexical variables.
Examples
open allclose allBasic Examples (3)
Evaluate an expression with x locally set to 7:
With works even without evaluation:
Scope (4)
Use With to insert values into held expressions:
Use := to insert the unevaluated form of an expression into the result:
Applications (1)
With allows inserting values into unevaluated expressions:
Properties & Relations (5)
Using x:=x0 inserts the unevaluated form of x0, which then evaluates based on its surroundings:
Using x=x0 evaluates x0 once and inserts the result everywhere x appears:
Module introduces local variables to which values can be assigned:
With variables are read only:
Block localizes values only; it does not substitute values. Module creates new symbols:
With allows substitution inside an unevaluated expression, preserving nested scopes:
Possible Issues (1)
With is a scoping construct; variables are renamed in nested scopes:
Neat Examples (2)
Find a zero of an arbitrary function using Newton's method:
A version of With where the initializer is within the scope of the local variable:
Here the f inside the function definition is not inside its own scope:
Text
Wolfram Research (1991), With, Wolfram Language function, https://reference.wolfram.com/language/ref/With.html.
CMS
Wolfram Language. 1991. "With." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/With.html.
APA
Wolfram Language. (1991). With. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/With.html