I remember seeing a recipe to take an expression and evaluate every Head that matches pattern x, while leaving subexpressions with non-matching heads unevaluated. I can't find this recipe anymore, does anyone know the right way to do this?
This one is from Ted Ersek's Mathematica Tricks under "Clever Little Programs".
Thanks to @TomD for the pointer.
EvaluatePattern[expr_,pattn_]:=expr/.Pattern[p, pattn]:>With[{eval=p},eval/;True]
In[368]:= test = HoldForm[7 (1 + 2 - 2^2) (8 + 8)];
EvaluatePattern[test, _Plus] //InputForm
Out[369]= HoldForm[7*-1*16]
Edit
It seems to work also with Hold[], but I never ran a deep test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With