MathGroup Archive 2005

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

Search the Archive

Re: A ToExpression question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58360] Re: [mg58316] A ToExpression question
  • From: János <janos.lobb at yale.edu>
  • Date: Tue, 28 Jun 2005 21:56:55 -0400 (EDT)
  • References: <200506280913.FAA05098@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Jun 28, 2005, at 5:13 AM, Ray Koopman wrote:

> f1[x_] := Print[x];
> f2[x_] := ToExpression["Print[x]"];
> f3[x_] := Module[{y = x}, ToExpression["Print[y]"]];
> f4[x_] :=  Block[{y = x}, ToExpression["Print[y]"]];
>
> Scan[#[0]&,{f1,f2,f3,f4}]
>
> 0
> x
> y
> 0
>
> Why don't f2 and f3 print 0 ?
>

Ray,

I always marvel at your postings.  I do not know.  However one of my  
physics prof used to say: "  Whatever you think about the laws of  
physics, try it first on yourself".  So I tried it on ToExpression.   
I created a g function by replacing Print[x] with ToExpression["x"]:

In[26]:=
g[x_] := ToExpression[
     "ToExpression["*x*"]"];

In[78]:=
g[0]
Out[78]=
0

In[79]:=
g[1]
 From In[79]:=
\!\(\*
   RowBox[{\(ToExpression::"notstrbox"\),
     ":", "\<\"\\!\\(\\\"]\\\"\\\\ \\\"ToExpression[\\\"\\) is not a  
string or \
a box. ToExpression can only interpret strings or boxes as  
Mathematica input. \
\\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\ 
\"RefGuideLinkText\
\\\", ButtonFrame->None, \
ButtonData:>\\\"ToExpression::notstrbox\\\"]\\)\"\>"}]\)
Out[79]=
$Failed
 From In[84]:=
ToExpression::"notstrbox":"\!\(\"]\"\\ \"ToExpression[\"\) is not a  
string or \
a box. ToExpression can only interpret strings or boxes as  
Mathematica input. \
\!\(\*ButtonBox[\"More\[Ellipsis]\", ButtonStyle->\"RefGuideLinkText 
\", \
ButtonFrame->None, ButtonData:>\"ToExpression::notstrbox\"]\)"

Why it did not blow up on g[0] ?  On the same time if I replace the  
outer quotes with ToString like:

In[93]:=
g[x_] := ToExpression[
     ToString[ToExpression[
       "x"]]];

Than it works fine.  Obviously ToString[x] is not equal to "x" although:

In[101]:=
"tttt" == ToString[tttt]
Out[101]=
True

With the best,

János


  • Prev by Date: Re: strange errors for numerical evaluation of an infinite sum
  • Next by Date: Re: Making a presentation to an audience
  • Previous by thread: Re: A ToExpression question
  • Next by thread: Re: A ToExpression question