lhs:=rhs
将 rhs 延时赋给 lhs. rhs 保留未计算的形式. 当 lhs 出现时,用 rhs 对其进行替换,并且每次重新计算.
SetDelayed 
lhs:=rhs
将 rhs 延时赋给 lhs. rhs 保留未计算的形式. 当 lhs 出现时,用 rhs 对其进行替换,并且每次重新计算.
更多信息
- SetDelayed 有属性 HoldAll,而不是 HoldFirst 属性.
- 您可以进行诸如 lhs:=rhs/;test 的赋值,这里 test 给出使用每个变换规则的条件. 您可进行具有相同 lhs 而有不同 test 形式的赋值.
- 如果指定的赋值可以执行,lhs:=rhs 返回 Null; 否则返回 $Failed.
范例
打开所有单元 关闭所有单元范围 (9)
左侧 (4)
SetDelayed 定义的变量在每次使用时计算:
r := RandomReal[]
{r, r, r}fact[1] = 1;
fact[n_] := n fact[n - 1]
fact[10]f[x_ /; x > 0] := Sqrt[2x]{f[2], f[-2]}unit[x_ /; x < 0] := 0
unit[x_ /; x ≥ 0] := 1
{unit[-2], unit[0], unit[1], unit[a]}不同类型的值 (5)
r := RandomReal[]
OwnValues[r]f[x_] := x ^ 2
DownValues[f]derivative[1][f][x_] := fg[x];
SubValues[derivative]mod/:a_mod + b_mod := modPlus[a, b]
UpValues[mod]NValues[f]N[f[2]]应用 (3)
newton[r_] := FixedPoint[Function[x, (x + r / x) / 2], r]
newton[5.0]pi2 := pi2 = N[Pi ^ 2, 50]
pi2Definition[pi2]SetAttributes[f, HoldFirst]
f[sym_, val_] := (sym = val ^ 2)x = 17;f[x, 5]x属性和关系 (9)
x = RandomReal[];
{x, x, x}y := RandomReal[];
{y, y, y}x = 5;
f[x] := 17
Definition[f]f[x_] := x ^ 2
f[x_] := x ^ 3
f[2]makedef[z_] := (f[x_] := z ^ 2;)
makedef[x]
Definition[f]因此,输入会与模式变量匹配,而定义右侧的变量则不会与其匹配:
f[5]x = 5;
f[x_] := x ^ 2
{f[2], x}g[x_] = x ^ 2;
{g[2], x}f[2 x y] + f[x y] //. f[a_ b_] :> f[a] + f[b]g[a_ b_] := g[a] + g[b]
g[2 x y] + g[x y] + f[x y]fact[n_] := n fact[n - 1];
fact[1] = 1;
Definition[fact]fact[10]Definition 输出与符号相关的定义:
f[x_] := x ^ 2
Definition[f]Information 输出关于符号的各种信息,包括任何定义:
? fDownValues 返回对应任何定义 downvalue 的规则列表:
DownValues[f]f[-1] = I;
f[x_] := x ^ 2
SetAttributes[f, Listable]用 Unset (=.) 清除特定左边的定义:
f[-1]=.Definition[f]Clear[f]
Definition[f]用 ClearAll 也清除属性:
ClearAll[f]
Definition[f]可能存在的问题 (4)
f[x] := x ^ 2;
g[x_] := x ^ 2;
{f[x], g[x]}{f[2], g[2]}Expand[(x + 1) ^ 3]f[x_] := %Definition[f]f[1]Expand[(x + 1) ^ 3]用 Evaluate 强制对右侧进行计算:
g[x_] := Evaluate[%]
Definition[g]g[1]Expand[(x + 1) ^ 3]或用 Set 强制对右侧进行计算:
h[x_] = %Definition[h]h[1]f[x_] := f[x + 1]
f[1]g[10] = 10;
g[x_] := g[x + 1]
g[1]f[x_] := f[Floor[x / 2]] + 1
f[10]g[1] = 0;
g[x_] := g[Floor[x / 2]] + 1
g[10]技术笔记
相关的工作流程
- 清除符号和函数的定义 ▪
- 查找所有定义的函数
历史
1988年引入 (1.0)
文本
Wolfram Research (1988),SetDelayed,Wolfram 语言函数,https://reference.wolfram.com/language/ref/SetDelayed.html.
CMS
Wolfram 语言. 1988. "SetDelayed." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/SetDelayed.html.
APA
Wolfram 语言. (1988). SetDelayed. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/SetDelayed.html 年
BibTeX
@misc{reference.wolfram_2026_setdelayed, author="Wolfram Research", title="{SetDelayed}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/SetDelayed.html}", note=[Accessed: 06-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_setdelayed, organization={Wolfram Research}, title={SetDelayed}, year={1988}, url={https://reference.wolfram.com/language/ref/SetDelayed.html}, note=[Accessed: 06-November-2026]}