开发者

Evaluating only particular Head type in expression?

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 开发者_如何学JAVAthis 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜