Set inside Module

• To: mathgroup <mathgroup at yoda.physics.unc.edu>
• Subject: Set inside Module
• From: HAY at leicester.ac.uk
• Date: Tue, 20 OCT 92 16:36:14 GMT

```Michael Trott observes
In[] y=1;g[y_]=y;{g[y],g[b]}
Ou[] {1, 1}

but

In[] Module[{x=1},f[x_]=x;{f[x],f[a]}]
Out[] {1, a}

and asks how to get the first output from a modification of the second
expression.

The reason for the behaviour of he second expression is that the two

occurrences of  x  in f[x_]=x are not free and so are not renamed. The

evaluation is therefore of
x\$12 = 1;
f[x_] = x;{f[x\$12],f[a]}
(*where \$ModuleNumber = 12*)
{1, a}
This can be seen by entering
Clear[f];
TracePrint[Module[{x=1,},f[x_]=x;{f[x],f[a]}]]

Block,which does not rename, behaves like the first expression
Clear[f];
Block[{x=1},f[x_]=x;{f[x],f[a]}]
{1, 1}
Some other possibilities are
Clear[f];
Module[{x=1,y},f[y_] = (y/.y->x);{ f[x],f[a]}]
{1, 1}
Clear[f];
Module[{x=1,y},f[y_] = y/.y->x);{ f[x],f[a]}]
{1, 1}
BUT
Clear[f];
Module[{x=1,y},(f[y_] = y)/.y->x;{ f[x],f[a]}]
{1, a}

Allan Hayes
hay at leicester.ac.uk

```

• Prev by Date: Re: Transparency in 3D objects?
• Next by Date: Linear Programming
• Previous by thread: Set inside Module
• Next by thread: sound with Soundblaster Card