4
$\begingroup$

I have a list like

l = {2 - 3, 12 - 4, Times, aab}

I want to end up with

{"2-3", "12-4", "Times", "aab"}

I have tried

lh = Map[ToString, HoldForm[l], {2}]

Which gives the promising result

{ToString[2-3],ToString[12-4],ToString[Times],ToString[aab]}

However when I add ReleaseHold the numerical values get evaluated (then converted to strings).

ReleaseHold@lh

{"-1", "8", "Times", "aab"}

How can I achieve the fully unevaluated string form?

This question may be a duplicate. If so, I apologise.

$\endgroup$

2 Answers 2

5
$\begingroup$
ToString /@ HoldForm /@ Unevaluated@{2 - 3, 12 - 4, Times, aab}

or

HoldForm /@ Unevaluated@{2 - 3, 12 - 4, Times, aab} /. x_ :> ToString @ x

both give

 (* {"2 - 3", "12 - 4", "Times", "aab"} *)
$\endgroup$
0
2
$\begingroup$

That might be the ugliest way to do it, but it works:

list = HoldForm@{2 - 3, 12 - 4, Times, aab};
string = ToString@list
listString = 
  StringSplit[StringReplace[string, {"{" -> "", "}" -> "", " " -> ""}], ","]

{"2-3", "12-4", "Times", "aab"}

Then, ToExpression /@ listString yields:

{-1, 8, Times, aab}
$\endgroup$
2
  • $\begingroup$ This doesn't work properly for cases such as l = {Add[a, b], Mul[b, c]} $\endgroup$
    – Aky
    May 20, 2014 at 15:42
  • $\begingroup$ It doesn't work if you have a comma indeed. $\endgroup$
    – Öskå
    May 20, 2014 at 16:05

Your Answer

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

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