Is there a way to convert a Wolfram Language expression / code into a string expression? For example,
Convert the following including its indentations (very important),
If[Length[$ScriptCommandLine]==1,
Print["No expressions were given to square."],
args = Rest[$ScriptCommandLine];
squares = Quiet @ Map[Replace[ToExpression[#], {$Failed:>ToString[#,InputForm]^2, x_ :> x^2}]&, args];
MapThread[Print["The square of ", #1, " is ", #2, "."]&, {args, squares}]
]
into
"If[Length[$ScriptCommandLine]==1,
Print[\"No expressions were given to square.\"],
args = Rest[$ScriptCommandLine];
squares = Quiet @ Map[Replace[ToExpression[#], {$Failed:>ToString[#,InputForm]^2, x_ :> x^2}]&, args];
MapThread[Print[\"The square of \", #1, \" is \", #2, \".\"]&, {args, squares}]
]".
I am looking for a builtin function or a function f that can be written to work like
f[...code...]→ ...string version of code....
I am trying to do this programmatically and probably will require going into the Cell expression.
ToStringdoes this. – Roman Apr 22 '19 at 15:58StringReplace[ToString[Defer[2 + 2]], "Defer[" ~~ x___ ~~ "]" :> x]– ktm Apr 22 '19 at 16:01Cellexpression, but this only exists in the FrontEnd. What do you mean by that? – Somos Apr 22 '19 at 16:08Copy As->Plain Text– Fortsaint May 01 '19 at 17:18