Mathematica — why does TreeForm[Unevaluated[4^5]] evaluate the 4^5?

谁说我不能喝 提交于 2020-01-02 01:47:08

问题


If I give Mathematica the input

TreeForm[Unevaluated[4^5]]

I expect to see three boxes -- power, 4, and 5.

Instead I see a single box with 1024. Can anyone explain?


回答1:


Compare

TreeForm@Unevaluated[4^5]  

with

TreeForm@Hold[4^5]  

From the help:

Unevaluated[expr] represents the unevaluated form of expr when it appears as the argument to a function.

and

Hold[expr] maintains expr in an unevaluated form.

so, as Unevaluated[4^5] gets to TreeForm ... it gets evaluated ...

It works like this:

f[x_+y_]:=x^y;
f[3+4]
(*
-> f[7]
*)
f[Unevaluated[3+4]]
(*
->81
*)



回答2:


A level of Unevaluated is stripped off with every evaluation, so you can get what you want with:

TreeForm[Unevaluated@Unevaluated[4^5]]



来源:https://stackoverflow.com/questions/5722679/mathematica-why-does-treeformunevaluated45-evaluate-the-45

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!