6
$\begingroup$

I would like to be able to pass an unquoted string as a parameter to a Mathematica function [that I am writing] and have it show up without evaluation if the string happens to be defined. Thus I would like

Clear["Global`*"]
f[x_] := Print[ToString[x]];
f[Middle] (* prints the string Middle *)
Middle = 3
f[Middle] (* prints the string Middle *)

Obviously this cannot work the way I've written it. Yet it is legal to write

Plot[g[x],{x,0,3}, PlotStyle->Black]

and Black happens to be a protected variable, so it will never be defined.

How can I emulate, or come close to emulating, this behavior. I'm willing to put up with some compromises in the function invocation syntax (in particular, the option syntax opt->Middle would be fine).

Apologies if this is somewhere in the docs. I couldn't find it...

Thanks.

Addendum: While J.M.'s solution answers the question I posed above, I would also like the following to work:

Clear["Global`*"]
SetAttributes[foo,HoldAll];
foo[x_] := Print[ToString[Unevaluated[x]]];
Table[foo[i],{i,{a,b,c}}] (* prints {i,i,i}, as expected *)
Table[foo[Evaluate[i]], {i,{a,b,c}}] (* prints {a,b,c} as expected *)
a = 3
Table[foo[Evaluate[i]], {i,{a,b,c}}] (* prints {3,b,c} *)

I would like the last line to print {a,b,c} just like the one before it - that is, I'd like i evaluated only once. Am I asking too much?

For context: this is part of a larger function that computes something, but does so in different ways depending on the setting of a mode parameter. I want to be able to pass in that mode parameter without evaluation; hence the original question. But I also want to be able to invoke the function inside a Table function as above. Here's a simple example:

Clear["Global`*"]
SetAttributes[foo,HoldAll];
foo[x_] := Block[{z=ToString[Unevaluated[x]]}, If[x == "square", x^2, x+1]];
Table[Integrate[foo[Evaluate[i]],{x,0,3}],{i,{"square", "xyzzy"}}];
    {9, 45/2}
a=3
Table[Integrate[foo[Evaluate[i]],{x,0,3}],{i,{"square", "xyzzy"}}];
    {45/2, 45/2}

since in the second invocation the parameter to foo has the value 3, not the value a. How can I achieve both of these behaviors?

$\endgroup$
1

2 Answers 2

11
$\begingroup$

This seems to work:

SetAttributes[f, HoldAll];
f[x_] := Print[ToString[Unevaluated[x]]]

Now, try Middle = 3; f[Middle].

$\endgroup$
2
  • 1
    $\begingroup$ I was aboooout to post the same. +1 $\endgroup$
    – Rojo
    Jun 23, 2012 at 17:04
  • $\begingroup$ That should work fine. Thank you. $\endgroup$
    – rogerl
    Jun 23, 2012 at 17:28
7
$\begingroup$

You may want to use the attribute HoldAllComplete to avoid the Orderless property of Times, allowing multiple-word strings:

SetAttributes[func, HoldAllComplete]
func[stuff___] := ToString[Unevaluated[stuff]]

func[this is a string without quotes]
"this is a string without quotes"

Lower level syntax rules are still parsed however, so this is only safe with alphanumeric words:

func[this is @ a @ string // without quotes]
"(without quotes)[this is[a[string]]]"

Regarding your updated question you should almost certainly be using indexed variables (DownValues). See this answer for reference.

Your attempt:

a = 3

Table[foo[Evaluate[i]], {i,{a,b,c}}] (* prints {3,b,c} *)

Cannot work, as a evaluates to 3 before Table assigns it to i (using a mechanism similar to Block). Consider:

a := Print["!"]

Table[Abort[], {i, {a, b, c}}]
During evaluation of In[]:= !

Out[]= $Aborted

As you can see the Print evaluates before the body.

If you have advance knowledge of the symbol names you could use Block (but IMHO this entire process is inadvisable):

Block[{a},
 Table[ToString[i], {i, {a, b, c}}]
]
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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