Re: Taking either a sequence or a list
- To: mathgroup at smc.vnet.net
- Subject: [mg63318] Re: Taking either a sequence or a list
- From: Maxim <m.r at inbox.ru>
- Date: Fri, 23 Dec 2005 05:08:40 -0500 (EST)
- References: <dnm29m$89q$1@smc.vnet.net> <dnopf9$2gp$1@smc.vnet.net> <dnrd16$kqf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Thu, 15 Dec 2005 09:29:10 +0000 (UTC), Maxim <m.r at inbox.ru> wrote: > On Wed, 14 Dec 2005 09:43:05 +0000 (UTC), dh <dh at metrohm.ch> wrote: > >> >> I first tried the more obvious: >> >> f[ x : (__Integer | {__Integer})] >> >> but this doesn not work. It is not clear to my why not, but may be there >> is somebody who has an explanation. >> > > This definitely looks like an inconsistency: > > In[1]:= MatchQ[{a}, {__Integer | a}] > > Out[1]= True > > In[2]:= MatchQ[{a}, {x : __Integer | a}] > > Out[2]= False > > Just adding a pattern name shouldn't change the outcome. The problem is > that one alternative is a sequence and another is a single element. This > would work: > > In[3]:= MatchQ[{a}, {x : __Integer | (a)..}] > > Out[3]= True > > It is also unclear exactly what value is assigned to x here: > > In[4]:= tmp = {a} /. {x : __} -> x > > Out[4]= a > > In[5]:= tmp2 = {a} /. {x : __ | __} -> x > > Out[5]= Sequence[a] > > In[6]:= (HoldComplete[tmp] /. OwnValues[tmp]) === > (HoldComplete[tmp2] /. OwnValues[tmp2]) > > Out[6]= False > > So there might be some discrepancy if the results are later used as > arguments to functions with the attribute SequenceHold. > > Maxim Rytin > m.r at inbox.ru > Here's another curious inconsistency: In[1]:= MatchQ[{a, a}, {s_, s_} /; True /; False] Out[1]= False In[2]:= MatchQ[{a, a}, {s__, s__} /; True /; False] Out[2]= True Perhaps Mathematica takes some shortcuts when dealing with patterns that require a combinatorial search (sequences or Orderless/Flat functions), but such 'simplified' handling of certain classes of patterns can produce rather strange results. Maxim Rytin m.r at inbox.ru
- Follow-Ups:
- Re: Re: Taking either a sequence or a list
- From: "Oyvind Tafjord" <tafjord@wolfram.com>
- Re: Re: Taking either a sequence or a list