Re: Taking either a sequence or a list
- To: mathgroup at smc.vnet.net
- Subject: [mg63094] Re: Taking either a sequence or a list
- From: dh <dh at metrohm.ch>
- Date: Wed, 14 Dec 2005 04:35:56 -0500 (EST)
- References: <dnm29m$89q$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Trevor, the following should work: Clear[f] f[x : __?(MatchQ[{1, 2}, (__Integer | {__Integer} )] &)] := Print[x]; f[{1, 2}] f[1, 2] 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. HOWEVER, I think your approach is clearer because inside the function you will have to separate the two cases anyway. The only thing you achieve, is to make things more complicated. Daniel Trevor Baca wrote: > I somtimes SetDelay expressions twice, as in > > Foo[L:{___Integer}] := StringJoin["* * * ", ToString[Length[L]], " * > * * "]; > Foo[n___Integer] := Foo[{n}]; > > reason being that I can then equivalently call either > > Foo[{1, 2, 3, 4, 5}] > Foo[1, 2, 3, 4, 5] > > and not have to worry about whether Foo[ ] takes a string or sequence > as argument. (StringJoin[ ] works the same way, which is where I grew > to like the idea.) > > Question: is there a better way to accomplish this type of setting up > both a list-based and sequence-based interface to an expression, other > than twice calling SetDelay explicitly (perhaps by cleverly using > SetAttributes[ ])? > > Trevor. >