Function 
更多信息
- 当 Function[body] 或 body& 用到参数集合上时,#(或 #1)由第一个参数代替,#2 由第二个代替,以此类推. #0 由函数本身代替.
- 如果给出的参数比函数中 # i 数目更多的话,剩余的参数被忽略. »
- ## 表示提供的所有参数的序列. »
- ## n 表示从数字 n 开始的参数. »
- 当应用于相关性时,#name 等价于 #["name"],并且挑选相关性中的元素.
- 在格式 #name 中,name 中的字符可以是不以数字开始的任意字母数字组合.
- 用
|->
、
fn
或 \[Function] 键入符号 . - Function 与 LISP 或形式逻辑中的 λ 类似.
- Function 有属性 HoldAll. 仅在形式参数被自变量替换后计算函数体.
- Function[{x1,…},body] 中已命名的形式参数 xi 视为局部变量处理,并当需要避免和提供给函数的实际自变量混淆时,被重命名为 xi$. »
- Function 结构可以以任意方式嵌套. 每种方式都可以当作作用域结构处理,如果有必要,已命名的内部变量被重命名. »
- 在 Function[params,body,attrs] 中,attrs 可以是一个属性或属性列表. »
- Function[Null,body,attrs] 表示一个函数,用 # 等给出 body 内的参数.
范例
打开所有单元 关闭所有单元基本范例 (4)
Function[u, 3 + u][x]Function[3 + #][x](3 + #)&[x]Function[{u, v}, u ^ 2 + v ^ 4][x, y](#1 ^ 2 + #2 ^ 4)&[x, y]f = (3 + #)&{f[a], f[b]}f[#u, #v, #u]&[<|"u" -> x, "v" -> y|>]范围 (15)
把纯函数用作变量 (5)
g[#, # ^ 2]& /@ {x, y, z}Select[{1, -1, 2, -2, 3}, # > 0&]Cases[{1, -1, 2, -2, 3}, _Integer ? (# > 0&)]Array[1 + # ^ 2&, 10]Sort[{{a, 2}, {c, 1}, {d, 3}}, #1[[2]] < #2[[2]]&]把纯函数用作选项值 (3)
在 FixedPoint 中指定一个自定义的比较函数:
FixedPoint[(# + 2 / # ) / 2&, 1`20, SameTest -> (Abs[#1 - #2] < 1*^-10&)]DensityPlot[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> (RGBColor[1 - #, #, 1 - #]&)]Nearest[{1, 2, 4, 8, 16}, 5, DistanceFunction -> ((#1 - #2) ^ 2&)]将一个纯函数作为结果返回 (4)
Function[x, x ^ 2]'Tan 的导数:
Tan'DSolve[{y'[x] == ay[x], y[0] == 1}, y, x]RSolve[{a[n + 1] - 2a[n] == 1, a[0] == 1}, a, n]函数和相关性 (3)
#x&[<|"x" -> a, "y" -> b|>]#["x"]&[<|"x" -> a, "y" -> b|>]#y&[<|"x" -> 1, "y" -> 2|>, <|"x" -> 3, "y" -> 4|>]#2["y"]&[<|"x" -> 1, "y" -> 2|>, <|"x" -> 3, "y" -> 4|>]推广和延伸 (4)
f[##]&[a, b, c, d]f[X, ##, Y, ##]&[a, b, c, d]f[##2]&[a, b, c, d]f[##1, X, ##2, Y, ##3, Z, ##4]&[a, b, c, d]创建一个具有 Listable 属性的纯函数:
Function[{u}, g[u], Listable][{a, b, c}]Function[{u}, g[u]][{a, b, c}]f[#0]&[x]f = If[#1 == 1, 1, #1 #0[#1 - 1]]&f[10]应用 (3)
cplus = Plus@@#&cplus[{a, b, c}]makef[n_] := Function[x, n x]f2 = makef[2]f2[5]Select[Hold[x, $MaxMachineNumber], Function[symbol, Context[symbol] === "System`", HoldAll]]属性和关系 (11)
f[#1]&[x, y, z]17& /@ {1, 2, 3}(p + #)& /. p -> q%[x]Function[{x}, Function[{y}, f[x, y]]][y]Function[{x}, Function[{y}, f[x, y]]][a]Function[{x}, Function[{y}, Function[{z}, f[x, y, z]]]][a]Function[x, Function[y, x ^ y]][x][y]Function[y, Function[x, y ^ x]][x][y]Function[x, Function[x, x ^ x]][x][y]Function[x, Function[y, x ^ y]][a][b]Function[{x, y}, x ^ y][a, b]f[#]&[a]f[##]&[a, b, c]formula = (1 + x) ^ 2;Function[x, Evaluate[formula]]在 Table 中使用一个公式:
Table[i ^ 2, {i, 10}]在相等的 Array 表达式中,用相应的纯函数:
Array[# ^ 2&, 10]特殊目的的函数结构包括 InterpolatingFunction:
f = Interpolation[{1, 2, 4, 5, 6}]f[4]f = Compile[{x}, x ^ 2]f[5.0]f = Nearest[{1, 2, 3, 4, 5}]f[3.1]f = LinearSolve[{{1, 2}, {3, 4}}]f[{5, 6}]可能存在的问题 (4)
& 的绑定较 -> 宽松,所以它通常需要在规则中使用圆括号:
FullForm[x -> y&]FullForm[x -> (y&)]& 的绑定较 ? 宽松,所以它通常需要在模式测试中使用圆括号:
Cases[{1, 2, 3, 4}, _ ? (OddQ[# / 2]&)]Function 直到函数被应用才计算它的函数体:
(# + # + #)&%[a]#2&[x]巧妙范例 (2)
定义一个递归理论的递归运算符 [更多信息]:
r[g_, h_] = If[#1 == 0, g[##2], h[#0[#1 - 1, ##2], #1 - 1, ##2]]&r[1&, #1(#2 + 1)&][10]NewtonZero[f_, x0_] := FixedPoint[# - f[#] / f'[#]&, x0]NewtonZero[BesselJ[2, #]&, 5.0]参见
Apply Construct CurryApplied ApplyTo CompiledFunction InterpolatingFunction Slot SlotSequence FunctionCompile IncrementalFunction
字符: \[Function]
Function Repository: ExpressionToFunction
技术笔记
-
▪
- 纯函数 ▪
- 纯函数和规则中的变量 ▪
- 常用记号和表示惯例
相关的工作流程
- 对笔记本中的单元应用函数
历史
1988年引入 (1.0) | 在以下年份被更新:2008 (7.0) ▪ 2014 (10.0) ▪ 2020 (12.2)
文本
Wolfram Research (1988),Function,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Function.html (更新于 2020 年).
CMS
Wolfram 语言. 1988. "Function." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2020. https://reference.wolfram.com/language/ref/Function.html.
APA
Wolfram 语言. (1988). Function. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Function.html 年
BibTeX
@misc{reference.wolfram_2026_function, author="Wolfram Research", title="{Function}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/Function.html}", note=[Accessed: 27-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_function, organization={Wolfram Research}, title={Function}, year={2020}, url={https://reference.wolfram.com/language/ref/Function.html}, note=[Accessed: 27-August-2026]}