I've been having an issue with functions which are supposed to receive as input a list of tuples. My troubles are two-fold:
- The function doesn't properly identify the pattern test I laid out in the definition of the inputs.
- The pattern matching itself is not ideal for a list of (same length) tuples and I couldn't find a more suitable pattern to match for.
My issue was originally in a very convoluted function, but I managed to replicate it in a simpler form.
g[x_, {a_, b_}] := a x^b (*generic function - actual definition of G is irrelevant other than the input structure*)
SetAttributes[myfun, HoldFirst]
myfun[f_[args__, pars:{__}], x_ : {{__} ..}] := f[Sequence @@ #, pars] & /@ x
First I check to see if my pattern matches what I want it to do (more or less as it still doesn't properly identify tuples of different lengths).
In[58]:= MatchQ[Tuples[Range@10,1],{{__}..}]
MatchQ[Range@10,{{__}..}]
MatchQ[x,{{__}..}]
Out[58]= True
Out[59]= False
Out[60]= False
Note that a simple Range doesn't trigger the pattern match and neither does a generic symbol x (The symbol returns a weird output which I just noticed after posting this question - the function is not applied and yet the symbol is returned). Despite this:
In[68]:= myfun[g[x, {a, b}], Range[2, 4]] (*Regular range*)
myfun[g[x, {a, b}],Tuples[ Range[2, 4],1]] (*Properly formatted tuples*)
myfun[g[x,{a,b}],x] (*Symbol (!) - still works*)
Out[68]= {2^b a,3^b a,4^b a}
Out[69]= {2^b a,3^b a,4^b a}
Out[70]= x
My only conclusion is that for some reason the function does not evaluate the condition I set. Were I a betting man, my money is likely on problematic syntax. Also, if there is a better way to match a list of tuples I would appreciate the insight.
Help is greatly appreciated!
x_List?MatrixQ
? $\endgroup$?
syntax (PatternTest) is explained a little further down. $\endgroup$