assigns rhs to be the delayed value of lhs. rhs is maintained in an unevaluated form. When lhs appears, it is replaced by rhs, evaluated afresh each time.
- SetDelayed has attribute HoldAll, rather than HoldFirst.
- You can make assignments of the form lhs:=rhs/;test, where test gives conditions for the applicability of each transformation rule. You can make several assignments with the same lhs but different forms of test.
- lhs:=rhs returns Null if the assignment specified can be performed, and returns $Failed otherwise.
Examplesopen allclose all
Basic Examples (1)
Left Hand Sides (4)
A variable defined with SetDelayed is evaluated every time it is used:
Make definitions for special and general cases using immediate and delayed assignments:
Properties & Relations (9)
The right side of an immediate definition is evaluated when the definition is made:
The right side of a delayed definition is evaluated each time the definition is used:
The arguments of the left side of a definition are evaluated before the definition is made:
Definitions with the same left side overwrite earlier ones:
The pattern variable is renamed if necessary inside a nested scope:
Delayed assignment introduces a scope that is not affected by global variables:
Immediate assignment does not introduce a scope:
Use a rule to do a transformation to a particular expression:
Use a definition to do a transformation automatically for all expressions involving g:
More specific definitions are put in front of more general ones:
Definition prints definitions associated with a symbol:
Information prints various information about a symbol, including any definitions:
DownValues returns a list of rules corresponding to any downvalues defined:
Use Unset (=.) to clear definitions with a particular left-hand side:
Clear any definitions, but not attributes:
Use ClearAll to clear attributes, too:
Possible Issues (4)
Omitting the _ defines a transformation only for a literal value, rather than a function:
Using delayed definitions may have unexpected consequences:
The definition actually made and its behavior:
Use Evaluate to force evaluation of the right-hand side:
Or use Set to force evaluation of the right-hand side:
An iterative runaway definition:
By providing a base case, the iteration terminates:
A recursive runaway definition:
Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.
Wolfram Language. 1988. "SetDelayed." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetDelayed.html.
Wolfram Language. (1988). SetDelayed. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetDelayed.html