why does Function have the HoldAll attribute?
Because it couldn't possibly work if it weren't HoldAll
... There's so much code which is just plain invalid unless the argument is substituted in.
What about e.g.
Plus@@Table[i, {i, #}]&
? Just try what happens if you remove the &
and let the innards evaluate.
It goes wrong in multiple ways.
In[1]:= Plus @@ Table[i, {i, #}]
During evaluation of In[1]:= Table::iterb: Iterator {i,#1} does not have appropriate bounds.
Out[1]= {2 i, i + #1}
Just about any function except for ones defined as trivial formulae would break.
Plus, the effect of HoldAll
can always be cancelled with a simple Evaluate
.
In[2]:= f = 2 # &
Out[2]= 2 #1 &
In[3]:= g = Evaluate[f[#]/2] &
Out[3]= #1 &
The first question is a bit abstruse for me so I'd like to leave it to someone more knowledgeable. As to the second question,
g = f[[1]]/2 & /. OwnValues[f]
g = With[{f = f}, f[[1]]/2 &]
are 2 common ways to handle the issue.