[Date Index]
[Thread Index]
[Author Index]
RE: Help: Why no output?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg34948] RE: [mg34941] Help: Why no output?
*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
*Date*: Sat, 15 Jun 2002 02:27:30 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
> -----Original Message-----
> From: kzhang at flashmail.com [mailto:kzhang at flashmail.com]
To: mathgroup at smc.vnet.net
> Sent: Friday, June 14, 2002 8:39 AM
> Subject: [mg34948] [mg34941] Help: Why no output?
>
>
> 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
>
Kezhao,
to resolve the puzzle we have to follow the evaluation sequence of e.g.
f[0.5] (or equally well f[{1,2}], both cases are quite the same). So the
decisive question is: what is the value of n___, when n is not given?
...
..well Pattern[n, BlankNullSequence[Integer]] just matches to the null
sequence!
...now we have to ask what happens to the right side of the transformation
rule?
That's simple, just replace n by nothing
f[0.5] --> If[Length[{}]==1, Length[0.5]]
n just has disappeared from the expression! The condition becomes False, but
the else-case is no more present, so Null is returned. That is the value of
f[0.5], but that is not printed.
This is much the same as
Unevaluated[If[Length[{n}]==1, n, Length[x]]] /. n -> Sequence[]
If you now consider that
In[7]:= 1*Sequence[]
Out[7]= 1
then you'll find the answer to your second question.
--
Hartmut
Prev by Date:
**RE: 3d table or list to file**
Next by Date:
**RE: Help: Why no output?**
Previous by thread:
**RE: Help: Why no output?**
Next by thread:
**RE: Help: Why no output?**
| |