Re: Putting an expression into a module
- To: mathgroup at smc.vnet.net
- Subject: [mg84886] Re: Putting an expression into a module
- From: dh <dh at metrohm.ch>
- Date: Fri, 18 Jan 2008 05:44:14 -0500 (EST)
- References: <fmngh8$3fl$1@smc.vnet.net>
Hi Hugh, here is a solution. You must first prevent evaluation, replace the expression and then allow evaluation: Hold[f[]:=Module[{x=e1},Print[x]]] /. e1\[Rule]e // ReleaseHold Why your attempts did not work, see below. hope this helps, Daniel Hugh wrote: > Suppose you have generated a long symbolic expression, e, and now wish > to use the expression inside a Module. One method would be to cut-and- > paste the expression into the Module but how do you do this without > cutting-and-pasting? > Below in a warm up problem I first get what I want by cutting-and- > pasting and then try various combinations of Hold, ReleaseHold and > Evaluate. These don't work (why?) but how can it be done? > > Thanks > Hugh Goyder > > e = y^2 - x^2 > > ClearAll[f]; > f[x_] := Module[{y}, y /. FindRoot[y^2 - x^2, {y, 1}]] > > ClearAll[f]; > ReleaseHold[f[x_] := Hold[Module[{y}, y /. FindRoot[e1, {y, 1}]]] /. > e1 -> e] ReleaseHold only removes one layer of Hold, It does not remove "inner Holds" > > ClearAll[f]; > ReleaseHold[ > f[x_] := Evaluate[Hold[Module[{y}, y /. FindRoot[e1, {y, 1}]]] /. > e1 -> e]] same as above > > ClearAll[f]; > Evaluate[ReleaseHold[ > f[x_] := Hold[Module[{y}, y /. FindRoot[e1, {y, 1}]]] /. > e1 -> e]] same as above > > ClearAll[f]; > ReleaseHold[ > f[x_] := Evaluate[Hold[Module[{y}, y /. FindRoot[e1, {y, 1}]]] /. > e1 -> e]] same as above > > ClearAll[f]; > f[x_] := Evaluate[ > ReleaseHold[Hold[Module[{y}, y /. FindRoot[e1, {y, 1}]]] /. > e1 -> e]] here you force FindRoot to be evaluated >