4
$\begingroup$

I mean, suppose I have this expression:

2 + 2

I want to get something like this:

Plus[2, 2]

as an output. I've tried FullForm, but this just evaluates 2+2 and returns 4. Is this even possible?

$\endgroup$
4
  • 1
    $\begingroup$ Try 2+2//Hold//FullForm $\endgroup$
    – C. E.
    Commented Aug 27, 2014 at 21:44
  • $\begingroup$ Have a look at HoldForm $\endgroup$
    – mfvonh
    Commented Aug 27, 2014 at 21:44
  • $\begingroup$ You can try prepending a Hold. Hold[2+2] // FullForm. You might also like TreeForm for this purpose. $\endgroup$
    – evanb
    Commented Aug 27, 2014 at 21:45
  • $\begingroup$ Generally, what you are seeking is to prevent kernel evaluation. Hold and its ilk (Defer, Unevaluated, Inactive, etc.) are the mechanisms for this, depending on precisely what you want. Look at this page for more information. $\endgroup$
    – mfvonh
    Commented Aug 27, 2014 at 21:49

1 Answer 1

7
$\begingroup$
tree = Function[x, Defer @ FullForm @ x, HoldAll];

Now:

2 + 2 // tree
Plus[2, 2]

I used Defer to allow the output to be evaluated. If you do not prefer this replace it with HoldForm.

For some explanation of the mechanics of this code see: Why doesn't "Defer" work with "TableForm"?

See also my standard methods for analyzing parsing.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.