SetDelayed
✖
SetDelayed
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 allBasic Examples (1)Summary of the most common use cases
https://wolfram.com/xid/0enwibiui-l94
https://wolfram.com/xid/0enwibiui-yf8
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:
https://wolfram.com/xid/0enwibiui-v44s82
https://wolfram.com/xid/0enwibiui-fbhfge
Make definitions for special and general cases using immediate and delayed assignments:
https://wolfram.com/xid/0enwibiui-4yu8wt
https://wolfram.com/xid/0enwibiui-5ntk8t
https://wolfram.com/xid/0enwibiui-5mku1t
https://wolfram.com/xid/0enwibiui-ogny7c
Define a function by several conditional cases:
https://wolfram.com/xid/0enwibiui-inl1md
https://wolfram.com/xid/0enwibiui-9oay1
Different Kinds of Values (5)
https://wolfram.com/xid/0enwibiui-bo0ezs
https://wolfram.com/xid/0enwibiui-jxkw44
https://wolfram.com/xid/0enwibiui-m7v0pq
https://wolfram.com/xid/0enwibiui-pauupj
https://wolfram.com/xid/0enwibiui-bez8mk
https://wolfram.com/xid/0enwibiui-42ie1y
https://wolfram.com/xid/0enwibiui-01nq8a
https://wolfram.com/xid/0enwibiui-jhbgh4
https://wolfram.com/xid/0enwibiui-mb1
https://wolfram.com/xid/0enwibiui-jfvucy
https://wolfram.com/xid/0enwibiui-5zrbij
Applications (3)Sample problems that can be solved with this function
Define a procedure that computes a square root with Newton's method:
https://wolfram.com/xid/0enwibiui-l05wi2
https://wolfram.com/xid/0enwibiui-jyuz61
Perform a calculation on demand and cache the result:
https://wolfram.com/xid/0enwibiui-4g98fd
https://wolfram.com/xid/0enwibiui-gjjpox
https://wolfram.com/xid/0enwibiui-d5uee3
Definitions for unevaluated expressions can implement call-by-name semantics:
https://wolfram.com/xid/0enwibiui-ila09r
https://wolfram.com/xid/0enwibiui-o26f52
https://wolfram.com/xid/0enwibiui-7l5qct
https://wolfram.com/xid/0enwibiui-uj8m0p
The global variable has been modified:
https://wolfram.com/xid/0enwibiui-1d1d09
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:
https://wolfram.com/xid/0enwibiui-4kvxs
https://wolfram.com/xid/0enwibiui-j140e
The right side of a delayed definition is evaluated each time the definition is used:
https://wolfram.com/xid/0enwibiui-bmw5eu
https://wolfram.com/xid/0enwibiui-da61nw
The arguments of the left side of a definition are evaluated before the definition is made:
https://wolfram.com/xid/0enwibiui-12wit
https://wolfram.com/xid/0enwibiui-4l5rnz
https://wolfram.com/xid/0enwibiui-yeesj1
Definitions with the same left side overwrite earlier ones:
https://wolfram.com/xid/0enwibiui-wgrx80
https://wolfram.com/xid/0enwibiui-xfiz9f
https://wolfram.com/xid/0enwibiui-s2i97q
The pattern variable is renamed if necessary inside a nested scope:
https://wolfram.com/xid/0enwibiui-qgp3be
https://wolfram.com/xid/0enwibiui-3qyimu
https://wolfram.com/xid/0enwibiui-33rasz
https://wolfram.com/xid/0enwibiui-4l3nrq
Delayed assignment introduces a scope that is not affected by global variables:
https://wolfram.com/xid/0enwibiui-lynxon
https://wolfram.com/xid/0enwibiui-otatma
https://wolfram.com/xid/0enwibiui-bj9a49
Immediate assignment does not introduce a scope:
https://wolfram.com/xid/0enwibiui-t1bwvy
https://wolfram.com/xid/0enwibiui-3f04oz
Use a rule to do a transformation to a particular expression:
https://wolfram.com/xid/0enwibiui-j6btlh
Use a definition to do a transformation automatically for all expressions involving g:
https://wolfram.com/xid/0enwibiui-57yn7y
https://wolfram.com/xid/0enwibiui-nwkpaw
More specific definitions are put in front of more general ones:
https://wolfram.com/xid/0enwibiui-6axrr3
https://wolfram.com/xid/0enwibiui-fsrdmk
https://wolfram.com/xid/0enwibiui-bg1fm4
Definition prints definitions associated with a symbol:
https://wolfram.com/xid/0enwibiui-h2oqvx
https://wolfram.com/xid/0enwibiui-punx2e
Information prints various information about a symbol, including any definitions:
https://wolfram.com/xid/0enwibiui-25t8k
DownValues returns a list of rules corresponding to any downvalues defined:
https://wolfram.com/xid/0enwibiui-crqqi
https://wolfram.com/xid/0enwibiui-0s77k
Use Unset (=.) to clear definitions with a particular left-hand side:
https://wolfram.com/xid/0enwibiui-hbvy5w
https://wolfram.com/xid/0enwibiui-62m1c
Clear any definitions, but not attributes:
https://wolfram.com/xid/0enwibiui-gbur
https://wolfram.com/xid/0enwibiui-g9pr51
Use ClearAll to clear attributes, too:
https://wolfram.com/xid/0enwibiui-b2spk9
https://wolfram.com/xid/0enwibiui-ch01mm
Possible Issues (4)Common pitfalls and unexpected behavior
Omitting the _ defines a transformation only for a literal value, rather than a function:
https://wolfram.com/xid/0enwibiui-4tisz0
https://wolfram.com/xid/0enwibiui-4xdnx9
https://wolfram.com/xid/0enwibiui-d415wl
Using delayed definitions may have unexpected consequences:
https://wolfram.com/xid/0enwibiui-f1lv1m
https://wolfram.com/xid/0enwibiui-hxwcfv
The definition actually made and its behavior:
https://wolfram.com/xid/0enwibiui-gsr7fo
https://wolfram.com/xid/0enwibiui-nvffht
Use Evaluate to force evaluation of the right-hand side:
https://wolfram.com/xid/0enwibiui-czqpfh
https://wolfram.com/xid/0enwibiui-edyilb
https://wolfram.com/xid/0enwibiui-jf7hi
https://wolfram.com/xid/0enwibiui-gtqr4z
Or use Set to force evaluation of the right-hand side:
https://wolfram.com/xid/0enwibiui-t5z5z
https://wolfram.com/xid/0enwibiui-gygprf
https://wolfram.com/xid/0enwibiui-t3nz1
https://wolfram.com/xid/0enwibiui-e83jpe
An iterative runaway definition:
https://wolfram.com/xid/0enwibiui-0wg4ks
https://wolfram.com/xid/0enwibiui-epzwth
By providing a base case, the iteration terminates:
https://wolfram.com/xid/0enwibiui-hne6n
https://wolfram.com/xid/0enwibiui-kqkws
A recursive runaway definition:
https://wolfram.com/xid/0enwibiui-9fyukt
https://wolfram.com/xid/0enwibiui-68zw6h
By providing a base case, the recursion terminates:
https://wolfram.com/xid/0enwibiui-b16ocs
https://wolfram.com/xid/0enwibiui-d4c3w7
Neat Examples (1)Surprising or curious use cases
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
]}
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
]}