Re: Using substitution rules to define a function

• To: mathgroup at yoda.physics.unc.edu
• Subject: Re: Using substitution rules to define a function
• From: "Dr A. Hayes" <hay at leicester.ac.uk>
• Date: Fri, 1 Apr 1994 10:47:25 +0100 (BST)

```Jean:
Here is a simplified version of your problem and a solution to it.

PROBLEM
In[44]:=
sol = {{x->w},{x->-w}}
Out[44]=
{{x -> w}, {x -> -w}}
In[45]:=
f[w_] := sol[[1,1,2]];
In[46]:=
f[t]
Out[46]=
w

SOLUTION
Use Set instead of SetDelayed:
In[47]:=
f[w_] = sol[[1,1,2]];
In[48]:=
f[x]
Out[48]=
x

EXPLANATION
With
In[49]:=
f[w_] := sol[[1,1,2]];
the rule stored is
In[50]:=
?f
Global`f
f[w_] := sol[[1,1,2]]
(unevaluated right side)
so when f[x] there is no w for the slot w_
to relate to as a place to substitute x for w.
In[51]:=
TracePrint[f[x]]
f[x]
f
x
sol[[1,1,2]]
Part
sol
{{x -> w}, {x -> -w}}
1
1
2
{{x -> w}, {x -> -w}}[[1,1,2]]
w
Out[51]=
w

But with
In[52]:=
f[w_] = sol[[1,1,2]];
the rule stored is
In[53]:=
?f
Global`f
f[w_] = w
and the evaluation process is
In[54]:=
TracePrint[f[x]]
f[x]
f
x
x
Out[54]=
x

Allan Hayes
hay at le.ac.uk

```

• Prev by Date: 3D Plotting in Mathematica of Data from an External File
• Next by Date: ListContourPlot3D ?
• Previous by thread: 3D Plotting in Mathematica of Data from an External File
• Next by thread: ListContourPlot3D ?