MathGroup Archive 2009

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

Search the Archive

Re: writing a function that would print the name of a

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97665] Re: [mg97609] writing a function that would print the name of a
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Wed, 18 Mar 2009 04:54:15 -0500 (EST)
  • References: <200903170956.EAA16955@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

Something like...

Clear[echo]
SetAttributes[echo, HoldFirst]
echo[x_] := Print[ToString@HoldForm@x, " = ", x]

x = 10;
echo[x]

x = 10

echo[10]

10 = 10

echo[10!]

10! = 3628800

Bobby

On Tue, 17 Mar 2009 04:56:59 -0500, <johnclin at hss.caltech.edu> wrote:

> I have a question that seems simple but I just could not figure out
> how to do it.
>
> I am trying to write a mathematica function that will print the *name*
> of the variable that I pass to the function as well as the value
> itself.
>
> For example, let's say that I define a variable x = 10.
>
> Now, I want to write a function that will print:
> "The variable x contains the value 10".
>
> I tried something like:
> displayOutput [varToPrint_] := (
> Print["The variable "];
> Print[ToString[varToPrint]]; (* <-- this is the problematic line*)
> Print["contains the value "];
> Print[x];
> )
>
> BTW, I do realize that the following works:
> x = 10;
> Print["The symbol ", HoldForm[x], " has a value of ", x];
>
> So there is something about how Mathematica evaluates expression
> inside a function that I am not understanding. Any suggestions?
>
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: All Possible Combinations from N Sublists
  • Next by Date: Superscripts in post-processed plot labels
  • Previous by thread: writing a function that would print the name of a passed variable
  • Next by thread: Re: Re: writing a function that would print the