Re: Pattern test and expression sequence
- To: mathgroup at smc.vnet.net
- Subject: [mg83147] Re: [mg83122] Pattern test and expression sequence
- From: DrMajorBob <drmajorbob at bigfoot.com>
- Date: Tue, 13 Nov 2007 03:35:21 -0500 (EST)
- References: <3729243.1194879781670.JavaMail.root@m35>
- Reply-to: drmajorbob at bigfoot.com
x__?myQ matches one or more elements, EACH of which must satisfy the myQ test (by itself). But you want the full List of them to satisfy the test, as in your workaround, so I'd define it this way: f[x__]/;myQ[x]:=works[x] or, for that particular test... (Length[{x}] > 1)... f[first_,others__]:=works[first,others] Bobby On Mon, 12 Nov 2007 04:11:00 -0600, Jan Lellmann <lellmann at uni-mannheim.de> wrote: > Hello, > > I want to define a pattern that accepts a sequence of expressions (of > arbitrary length) that matches some condition. So as a minimalist > example, if I wanted to accept all expressions with f as head and at > least 2 arguments, I want to write > > myQ[x__] := (Print[x]; Length[{x}] > 1) > f[x__?myQ] := works[x]; > f[1, 2] > > This should print "1 2" and return "works[1,2]". But it doesn't, it just > prints "1" and returns f[1,2] unevaluated. So apparenty Mathematica does > not pass the complete expression sequence to myQ, but only the first > element ("1"). > > There is a workaround using /; : > f[x__] /; (myQ[x] == True) := works[x]; > > but it is rather ugly and cumbersome when there is more than one > argument. > > Does anyone have an idea why Mathematica does this? Is it a bug? Or is > there a (nice) workaround? > > Best regards, > Jan > > -- DrMajorBob at bigfoot.com