1
$\begingroup$

Forgive my ignorance, I'm new to Mathematica/the Wolfram Language.

I'm looking for a way to apply HoldForm or some equivalent to the result of a function, as well as its arguments.

For example, I would like to modify

Sort[Unevaluated[n+1+2]]]

so that it returns 1+2+n rather than 3+n.

Using Trace on this line reveals the following.

{Sort[n+1+2],1+2+n,3+n}

So it appears that my arguments are being passed in unevaluated, as I had hoped, but that the result is evaluated. How can I prevent this?

Edit:

I have found that using some version of Inactive doesn't quite suit my needs, at least on its face, because I would like to be able to convert the result to a string for export. From what I can tell:

ToString[HoldForm[1+2+n]]

returns "1 + 2 + n", which is what I need.

However,

ToString[Inactive[Plus][1+2+n]]

or

ToString[Inactive[Plus] @@ Sort[{n, 1, 2}]]

returns a string with the Inactive wrapper.

Is there a way to use HoldForm rather than Inactive? Or is there a way to make an inactive expression convert to a string without the wrapper?

$\endgroup$

closed as off-topic by Kuba Mar 16 '17 at 7:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Inactive[Plus] @@ Sort[{n, 1, 2}] $\endgroup$ – Bob Hanlon Mar 11 '17 at 20:16
  • $\begingroup$ Thanks for the comment, @Bob Hanlon. I have found that Inactive[Plus] doesn't quite suit my needs. I have modified my question to add clarification. $\endgroup$ – mm716783 Mar 11 '17 at 20:38
  • $\begingroup$ ToString[Plus @@ (ToString /@ Sort[{n, 1, 2}])] $\endgroup$ – Bob Hanlon Mar 11 '17 at 21:46
  • $\begingroup$ You can also circumvent the issue of Inactive[Plus] by replacing it afterwards: Inactive[Plus] @@ Sort[{n, 1, 2}] /. Inactive[Plus][a__] :> HoldForm[Plus[a]] $\endgroup$ – b3m2a1 Mar 12 '17 at 20:12
  • $\begingroup$ Do you care about preventing evaluation of n or only about 1/2 not being added up. What is really the input here? $\endgroup$ – Kuba Mar 13 '17 at 19:36
0
$\begingroup$

Depending on exactly what you are trying to achieve, you could replace Plus with, for example, CirclePlus

Sort[n⊕1⊕2]
(* 1⊕2⊕n *)
$\endgroup$

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