Default
更多信息
- _. 表示一个函数的可选参数,其默认值由 Default 指定.
- 必要的 Default[f] 值必须在 _. 作为 f 的参数使用前定义.
- 为 Default[f,…] 定义的值与 Options[f] 的值一起存储在 DefaultValues[f] 中.
- 默认值仅影响模式匹配,不会为 f 创建任何计算规则.
范例
打开所有单元 关闭所有单元基本范例 (2)
Default[f] = 0f[x_., y_.] = {x, y}f[a]f[]Default[f, 1] = 1;
Default[f, 2] = 2;Replace[f[], f[x_., y_.] :> {x, y}]范围 (1)
Default[f, 1, 2] = 2;Default[f, 1, 3] = 3;Replace[f[y], f[x_., y_] :> {x, y}]Replace[f[y, z], f[x_., y_, z_] :> {x, y, z}]属性和关系 (4)
为 Default[f] 赋值将为所有位置定义默认参数:
Default[f] = 0Replace[f[], f[x_., y_.] :> {x, y}]给 Default[f] 赋值只会影响 f 的模式匹配:
f[x_Integer] := xMatchQ[f[], f[_.]]Default[f] = 0MatchQ[f[], f[_.]]{f[], f[0]}在 f 的定义中使用涉及 Optional 的模式来创建默认计算:
f[Optional[x_Integer]] := xf[]Default[f] = 0;
Default[g, n_] := n ^ 2;f[a_, b_, c_.] := c
g[a_, b_, c_.] := c{f[a, b], g[a, b]}使用 ClearAll 清除默认值:
Default[f] = Default[g] = 0ClearAll[f];
DefaultValues[f]Clear 则不会清除默认值:
Clear[g];
DefaultValues[g]相关指南
-
▪
- 模式
历史
1988年引入 (1.0)
文本
Wolfram Research (1988),Default,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Default.html.
CMS
Wolfram 语言. 1988. "Default." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Default.html.
APA
Wolfram 语言. (1988). Default. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Default.html 年
BibTeX
@misc{reference.wolfram_2026_default, author="Wolfram Research", title="{Default}", year="1988", howpublished="\url{https://reference.wolfram.com/language/ref/Default.html}", note=[Accessed: 11-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_default, organization={Wolfram Research}, title={Default}, year={1988}, url={https://reference.wolfram.com/language/ref/Default.html}, note=[Accessed: 11-September-2026]}