MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Displaying a list of variables together with their names

Hi Szabolcs,


> My Mathematica skills got a little rusty

You are kidding, right?

Seriously, I don't see an easy solution which would be much simpler
conceptually -
looks like some number of Unevaluated will be needed anyways in this
You can save some keystrokes by using Block trick though:

SetAttributes[dynamicVariableDisplayAlt, HoldAll];
dynamicVariableDisplayAlt[vars__] :=
  Dynamic@Grid[Block[{vars}, {SymbolName[#], #} & /@ {vars}],
    Alignment -> {{Left, "."}}];

Here I exploited the way name conflicts are resolved when RuleDelayed is the
scoping construct - namely that it does not respect inner scoping constructs
(Block here).
So, while you get a warning by {vars} painted in red inside Block, it will
work all right.
You can also make this a pure function, which is a little more compact:

dynamicVariableDisplayPure =
   Dynamic@Grid[Block[{##}, {SymbolName[#], #} & /@ {##}],
     Alignment -> {{Left, "."}}], HoldAll];

This example is actually quite interesting, since here, while Function is a
lexical scoping
construct, with slots is shows macro-like behavior. And I found very
puzzling why this
version with fixed number of named arguments also works:

dynamicVariableDisplayPure3Args =
  Function[{x, y, z},
   Dynamic@Grid[Block[{x, y, z}, {SymbolName[#], #} & /@ {x, y, z}],
     Alignment -> {{Left, "."}}], HoldAll];

For this, I have no explanation. I would expect Function to respect Block
and therefore the names
of the variables to be {"x","y","z"} and not {"a","b","c"}. But Function
acted as ruthlessly as
RuleDelayed here. Actually, the same can be seen on a much simpler
example Function[x, Block[{x}, Print[x]]][1].


, I was wondering what
> is a simpler / better way to achieve it than this:
> SetAttributes[dynamicVariableDisplay, HoldAll]
> dynamicVariableDisplay[vars__] :=
>  Dynamic@Grid[
>    Transpose[{{Function[x, SymbolName[Unevaluated[x]], {HoldAll}] /@
>        Unevaluated[vars]}, {vars}}], Alignment -> {{Left, "."}}]
> a=1; b=2; c=3;
> dynamicVariableDisplay[a, b, c]
> There's too much juggling with unevaluated symbols.
> Thanks for the help in advance,
> Szabolcs

  • Prev by Date: Mathematica developer
  • Next by Date: Manipulation Problem
  • Previous by thread: Displaying a list of variables together with their names
  • Next by thread: Re: Very very basic question about Mathematica