Re: Help: Why no output?
- To: mathgroup at smc.vnet.net
- Subject: [mg34951] Re: [mg34941] Help: Why no output?
- From: BobHanlon at aol.com
- Date: Sat, 15 Jun 2002 02:27:33 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
In a message dated 6/14/02 3:18:53 AM, kzhang at flashmail.com writes:
>The behavior of the following function is puzzling to me:
>
>In[1]:=f[x_, n___Integer] := If[Length[{n}] == 1, n , Length[x]]
>In[2]:=f[0.5] (* Nothing returned. *)
>In[3]:=f[{1,2}] (* Nothing returned. The length of x should be
>returned *)
>(* However, with the modification to f, everything works as intended
>*)
>In[4]:=f[x_, n___Integer] := If[Length[{n}] == 1, 1*n , Length[x]]
> ^^^^^^
>In[5]:=f[0.5]
>Out[5]:=0
>In[6]:=f[{1,2}]
>Out[6]:=2
>
>Could anyone help me understand why changing n to (1*n) in the If[]
>statement makes such difference? Why doesn't f[] defined in the In[1]
>work?
Use Trace to see what happens
f[x_, n___Integer] := If[Length[{n}]==1, n, Length[x]];
Trace[f[0.5]]
{f[0.5], If[Length[{}]==1, Length[0.5]], {{Length[{}], 0}, 0==1, False},
If[False, Length[0.5]], Null}
f[x_, n___Integer] := If[Length[{n}]==1, 1*n, Length[x]];
Trace[f[0.5]]
{f[0.5], If[Length[{}]==1, Times[1], Length[0.5]], {{Length[{}], 0}, 0==1,
False},
If[False, Times[1], Length[0.5]], Length[0.5], 0}
Bob Hanlon
Chantilly, VA USA