Re: Which inside Module causes problems with ReplaceAll
- To: mathgroup at smc.vnet.net
- Subject: [mg111385] Re: Which inside Module causes problems with ReplaceAll
- From: "Tony Harker" <a.harker at ucl.ac.uk>
- Date: Fri, 30 Jul 2010 06:55:21 -0400 (EDT)
The problem arises becauses when you evaluate test[t] it returns the Which unevaluated (as it must, as t does not have a value), so it contains the Module-generated name of the form u$nnn in the statement u$nnn[t_]:=t^2. What then trips it up is converting t_ to 3_, which is invalid. If you really need a function of this sort, you can use a pure function form for u. For example, test[x_] := Module[{u}, Which[x == 0, 0, True, u = #^2 &; u[x]]] Tony Harker ]-> -----Original Message----- ]-> From: P. Fonseca [mailto:public at fonseca.info] ]-> Sent: 29 July 2010 11:44 ]-> To: mathgroup at smc.vnet.net ]-> Subject: [mg111375] Which inside Module causes problems ]-> with ReplaceAll ]-> ]-> Hi, ]-> ]-> Version 7.0.1 ]-> ]-> This works: ]-> ]-> In[7]:= test[x_]:=Module[{u}, ]-> u[t_]:=t^2; ]-> u[x]] ]-> ]-> In[8]:= test[t]/.t->3 ]-> ]-> Out[8]= 9 ]-> ]-> ]-> ]-> ]-> This doesn't: ]-> ]-> In[9]:= test[x_]:=Module[{u}, ]-> ]-> Which[ ]-> x==0,0, ]-> ]-> True, ]-> u[t_]:=t^2; ]-> u[x] ]-> ] ]-> ] ]-> ]-> In[10]:= test[t]/.t->3 ]-> ]-> During evaluation of In[10]:= Pattern::patvar: First element in ]-> pattern Pattern[3,_] is not a valid pattern name. >> ]-> Out[10]= u$670[3] ]-> ]-> ]-> ]-> What's going on? ]-> ]-> Regards, ]-> P. Fonseca ]-> ]-> ]->