MathGroup Archive 2002

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

Search the Archive

Re: Optional

  • To: mathgroup at smc.vnet.net
  • Subject: [mg33696] Re: [mg33689] Optional
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Mon, 8 Apr 2002 03:04:24 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I haven't got  Mathematica 3.0 installed anymore but if what you say is 
true the behaviour of 4.1 seems more reasonable to me. Observe that:

In[1]:=
Clear[f]

In[2]:=
f[Optional[index : {_Integer }.., {1}]] := index

In[3]:=
f[]

Out[3]=
{1}

On the other hand

In[4]:=
Clear[f]

In[5]:=
f[Optional[index : {_Integer }..., {1}]] := index

In[6]:=
f[]

Out[6]=
Sequence[]


This seems perfectly logical to me. In the first case we use .. which 
means one repetition or more. Since the empty collection of arguments 
does not match this the default is used. On the other hand, in the 
second case we use ..., which means zero repetitions or more. No 
arguments matches zero repetitions so you get Sequence[] as the output.


  Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/


On Saturday, April 6, 2002, at 02:49  PM, Ben Langton wrote:

> Hi all,
>
> I am having a problem with the Optional function which I have reduced 
> to the
> following snippet of code :
>
> f // Clear;
> f[Optional[index : {_Integer ...}, {1}]] := index;
> f[]
>
> My understanding of the Optional command is that the function f should
> accept a list of zero or more integers as its argument or, if that is
> missing, use the default value of {1}.
>
> Under version 3, f[] (f with no arguments) returns {1}, as I would 
> expect,
> yet under version 4.1 it returns the function evaluated.
>
> Can anyone shed any light on why this code acts differently under the 
> two
> different versions? What can I do to make it work as expected under 
> version
> 4.1?
>
> Many thanks,
>
> Ben Langton
>
>
>



  • Prev by Date: Re: Using rules from Solve
  • Next by Date: RE: Symbols using Prolog
  • Previous by thread: RE: Optional
  • Next by thread: Re: Optional