Re: Help: Why no output?
- To: mathgroup at smc.vnet.net
- Subject: [mg34952] Re: [mg34941] Help: Why no output?
- From: Andrzej Kozlowski <andrzej at bekkoame.ne.jp>
- Date: Sat, 15 Jun 2002 02:27:35 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
A correction to my earlier message. The first lines should have been:
g[x___]:=h[a,x,b]
In[2]:=
g[]
Out[2]=
h[a,b]
In[3]:=
Clear[g]
In[4]:=
g[x___]:=h[a,1*x,b]
In[5]:=
g[]
Out[5]=
h[a,1,b]
and the rest as I originally wrote.
On Friday, June 14, 2002, at 06:09 PM, Andrzej Kozlowski wrote:
> The explanation lies in the following behaviour :
>
> In[1]:=
> g[x___]:=h[a,x,b]
>
> In[2]:=
> g[]
>
> Out[2]=
> h[a,b]
>
> In[3]:=
> g[1*x]
>
> Out[3]=
> h[a,x,b]
>
> This behaviour is analogous to what you would get if you inserted used
> Sequence[] as your second argument:
>
> In[4]:=
> h[a,Sequence[],b]
>
> Out[4]=
> h[a,b]
>
> In[5]:=
> h[a,x*Sequence[],b]
>
> Out[5]=
> h[a,x,b]
>
> However, it does not seem that Sequence is actually used, since
>
> In[6]:=
> SetAttributes[h,SequenceHold]
>
> In[7]:=
> g[]
>
> Out[7]=
> h[a,b]
>
> In[8]:=
> h[a,Sequence[],b]
>
> Out[8]=
> h[a,Sequence[],b]
>
> In your case what happens is this. Let's just consider f[{1,2}]. As
> there is no second argument the function If turns into
> If[Length[{1,2}==1,Length[{1,2}]] (since there was no n, it vanished,
> just as if you had Sequence[] as the second argument). But this
> evaluates to If[False,Length[{1,2}]]. Since nothing is specified to be
> returned from If in case of False, nothing is returned. However, in the
> other case you get If[Length[{1,2}]==1,1,Length[{1,2}]] , which of
> course evaluates to If[False,1,Length[{1,2}]] which returns
> Length[{1,2}] which is 2.
>
> Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/
>
> On Friday, June 14, 2002, at 03:38 PM, Kezhao Zhang wrote:
>
>> 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?
>>
>> Thanks for your help.
>>
>> Kezhao Zhang
>>
>>
>>
>