Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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"} *)
share|improve this answer

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}
share|improve this answer
    
This doesn't work properly for cases such as l = {Add[a, b], Mul[b, c]} –  Aky May 20 at 15:42
    
It doesn't work if you have a comma indeed. –  Öskå May 20 at 16:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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