MathGroup Archive 2005

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

Search the Archive

Re: A ToExpression question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg58356] Re: A ToExpression question
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Tue, 28 Jun 2005 21:56:52 -0400 (EDT)
  • References: <d9r4h4$53i$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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 ?
> 
Hello,

In each of these x is replaced by its value - 0 - before the RHS gets 
evaluated. Thus in f2 the expression Print[x] only comes into being 
after this substitution has taken place. In a Module, local variables, 
like y, are replaced by unique variables such as y$30 - and again, this 
replacement happens before the expression Print[y] is created. f4 works 
because the values of local variabples are simply cleared and restored 
afterwards - no name change is involved.

David Bailey
http://www.dbaileyconsultancy.co.uk


  • Prev by Date: Re: Common factors in a list
  • Next by Date: Re: A ToExpression question
  • Previous by thread: Re: A ToExpression question
  • Next by thread: Re: A ToExpression question