Re: Which inside Module causes problems with ReplaceAll

• To: mathgroup at smc.vnet.net
• Subject: [mg111390] Re: Which inside Module causes problems with ReplaceAll
• From: Andy Ross <andyr at wolfram.com>
• Date: Fri, 30 Jul 2010 06:56:23 -0400 (EDT)

```On 7/29/2010 5:44 AM, P. Fonseca wrote:
> 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
>

I believe the following gives a clue...

test[x_] := Module[{u},
Which[x == 0, 0, True, u[t_] := t^2;u[x]]]

In[11]:= test[t]

Out[11]= Which[t == 0, 0, True, u\$612[t_] := t^2; u\$612[t]]

Since t == 0 doesn't explicitly evaluate to False, the entire Which
remains unevaluated.  You could use either of the following to ensure
the first check always evaluates to either True or False.

SameQ:

In[12]:= test[x_] := Module[{u}, Which[x === 0, 0, True, u[t_] := t^2;
u[x]]]

In[13]:= test[t]

Out[13]= t^2

TrueQ:

In[14]:= test[x_] :=
Module[{u}, Which[TrueQ[x == 0], 0, True, u[t_] := t^2;
u[x]]]

In[15]:= test[t]

Out[15]= t^2

-Andy

```

• Prev by Date: Re: All pairs shortest paths
• Next by Date: Re: discretized Laplacian or linear inverse problem with extremely
• Previous by thread: Re: Which inside Module causes problems with ReplaceAll
• Next by thread: Re: Which inside Module causes problems with ReplaceAll