Evaluation of Rules

• To: mathgroup at christensen.cybernetics.net
• Subject: [mg600] Evaluation of Rules
• From: seligjm at vax.sbu.ac.uk
• Date: Fri, 24 Mar 1995 10:06:18 BST

```Dear mathgroup,
a collegue asked me the following,

A) Consider the following

In[1]:=  f[x_]:=f[x]

Now try

In[2]:=  f[1]

We get

Out[2]=   \$IterationLimit::itlim:
Iteration limit of 4096 exceeded.
Hold[f[1]]

as expected, f[x_]:=f[x] is applied forever, the recursion does not end.

Now try the following definition:

In[3]:=   g[1]:=g[1]

then try

In[4]:=  g[1]

you get

Out[4]=  g[1]

In this case g[1]:=g[1] is applied (it seems) only once to g[1] but not to the
result of the first transformation. Why ??

B) why does

In[5]:=  h[g[2]]//.{g[2]:>1,h[g[2]]:>2}

give

Out[5]=  2

and not h[1]  ? i.e. why does h[g[2]]:>2 get applied first ?

I think the problem is that Mma treats rules defined by Set, SetDelayed etc
is different from the way it treats rules defined by Rule and RuleDelayed
evaluated by ReplaceAll etc.  If this is true it is certainly not documented
in Wolfram's book.