WOLFRAM

lhs:=rhs

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.

Details

  • 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.

Examples

open allclose all

Basic Examples  (1)Summary of the most common use cases

Out[2]=2

Scope  (9)Survey of the scope of standard use cases

Left Hand Sides  (4)

A variable defined with SetDelayed is evaluated every time it is used:

Out[2]=2

Make definitions for special and general cases using immediate and delayed assignments:

Out[2]=2

Make conditional definitions:

Out[2]=2

Define a function by several conditional cases:

Out[2]=2

Different Kinds of Values  (5)

Ownvalues:

Out[2]=2

Downvalues:

Out[2]=2

Subvalues:

Out[2]=2

Upvalues:

Out[2]=2

Numerical values:

Out[2]=2
Out[3]=3

Applications  (3)Sample problems that can be solved with this function

Define a procedure that computes a square root with Newton's method:

Out[2]=2

Perform a calculation on demand and cache the result:

Out[2]=2
Out[3]=3

Definitions for unevaluated expressions can implement call-by-name semantics:

Out[4]=4

The global variable has been modified:

Out[5]=5

Properties & Relations  (9)Properties of the function, and connections to other functions

The right side of an immediate definition is evaluated when the definition is made:

Out[2]=2

The right side of a delayed definition is evaluated each time the definition is used:

Out[4]=4

The arguments of the left side of a definition are evaluated before the definition is made:

Out[3]=3

Definitions with the same left side overwrite earlier ones:

Out[3]=3

The pattern variable is renamed if necessary inside a nested scope:

Out[3]=3
Out[4]=4

Delayed assignment introduces a scope that is not affected by global variables:

Out[3]=3

Immediate assignment does not introduce a scope:

Out[5]=5

Use a rule to do a transformation to a particular expression:

Out[1]=1

Use a definition to do a transformation automatically for all expressions involving g:

Out[3]=3

More specific definitions are put in front of more general ones:

Out[2]=2
Out[3]=3

Definition prints definitions associated with a symbol:

Out[2]=2

Information prints various information about a symbol, including any definitions:

DownValues returns a list of rules corresponding to any downvalues defined:

Out[4]=4

Use Unset (=.) to clear definitions with a particular left-hand side:

Out[3]=3

Clear any definitions, but not attributes:

Out[5]=5

Use ClearAll to clear attributes, too:

Out[7]=7

Possible Issues  (4)Common pitfalls and unexpected behavior

Omitting the _ defines a transformation only for a literal value, rather than a function:

Out[2]=2
Out[3]=3

Using delayed definitions may have unexpected consequences:

Out[1]=1

The definition actually made and its behavior:

Out[3]=3
Out[4]=4

Use Evaluate to force evaluation of the right-hand side:

Out[5]=5
Out[7]=7
Out[8]=8

Or use Set to force evaluation of the right-hand side:

Out[9]=9
Out[10]=10
Out[11]=11
Out[12]=12

An iterative runaway definition:

Out[2]=2

By providing a base case, the iteration terminates:

Out[4]=4

A recursive runaway definition:

Out[2]=2

By providing a base case, the recursion terminates:

Out[4]=4

Neat Examples  (1)Surprising or curious use cases

Dynamic programming for the Fibonacci sequence:

Out[2]=2

New definitions have been added during the calculation:

Out[3]=3
Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.
Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.

Text

Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.

Wolfram Research (1988), SetDelayed, Wolfram Language function, https://reference.wolfram.com/language/ref/SetDelayed.html.

CMS

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. Wolfram Research. https://reference.wolfram.com/language/ref/SetDelayed.html.

APA

Wolfram Language. (1988). SetDelayed. Wolfram Language & System Documentation Center. Retrieved from 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

BibTeX

@misc{reference.wolfram_2024_setdelayed, author="Wolfram Research", title="{SetDelayed}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/SetDelayed.html}", note=[Accessed: 10-January-2025 ]}

@misc{reference.wolfram_2024_setdelayed, author="Wolfram Research", title="{SetDelayed}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/SetDelayed.html}", note=[Accessed: 10-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_setdelayed, organization={Wolfram Research}, title={SetDelayed}, year={1988}, url={https://reference.wolfram.com/language/ref/SetDelayed.html}, note=[Accessed: 10-January-2025 ]}

@online{reference.wolfram_2024_setdelayed, organization={Wolfram Research}, title={SetDelayed}, year={1988}, url={https://reference.wolfram.com/language/ref/SetDelayed.html}, note=[Accessed: 10-January-2025 ]}