I was trying to test whether using func[x,y]
is the same as func[#,y]&@x
:
SetAttributes[test, HoldFirst]
test[1 + 2 - 3, 5 - 5]
test[#, 5 - 5] &@(1 + 2 - 3)
test[1 + 2 - 3, 0]
test[0, 0]
So apparently, HoldFirst
is ignored in the second case. Why is it so? Is the argument of @
evaluated before actually being passed?
Answer
andre has given a good answer. I will add a little further clarification. The function f[#]&
is a different function than the function f
and does not have its attributes. It is the short form of
Function[x, f[x]]
Assume
Clear[f]; SetAttributes[f, HoldFirst]
has been evaluated. Even though f
is HoldFirst
, both
Function[x, f[x]][1 + 1]
and
Function[x, f[x]] @ (1 + 1)
give
f[2]
When the pure function is also given the attribute HoldFirst
, evaluation goes as you expect.
Function[x, f[x], HoldFirst] @ (1 + 1)
f[1 + 1]
Comments
Post a Comment