Re: Optional
- To: mathgroup at smc.vnet.net
- Subject: [mg33712] [mg33712] Re: [mg33689] Optional
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Tue, 9 Apr 2002 01:03:04 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I took another look at your original posting and my reply and noticed I
had mis-typed your definition of f, which resulted in rather different
behaviour from the one you experienced. The crucial difference is the
position of the curly brackets {}.
As you correctly pointed out the definition
In[1]:=
f[Optional[index : {_Integer... }, {1}]] := index
leads to
In[2]:=
f[]
Out[2]=
f[]
Replacing ... with .. makes no difference here. We can see however that
besides the example that I mistook for yours the following one also
works fine:
In[3]:=
f[Optional[index : _Integer .., 1]] := index
In[4]:=
f[]
Out[4]=
1
So it appears that in general the following construction will not work:
f[Optional[index : F[p..], F[q]]] := something
even when q matches p (eg. q is p) except when F=Identity.
This indeed seems odd and although there are rather obvious ways to get
round this I would now tend to think that it may be a bug, particularly
if it indeed used to work in version 3.
On Saturday, April 6, 2002, at 11:05 PM, Andrzej Kozlowski wrote:
> 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
>>
>>
>>
>