Why is Unevaluated[#]& different from Unevaluated?
I thought that for any function
f
we could use "f
" interchangeably with "f[#]&
".
That is only true if the function has no special attributes. Function
effectively removes those attributes, and all other special behaviour.
For example,
Hold[1 + 1]
(* Hold[1 + 1] *)
Hold[#] &[1 + 1]
(* Hold[2] *)
Function[x, Hold[x], HoldAll][1 + 1]
(* Hold[1 + 1] *)
(For the last one, see the 3rd argument of Function
.)
Please read Evaluation of Expressions at least up to Nonstandard Evaluation. As I remember it should contain the answers to your question.
The two are not equivalent. Even if f
lacks special attributes.
Assuming no special attributes are present, a function's arguments are always evaluated before the function is called. This event occurs twice with f[#] &[x]
, but only once with f[x]
. Now, if everything has been fully evaluated, then you won't see a difference, but if something is marked as "do not evaluate", then you can see a difference.
(This can be very counter-intuitive if you're only used to traditional programming languages.)
Here's a counterexample:
Print [Unevaluated[2 + 2]] (* prints 2 + 2 *)
Print[#] &[Unevaluated[2 + 2]] (* prints 4 *)
However, I think (though I'm not 100% sure I've considered all the cases) that the following two would be equivalent if f
lacks special attributes:
f
f[Unevaluated[#]] &
The head Unevaluated
is a symbol with the HoldAllComplete
attribute. The head Unevaluated[#]&
is not a symbol and thus has no attributes to suppress Mathematica's evaluation of its tail.