MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Help: Why no output?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34956] RE: [mg34941] Help: Why no output?
  • From: "DrBob" <majort at cox-internet.com>
  • Date: Sat, 15 Jun 2002 02:27:41 -0400 (EDT)
  • Reply-to: <drbob at bigfoot.com>
  • Sender: owner-wri-mathgroup at wolfram.com

By the way, a solution is to use a definition like this:

ClearAll[f]
f[x_, n__Integer] := If[Length[{n}] == 1, n  , Length[x]]
f[x_]:=Length[x]

(two underscores where you had three)

Even better, you could use

ClearAll[f]
f[x_, n_Integer] := n
f[x_, n___Integer] := Length[x]

(one underscore in the first rule, three in the second)

Bobby

-----Original Message-----
From: DrBob [mailto:majort at cox-internet.com] 
To: mathgroup at smc.vnet.net
Subject: [mg34956] RE: [mg34941] Help: Why no output?

Kezhao,

When you give no second argument to f, n is Sequence[]... a list of
arguments to place in some function, but a list of zero length!

Hence in that case

If[Length[{n}] == 1, n, Length[x]]

is just:

If[Length[{}] == 1, Length[x]]

(NO arguments are inserted at the position of n.)

Now, also

1*Sequence[]

resolves to

Times[1, Sequence[]]

and hence to

Times[1]

and hence to

1

Therefore, when n=Sequence[], the following

If[Length[{n}] == 1, 1*n, Length[x]]

resolves to

If[Length[{}] == 1, 1, Length[x]]

which is just

Length[x]

Bobby Treat

-----Original Message-----
From: Kezhao Zhang [mailto:kzhang at flashmail.com] 
To: mathgroup at smc.vnet.net
Subject: [mg34956] [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





  • Prev by Date: RE: Help: Why no output?
  • Next by Date: Re: Help: Why no output?
  • Previous by thread: RE: Help: Why no output?
  • Next by thread: Re: Help: Why no output?