&
)
Function
[body]body &
#1
, #2
, etc.Function
[{$x_1$, $x_2$, ...}, body]Function
[{$x_1$, $x_2$, ...}, body, attr]f := # ^ 2 &
f[3]
#^3& /@ {1, 2, 3}
#1+#2&[4, 5]
You can use Function
with named parameters:
Function[{x, y}, x * y][2, 3]
Parameters are renamed, when necessary, to avoid confusion:
Function[{x}, Function[{y}, f[x, y]]][y]
Function[{y}, f[x, y]] /. x->y
Function[y, Function[x, y^x]][x][y]
Function[x, Function[y, x^y]][x][y]
Slots in inner functions are not affected by outer function application:
g[#] & [h[#]] & [5]
In the evaluation process, the attributes associated with an Expression are determined by its Head. If the Head is also a non-atomic Expression, in general, no Attribute is assumed. In particular, it is what happens when the head of the expression has the form:
``Function[body]``
or:
``Function[vars, body]``
h := Function[{x}, Hold[1+x]]
h[1 + 1]
Notice that Hold in the body prevents the evaluation of $1+x$, but not the evaluation of $1+1$. To avoid that evaluation, of its arguments, the Head should have the attribute HoldAll
. This behavior can be obtained by using the three arguments form version of this expression:
h:= Function[{x}, Hold[1+x], HoldAll]
h[1+1]
In this case, the attribute HoldAll
is assumed, preventing the evaluation of the argument $1+1$ before passing it to the function body.