For example, for a single variable I can write
Print[Unevaluated[a], "=", f[a]]
But if I try the next thing I'd think of doing, assuming I want a list, it doesn't work (as intended):
Print[Unevaluated[#], "=", f[#]] & /@ {a, b, c}
This will print the actual value of a
, instead of "a" itself (i.e. the variable name.) Any thoughts how I can achieve this?
-- Edit:
To be perfectly clear, I want something like:
{{"a", f[a]}, {"b", f[b]}, ...}
as the output. It tentatively seems to me that this just simply isn't possible (without putting a Hold or similar on every single thing in the list ...), but I hope I'm wrong.
-- Another edit:
Thanks for the comments; I've almost been able to do what I need. Here's the essential code:
{ SymbolName@Unevaluated[#] & /@ Unevaluated /@ Unevaluated@#1,
Distribute[f[#1, #2], List]} & @@
Unevaluated /@ Unevaluated@{{x, b, c}, {x, y}}
with output
{{x,b,c},{f(2,2),f(2,y),f(b,2),f(b,y),f(c,2),f(c,y)}}
Note that I'm only able to print "{x, b, c}" here. I actually would like a tree; i.e.,
{{x,{x,y}},{b,{x,y},{c,{x,y}}, {f(...), ...}
or something that is equivalent in letting my match the input to the output ..., any thoughts? I'm trying this myself but I keep getting stuck with how Unevaluated treats parameters ... (i.e. it's not listable; and Distribute seems to require that, in order to work in the expected way...)
(I should note, the only reason I want that tree form is because getting the exact form I asked for initially within the confines of the Distribute and so on seems harder (I couldn't work it out ...).) I don't actually care how it comes out, as long as I can Partition or whatever to get the appropriate structure.
-- Final edit:
Just incase anyone is interested, here is what I finally went with; it may not be amazingly elegant, but it's sufficient for what I needed. Note that it's actually neccessary to duplicate the expressions into the Unevaluated
calls (afaik).
fMap = Flatten[Outer[f[#1, #2], A, B, 1]];
n1 = SymbolName@Unevaluated[#] & /@
Unevaluated /@ Unevaluated[{x, b, c, d}];
n2 = SymbolName@Unevaluated[#] & /@
Unevaluated /@ Unevaluated[{x, y, z}];
I can then create the listing I wanted via
Grid[{
grid1 =
Flatten@Insert[n1, ConstantArray["", Length@n2 - 1],
Table[{i + 1}, {i, Range[Length@n1]}]],
grid2 = Flatten@ConstantArray[n2, Length@n1],
fMap
}\[Transpose], Frame -> All];
Print[ToString[#] <> "=" <> ToString[f[#]]] & /@ {a, b, c};
$\endgroup${a, b, c} = Range[3]
before that. $\endgroup$Print[#, "=", f[#]] & /@ Defer /@ Unevaluated@{a, b, c}
orPrint[#, "=", f[#]] & /@ HoldForm/@ Unevaluated@{a, b, c}
work for what you are trying to do? Or,Print[Unevaluated@#, "=", Unevaluated@f[#]] & /@ Unevaluated /@ Unevaluated@{a, b, c}
?:) $\endgroup$Unevaluated
in some detail in my post in this Mathgroup thread(my second post there). Some people found that useful. $\endgroup$