Re: A ToExpression question
- To: mathgroup at smc.vnet.net
- Subject: [mg58341] Re: A ToExpression question
- From: dh <dh at metrohm.ch>
- Date: Tue, 28 Jun 2005 21:56:40 -0400 (EDT)
- References: <d9r4h4$53i$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Ray,
A function does replace its dummy arguments by the actual arguments
before it evaluates the body. Values insides strings are not replaced.
Therefore, f2 prints x because it has not been replaced. f3 prints y
because Module renames y by y$nnn and gives it the value x, but the
replacement is not done inside the string. Here we still have y that has
no value and prints as "y". On the contrary, Block does not rename local
variables, it keeps the name, but temporaraily gives it the value of x.
Therefore, Print[y] prints 0 because y has temporarily the value 0.
sincerely, Daniel
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 ?
>