RE: Re: Defining a function in module problem?
- To: mathgroup at smc.vnet.net
- Subject: [mg46019] RE: [mg46007] Re: Defining a function in module problem?
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 3 Feb 2004 03:20:49 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: phbrf at t-online.de [mailto:phbrf at t-online.de] To: mathgroup at smc.vnet.net >Sent: Monday, February 02, 2004 11:21 AM >To: mathgroup at smc.vnet.net >Subject: [mg46019] [mg46007] Re: Defining a function in module problem? > > >jose flanigan <jflanigan at netzero.net> wrote: > >> a[x_] := Module[{f, g}, g = z/3; f = Function[z, Evaluate[g]]; f[x]] > >you could use > >a[x_] := Module[{f, g}, g[z_] = z/3; f = Function[z, >Evaluate[g]]; f[x]] > >or even more simple: > >a[x_] := Module[{f}, f = Function[z, Evaluate[z/3]]; f[x]] > >-- >Gruß Peter >-- >==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== >Peter Breitfeld, Bad Saulgau, Germany -- <http://www.pBreitfeld.de> > Peter, your first example doesn't work, as g has no DownValue. Perhaps you meant: Clear[z] a[x_] := Module[{f, g}, g[z_] = z/3; f = Function[z, Evaluate[g[z]]]; Scan[Information, Hold[f, g]]; f[x] // Trace] a[1] effectively this is the same as your second proposal: a[x_] := Module[{f}, f = Function[z, Evaluate[z/3]]; Information[f]; f[x] // Trace] a[1] Both break, if z has a global value. The second case should be replaced by a[x_] := Module[{f}, f = Function[z, z/3]; f[x]] but is completely trivial then. The first case by a[x_] := Module[{f, g}, g[z_] := z/3; f = Function[z, Evaluate[g[z]]]; f[x]] This method now opens the body to the renaming mechanism for z (see my reply to this post). Now, when renaming z -> z$ occurs for the definition of f, then z is visible to the renaming mechanism: Function[z, Evaluate[g[z]]] --> Function[z$, Evaluate[g[z$]] --> Function[z$, z$/3] In fact this might be considered as a nice trick to succeed not knowing whteher renaming will occur or not. My recomendation however was different (use Block). -- Hartmut