Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 380
William Jockusch Avatar asked Apr 19 '11 21:04

William Jockusch


2 Answers

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

TreeForm[Unevaluated@Unevaluated[4^5]]

enter image description here

like image 121
Mr.Wizard Avatar answered Oct 20 '22 12:10

Mr.Wizard


Compare

TreeForm@Unevaluated[4^5]  

enter image description here

with

TreeForm@Hold[4^5]  

enter image description here

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
*)
like image 27
Dr. belisarius Avatar answered Oct 20 '22 14:10

Dr. belisarius