Re: Scoping (was Procedure to Function)
- To: mathgroup at smc.vnet.net
- Subject: [mg8361] Re: [mg8236] Scoping (was Procedure to Function)
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Tue, 26 Aug 1997 02:22:58 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Robert Villegas <villegas at wolfram.com>
in [mg8236] Re: Procedure to Function
Gives the following example of a technique for avoiding renaming in
scoping constructs (replace every lhs := rhs with,SetDelayed @@
HoldComplete[lhs, rhs]):
Clear[u, v, x, y, g, HarmonicConjugate]
HarmonicConjugate[u_] :=
(SetDelayed @@ HoldComplete[
v[x_, y_], Integrate[D[u,x],y] + g[x]];
SetDelayed @@ HoldComplete[g[x_], Evaluate[
g[x] /.DSolve[D[v[x, y],x]==D[u,y], g[x], x][[1]]
]];
v[x, y]
)
Here are two additional tricks, based on replacement not being a
scoping construct, and so not triggering variable renaming:
(1) Clear[u, v, x, y, g]
HarmonicConjugate4[u_] :=
(Hold[ v[x_, y_] := Integrate[D[uu,x],y] + g[x];
g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[uu,y], g[x],
x][[1]]];
v[x, y]]/.uu->u)//ReleaseHold
HarmonicConjugate4[x^3 - 3 x y^2 + y]
x - 3*x^2*y - y^3 + C[1]
(2) Clear[u, v, x, y, g]
HarmonicConjugate5[u_] :=
(Unevaluated[ v[x_, y_] := Integrate[D[uu,x],y] + g[x];
g[x_] := Evaluate[g[x] /.DSolve[D[v[x, y],x] == D[uu,y], g[x],
x][[1]]];
v[x, y]]/.uu->u)
HarmonicConjugate5[x^3 - 3 x y^2 + y]
x - 3*x^2*y - y^3 + C[1]
Allan Hayes
hay at haystack.demon.co.uk
http://www.haystack.demon.co.uk/training.html
voice:+44 (0)116 2714198
fax: +44 (0)116 2718642
Leicester, UK